Skip to main content
May 10, 2014
Answered

Referring to multiple objects in Arrays

  • May 10, 2014
  • 1 reply
  • 427 views

I'm experimenting with a q/a application in Flash. Textfield is showing a question. Another inputfield is below for answering. When answering with text, an unformulated sentence can be correct, but not according to what the code expects. In that case I want to write down many different versions of the answer. Questions arrive from an Array. Another Array tells the answer.

For example

var questions:Array = new Array;

questions[0] =  ("How many cars");

answer:Array = new Array;

answer[0] = ("one", "1", "One", "ONE")

The case:

In a function where an if statement is waiting for the inputfield to have the correct answer, how does one write the code?

Example:

(...) function submit (MouseEvent) {

          if      (inputField == ?) {}

         

          else  {trace("wrong")}

     }

Posted via phone, my computer with the actual code doesn't have internet... Should have uploaded the code I know...

This topic has been closed for replies.
Correct answer Amy Blankenship

Maybe what you're looking for is

if (answers[currentQuestionNum].indexOf(inputField.text) > -1) {

     //something

} else {

     trace('wrong');

}

1 reply

Amy Blankenship
Amy BlankenshipCorrect answer
Legend
May 10, 2014

Maybe what you're looking for is

if (answers[currentQuestionNum].indexOf(inputField.text) > -1) {

     //something

} else {

     trace('wrong');

}

May 10, 2014

It worked! Thank you so much!

I didn't quite understand the "< -1" part though. What difference does it make?

Amy Blankenship
Legend
May 10, 2014

You're checking to see if the answer is at any index in the array that contains the admissible answers for that question. Note that it seems like the "make a link in text" functionality is currently broken in the forums, so pretend the word "checking" above links to this Array - Adobe ActionScript® 3 (AS3 ) API Reference