0

Simple question.. Not sure why this is happening...

Question: the "else" runs even when the if statement is true..

In this case...


The City entered is "Los Angelos" which is an array element of var "theAnswers" Meaning the alert should run and the program should finish...

What happens instead..


The program displays the else alert.. then displays the proper alert(if). See for yourself...

var theAnswers = ["New York City", "Chicago", "Los Angelos", "Cleveland", "Ohio", "Seattle", "Boston", "Miami", "Philadelphia"];

var userAnswer = prompt("I am thinking of a Major City.. guess which one it is?", "Los Angelos");
userAnswer = userAnswer.toLowerCase();

for (var i = 0; i < theAnswers.length; i++) {
    if (userAnswer === theAnswers[i].toLowerCase()) {
        alert("Good, your guess is correct. You get one point!");
        break;
    }

    else{
        alert("Hmm... not quite the City I was looking for... Better luck next time!");

    }
}
3
  • can you create fiddle for the same Commented May 24, 2015 at 8:55
  • you can use in array indexoff Commented May 24, 2015 at 8:56
  • @exexzian sure can.. excuse me for not doing so earlier. I thought you could run it directly from SO. jsfiddle.net/6rctjxot/#&togetherjs=cBb3GxLTAP Commented May 24, 2015 at 16:16

3 Answers 3

2

That's because you are showing the message in the else for each check that you make. It will show the message for each city that doesn't match. Look through all the cities first, then show the result once:

var found = false;
for (var i = 0; i < theAnswers.length; i++) {
    if (userAnswer === theAnswers[i].toLowerCase()) {
        found = true;
        break;
    }
}
if (found) {
    alert("Good, your guess is correct. You get one point!");
}else{
    alert("Hmm... not quite the City I was looking for... Better luck next time!");
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! This worked well. The issue i was having with the boolean was nesting it improperly when I attempted to use one.
1

Remove the for loop. You don't have to loop through the array to know if answer exist in it , Just know the position of it will suffice. Try this:

var theAnswers = ["New York City", "Chicago", "Los Angelos", "Cleveland", "Ohio", "Seattle", "Boston", "Miami", "Philadelphia"];
var userAnswer = prompt("I am thinking of a Major City.. guess which one it is?", "Los Angelos");

if (theAnswers.indexOf(userAnswer)>=0) {
    alert("Good, your guess is correct. You get one point!");
}else{
    alert("Hmm... not quite the City I was looking for... Better luck next time!");
}

Fiddle

2 Comments

indexOf would return 0 for New York City. You need to use >=0 for the test. Also the original code checks for a lowercase version.
Thank you for this one! I wondered how I could search all the elements without looping... this is it.
0

It doesn't actually execute the "else" when the condition is true.

if (userAnswer === theAnswers[i].toLowerCase())

The first time this condition is evaluated, i is 0, so theAnswers[i] is "New York City".

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.