views:

769

answers:

3

Hello, I'm building a short quiz where the user needs to input the meaning of an acronym. This means I need to compare a long string (usually a sentence) typed in by the user with an acronym. I have a feeling I'm not doing it right. For my testing I'm copy-pasting the correct answer to make sure the spelling is correct however I keep getting the feedback that the answer is incorrect. My question is, am I comparing correctly? Thank you!

Here's my code:

 var arrQuestions:Array = [["LOL","Laughing Out Loud"], ["OMG", "Oh My God"], ["BTW", "By The Way"]];
var i:Number=0;

    function setup():void {
     quiztext_txt.text = arrQuestions[i][0];
     trace(quiztext_txt.text);
     trace(arrQuestions[i][1]);
     check_btn.addEventListener(MouseEvent.CLICK, clickHandler);


    }//End of Setup()

    setup();

    function clickHandler(event:MouseEvent):void {

     var givenString:String;
     var inputString:String;
     inputString = userinput_txt.text;
     givenString = arrQuestions[i][1];
     if (inputString == givenString) {
      feedback_txt.text = "Correct!";


     } else {
      feedback_txt.text = "Wrong!";
     }
    }
A: 

Is there any whitespace before/after the user input? Is the value of i changing in between?

else 
{
  //what does it trace?
  trace("given answer: " + inputString + "\ncorrect answer: " + givenString);
  feedback_txt.text = "Wrong!";
}
Amarghosh
Sarit
trace(inputString.length + ", " + givenString.length);
Amarghosh
A: 

try clearing the text field in your setup function like so:

   function setup():void
   {
     userinput_txt.text = "";
     quiztext_txt.text = arrQuestions[i][0];
     trace(quiztext_txt.text);
     trace(arrQuestions[i][1]);
     check_btn.addEventListener(MouseEvent.CLICK, clickHandler);


    }//End of Setup()
Simon
A: 

For any kind of string matching I would strongly recommend looking into regular expressions (RegExp). In the regular expression written below I am matching each word, then I say [ ]+ which means "at least one or more spaces", then at the end of the expression I use /gi to say that the expression is case insensitive. In the code above if I type the phrase in lowercase its not going to match, a quick fix for this would be to use this if(inputString.toLowerCase() == givenString.toLowerCase()) which would catch this. Heres the regexp example:

// testString could easily equal myTextField.text
var testString:String = "lauGHing      OuT       loUD";

// you could store each one in an array, as you were before
var regEx:RegExp = /laughing[ ]+out[ ]+loud/gi

trace( regEx.test( testString ) ); //returns true,test() returns a Boolean

Hope this helps.