0

I have two variables, totalGuess and condensedAnswer. I am creating a jQuery click event and if totalGuess doesn't equal condensedAnswer then the click event will not occur and a div called message will display the message "Sorry, but your answer is incorrect. Please try again."

The problem is, totalGuess in the if statement is never equal to condensedAnswer. I've tried seeing typeof and they are both strings. I've tried console.log(totalGuess+"\n"+condensedAnswer); and they both return the same value. I've tried hardcoding the condensedAnswer, and totalGuess was able to be equal to the hardcoded answer. But when I tried comparing condensedAnswer with the hardcoded answer, it's not equal, even though the console.log value for condensedAnswer is the same. I'm not what's wrong.

Here's the code snippet:

$('.submitGuess').click(function(e){
    var totalGuess = "";
var condensedAnswer = answer.replace(new RegExp(" ","g"), "");
$('.crypto-input').each(function(){
    totalGuess += $(this).val();
    });
// if incorrect guess
if(totalGuess !== condensedAnswer) {
    $('.message').text("Sorry, but your answer is incorrect. Please try again.");
    e.preventDefault();
}
// if user wins, congratulate them and submit the form
else {
    return true;
}
});

If it helps, here's the page, just a random test cryptogram plugin for Wordpress: http://playfuldevotions.com/archives/140

Timigen
  • 1,055
  • 1
  • 17
  • 33
Sue
  • 378
  • 1
  • 12
  • This is a horrible way of doing it, and it will almost certainly fail *a lot*. You're adding all the input values up and comparing to a string where you've removed all spaces, one character off or space in the wrong place for one single input, and it fails. – adeneo May 08 '14 at 15:50
  • Works for me: http://jsfiddle.net/8wndm/ type `foobarbar` and the guess will be correct. Problem is likely elsewhere, or is related to code you have not shown. – Kevin B May 08 '14 at 15:50
  • @adeneo it is basically a crossword...It is building a string from textboxes. – epascarello May 08 '14 at 15:52
  • @epascarello - There still has to be a better way to do it, even if you have to add multiple values and compare to something? This seems very error prone to me, but I could be mistaken? – adeneo May 08 '14 at 15:54
  • Have you checked they have equal length? – Teemu May 08 '14 at 15:56
  • @adeneo Is there a better way to get the values for each of the input fields without adding them all up? – Sue May 08 '14 at 15:58
  • @KevinB Thanks for the fiddle. It does appear to work there, but I am still unable to get it to work when I tried it. – Sue May 08 '14 at 15:59
  • @espascarello Oh! they are different lengths. The guess is off by one character compared to the answer. – Sue May 08 '14 at 16:00

1 Answers1

4

The problem has nothing to do with the check. The problem is the fact your value you are checking against has hidden characters. However you are getting that string has the issue.

Simple debugging shows the problem

> escape(totalGuess)
"God%27sMasterpieceMatthew15%3A99Psalms129%3A158"
> escape(condensedAnswer)
"God%27sMasterpieceMatthew15%3A99Psalms129%3A158%00"

It has a null character at the end.

Now looking at how you fill in the answer you have an array with numbers

"071,111,100,039,...49,053,056,"

Look at the end we have a trailing comma

when you do a split that means the last index of your array is going to be "" and hence why you get a null.

enter image description here

Remove the trailing comma and it will magically work.

epascarello
  • 204,599
  • 20
  • 195
  • 236
  • @espascarello Thanks so much! I changed it to `var condensedAnswer = answer.replace(new RegExp(" ","g"), "").replace("\0", "");` and that did the trick. – Sue May 08 '14 at 16:05
  • Or you could fix the real problem with the trailing comma ;) – epascarello May 08 '14 at 16:06
  • Oh perhaps trying to remove the trailing comma would be the better solution. I'll look more into it. Thanks once again! – Sue May 08 '14 at 16:09
  • I was able to make the last comma go away. Turns out my PHP form generator has a counter, but the count starts at 0 so by the time it is one less than the answer length, it has looped one too many times. I changed that value to one less than the answer length and that did the trick. – Sue May 08 '14 at 16:20