0

My first post on here. I have a function on a website whereby a randomly generated French phrase is displayed, challenging the reader to translate it into English in a text box. On clicking on a button, the text entered is compared to all the possible answers (there are multiple correct translations for a given phrase). I've looked around for answers on this but nothing seems to suit my situation.

Here's the jQuery:

var correctAnswer = function(){$('#correctmessage').show('fast');$('#errormessage').hide('fast');}
var wrongAnswer = function(){$('#errormessage').show('fast');$('#correctmessage').hide('fast');}

$('#1').find('button').on('click', function(){
    var text = $(this).parent().find('.translatefield').val();
    var compareText = "I went to the cinema";
    var compareText2 = "I have been to the cinema";
    if (text == compareText || text == compareText2) {
        correctAnswer();
    }
    else {
        wrongAnswer(); 
    }
});

So I wondered if I can put the 'compare' variables into one variable i.e. 'I went to the cinema OR I have been to the cinema OR etc etc' within one variable for tidiness. But mainly I need to know how I can call that variable within the if so that it also accepts the answer without accented characters and regardless of upper or lower case... I hope this is clear! Thanks for any help you can give, this has been irritating me for a while!

TMG
  • 97
  • 11
  • 1
    You could put the phrases in an array. `var compareText = ['I went to the cinema','I have been to the cinema']` Then you could use the array .some() method to test. – Michael Oakley Feb 03 '16 at 18:39
  • Most likely easiest solution is to replace everything to its equivalent first, eg replace e-accent with e / all uppercase to lowercase, then you only need to check for 'e' – freedomn-m Feb 03 '16 at 18:42

3 Answers3

1

As commented by Mark Holland, use arrays for the compare phrases.

If you are using jQuery anyway, you could use jQuery.inArray().

https://api.jquery.com/jQuery.inArray/

var compareText = ['i went to the cinema','i have been to the cinema'];

if ($.inArray(text.toLowerCase(), compareText)) {
   ... do stuff
}

To ignore the accents, use a solution like this:

String.prototype.removeAccents = function(){
 return this
         .replace(/[áàãâä]/gi,"a")
         .replace(/[éè¨ê]/gi,"e")
         .replace(/[íìïî]/gi,"i")
         .replace(/[óòöôõ]/gi,"o")
         .replace(/[úùüû]/gi, "u")
         .replace(/[ç]/gi, "c")
         .replace(/[ñ]/gi, "n")
         .replace(/[^a-zA-Z0-9]/g," ");
}

Credits to Luan Castro Perform a find/match with javascript, ignoring special language characters (accents, for example)?

Community
  • 1
  • 1
jossiwolf
  • 1,865
  • 14
  • 22
  • 1
    Hi kernelmaster, thanks a lot for the help. I got the arrays working (wouldn't work with ($.inArray, but it works with jQuery.inArray - any idea why that is? Probably something obvious I'm missing). The accent character removal worked great too - thank you for linking me to Castro's solution. – TMG Feb 04 '16 at 17:43
0

As mentionned by Mark Holland, arrays would answer your first question.

JS arrays

To ignore accents, a quick search gave me this answer:

Replace accents

And to ignore lower/uppercase, a quick search gave me this answer.

Ignore case

Community
  • 1
  • 1
phenxd
  • 689
  • 4
  • 17
0

How about setting the strings into one array and iterate this array to compare with the answer?

Gabriel Marques
  • 276
  • 1
  • 3
  • 15