Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Check input fields with Array

Participant ,
Feb 09, 2017 Feb 09, 2017

Hi All,

I'm making a quiz where the user inputs the answer into an input box. I've been trying to use an array to check the answer against.

I have set it up to have 1 question per frame and want to have an array with multiple answers to the question on the frame.

What I currently have;

var CorrectAnswer1: Array = ["DBMS", "dbms", "Database Management System"];

submit.addEventListener(MouseEvent.CLICK, check)

function check(e: MouseEvent): void {

  if (ans1.text == CorrectAnswer1[0,1,2]) {

  result1.text = "Correct";

  } else {

  result1.text = "Incorrect";

  }

}

The error it gives;

ReferenceError: Error #1069: Property 2 not found on Number and there is no default value.

  at UniversalRevision_TEST_fla::MainTimeline/check()

I also tried;

var CorrectAnswer1: Array = new Array ["DBMS", "dbms", "Database Management System"];

Can anyone point me in the right direction how to properly use an array and check against it?

Here's what the frame looks like:

Capture.PNG

Pardon the really bad writing, not easy with a mouse

Thanks,

Jack

584
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Feb 09, 2017 Feb 09, 2017

This right here:

if (ans1.text == CorrectAnswer1[0,1,2])

No, no, noooooo. Comparisons do not work that way. Comparison operators only work between two specific values, not one value and an entire array (also the array syntax itself is invalid). You have to manually loop over the entire array and check against each element. Something like...

var i:Number;

var correct:Boolean = false;

var txt:String = ans1.text.toLowerCase();

for (i = 0; i < CorrectAnswer1.length; i++) {

     if (txt == CorrectAnswer1) {

...
Translate
LEGEND ,
Feb 09, 2017 Feb 09, 2017

This right here:

if (ans1.text == CorrectAnswer1[0,1,2])

No, no, noooooo. Comparisons do not work that way. Comparison operators only work between two specific values, not one value and an entire array (also the array syntax itself is invalid). You have to manually loop over the entire array and check against each element. Something like...

var i:Number;

var correct:Boolean = false;

var txt:String = ans1.text.toLowerCase();

for (i = 0; i < CorrectAnswer1.length; i++) {

     if (txt == CorrectAnswer1) {

          correct = true;

          break;

     }

}

if (correct) {

     result1.text = "Excellent";

}

else {

     result1.text = "Bogus";

}

Note that by applying toLowerCase() to the answer string you don't have to faff about with uppercase/lowercase variations of your answers.

Ah, wait, I see AS3 supports the indexOf() method on strings. In that case you can simplify things quite a bit:

if (CorrectAnswer1.indexOf(ans1.text.toLowerCase()) != -1) {

     result1.text = "Acceptable";

}

else {

     result1.text = "Unacceptable!";

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 09, 2017 Feb 09, 2017

Thanks ClayUUID​, the code is working without errors. This is how I've implemented it;

var CorrectAnswer1: Array = ["DBMS", "Database Management System"];

var i: Number;

var correct: Boolean = true;

var txt: String = ans1.text.toLowerCase();

for (i = 0; i < CorrectAnswer1.length; i++) {

  if (txt == CorrectAnswer1) {

  correct = true;

  break;

  }

}

submit.addEventListener(MouseEvent.CLICK, check)

function check(e: MouseEvent): void {

  if (correct) {

  result1.text = "Excellent";

  } else {

  result1.text = "Bogus";

  }

}

I noticed you said that my syntax is wrong, any advise on what I need to change for my var CorrectAnswer1: Array?

Also var Correct returns "Excellent" or "bogus" depending on if it is true or false, do I need to put the answers there?

Thanks again!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 09, 2017 Feb 09, 2017

Why are you using the long version of the code instead of the short version of the code?

And... why did you split it in half and put some of it in the initialization section and some of it in the event handler???

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 09, 2017 Feb 09, 2017

That is a ridiculously good question! Just changed it now.

This is what it looks like now;

var CorrectAnswer1: Array = new Array();

CorrectAnswer1.push("DBMS");

CorrectAnswer1.push("Database Management System");

trace (CorrectAnswer1);

submit.addEventListener(MouseEvent.CLICK, check)

function check(e: MouseEvent): void {

  if (CorrectAnswer1.indexOf(ans1.text.toLowerCase()) != -1) {

  result1.text = "Acceptable";

  } else {

  result1.text = "Unacceptable!";

  }

}

Upon tracing the CorrectAnswer1, it does show what I have set.

I have put the code within an event handler so when I click submit it shows if its correct or incorrect.

I'm still have the same issue with it displaying the Unacceptable result.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Feb 09, 2017 Feb 09, 2017

Of course it's not going to match, it's converting whatever the user types to all lowercase, but your answers have uppercase letters (and the array literal you were using in the first version of the code was more sensible than what you're doing now with the push statements).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 09, 2017 Feb 09, 2017
LATEST

Ah, I didn't realise. Thanks for the help, working brilliantly now

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Feb 09, 2017 Feb 09, 2017

I've just been having a look into arrays and changed my array to look like this:

var CorrectAnswer1: Array = new Array();

CorrectAnswer1.push("DBMS");

CorrectAnswer1.push("Database Management System");

and it still shows bogus.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines