1

I am trying to compare each element of an array with the user input to generate a message if the user's input doesn't match with any element of the array. The code I use to do is below.

var guess_input;
var finished = false;
var colors = ["Aqua", "BurlyWood", "Cornsilk", "DarkGrey", "DarkRed", "Indigo", "LightGrey"];

while (!finished) {
          guess_input = prompt("I'm thinking of one of these colors:\n\n" + "Aqua, BurlyWood, Cornsilk, DarkGrey, DarkRed, Indigo, LightGrey" + "\n\nWhat is the color I'm thinking of?");
          finished = check_guess();
          }
      }
      function check_guess() {

       if (guess_input !=  colors[0] || guess_input != colors[1] || guess_input != colors[2] || guess_input != colors[3])  {
          alert("Sorry, I don't recognize that color!\n\n" + "Please try again.");
          return false;
        }
}

the problem with this code is that if I only select one element from array, it works perfectly fine. but when I use 'OR' operator, it doesn't work. Is there any better way to do this? I am new with java script.

Thanks!

3
  • Why are you declaring the function inside your loop? Declare it outside, so you don't iterate over function creation everytime Commented Sep 4, 2016 at 19:28
  • Using array.prototype.find() would be more efficent Commented Sep 4, 2016 at 19:38
  • @Jonasw, this would only work, if all elemebt are to check, but not only a part of it, like element 0 ... 2. Commented Sep 4, 2016 at 19:50

2 Answers 2

1

You could use the logical AND operator &&, because you need a check for all colors to be checked.

if (guess_input != colors[0] && guess_input != colors[1] && guess_input != colors[2] && guess_input != colors[3])  {
     alert("Sorry, I don't recognize that color!\n\n" + "Please try again.");
    return false;
}

For a working code, you need to return true for a found color, as well.

var guess_input;
var finished = false;
var colors = ["Aqua", "BurlyWood", "Cornsilk", "DarkGrey", "DarkRed", "Indigo", "LightGrey"];

while (!finished) {
    guess_input = prompt("I'm thinking of one of these colors:\n\n" + "Aqua, BurlyWood, Cornsilk, DarkGrey, DarkRed, Indigo, LightGrey" + "\n\nWhat is the color I'm thinking of?");
    finished = check_guess();
}

function check_guess() {
    if (guess_input != colors[0] && guess_input != colors[1] && guess_input != colors[2] && guess_input != colors[3]) {
        alert("Sorry, I don't recognize that color!\n\n" + "Please try again.");
        return false;
    }
    return true; // necessary, otherwise the function returns undefined, which is a falsy value
}

Sign up to request clarification or add additional context in comments.

Comments

0

You need to compare guess_input against the items in the colours array. This is a perfect job for the Array.prototype.some() method, which returns true or false depending on a condition defined in its callback. For example...

var test_guess = colours.some(function(color) {
    /* 'color' = each item in 'colours' */
    return color === guess_input;
});

if (test_guess) {
   /* correct .....  */
} else {
   /* incorrect .... */
}

Here .some() begins to iterate through all the items in the colours array until the condition returns true. The variable test_guess will be true if the guess matches a colour, otherwise false.

See: Array.prototype.some() @ MDN

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.