var colors = ["blue", "cyan", "gold", "grey", "orange", "red"];
var gussess = 0;
var color_choosed;
var color_guessed;
function game() {
color_choosed = "grey";
color_guessed = prompt("I'm thinking of one of these colors");
if (colors.indexOf(color_guessed) > colors.indexOf(color_choosed))
alert("The color choosed is after my color");
else if (colors.indexOf(color_guessed) < colors.indexOf(color_choosed))
alert("The coloris befor my color");
else if (colors.indexOf(color_guessed) == colors.indexOf(color_choosed))
alert("congrats you are right my color is " + color_guessed);
else if (color_guessed not in colors)
alert("The color is not in the list");
}
I want to write an if
statement to check if the user has put a color not in the list .. do something ? to give him an alert that the color is not in the list
var colors = ["blue", "cyan", "gold", "grey", "orange", "red"];
var gussess = 0;
var color_choosed;
var color_guessed;
function game() {
color_choosed = "grey";
color_guessed = prompt("I'm thinking of one of these colors");
if (colors.indexOf(color_guessed) > colors.indexOf(color_choosed))
alert("The color choosed is after my color");
else if (colors.indexOf(color_guessed) < colors.indexOf(color_choosed))
alert("The coloris befor my color");
else if (colors.indexOf(color_guessed) == colors.indexOf(color_choosed))
alert("congrats you are right my color is " + color_guessed);
else if (color_guessed not in colors)
alert("The color is not in the list");
}
I want to write an if
statement to check if the user has put a color not in the list .. do something ? to give him an alert that the color is not in the list
-
2
colors.indexOf(input) === -1
– Rajesh Commented May 16, 2017 at 11:36 - else if ( colors.indexOf(color_guessed) === -1 ) alert("The color is is not in the list"); dosn't work with me also – tareq Commented May 16, 2017 at 11:41
-
Because of this I guess:
if (colors.indexOf(color_guessed) < colors.indexOf(color_choosed))
. Here it should be like-1 < 3
– Rajesh Commented May 16, 2017 at 11:43 -
1
There is also the more semantic includes:
if (colors.includes(color_guessed)) ...
. Might not be available in some browsers though, and has slightly different behaviour (which might be preferred). – RobG Commented May 16, 2017 at 12:24
4 Answers
Reset to default 3I want to write an if statement to check if the user has put a color not in the list ..
Here is the if statement you can use to check if the user's input is in your array:
if(colors.indexOf(color_guessed) === -1) { // code here }
Important notice: indexOf() returns -1
if the value is not present in the array. This will make some additional side effect in your code.
In order to fix this, you shall move your last if statement to the beginning, making it the first one, like so:
function game() {
color_choosed = "grey";
color_guessed = prompt("I'm thinking of one of these colors");
if(colors.indexOf(color_guessed) === -1) // color not found in array
alert("The color is not in the list");
else if (colors.indexOf(color_guessed) > colors.indexOf(color_choosed)) // color is after
alert("The color choosed is after my color");
else if (colors.indexOf(color_guessed) < colors.indexOf(color_choosed)) // color is before
alert("The coloris befor my color");
else if (color_guessed === color_choosed) // colors match!
alert("congrats you are right my color is " + color_guessed);
}
colors.indexOf(color_choosed)
works fine on arrays but are case sensitive... if you want to ignoreCase you should either care everything is lowerCase in your array and check for color_choosed.toLowerCase()
indexOf returns -1 if the element is not found in array
for performance and logic reasons you should check if its in array at first
Thnx every body i've [solved] the code .. i used indexOf === -1
var colors=["blue","cyan","gold","grey","orange","red"];
var gussess = 0 ;
var color_choosed;
var color_guessed;
function game(){
color_choosed = "grey";
color_guessed = prompt("I'm thinking of one of these colors");
if ( colors.indexOf(color_guessed) > colors.indexOf(color_choosed))
alert("The color choosed is after my color");
else if ( colors.indexOf(color_guessed) < colors.indexOf(color_choosed)&colors.indexOf(color_guessed) >0)
alert("The coloris befor my color");
else if ( colors.indexOf(color_guessed) == colors.indexOf(color_choosed))
alert("congrats you are right my color is " + color_guessed);
else if ( colors.indexOf(color_guessed) === -1 )
alert("The color is is not in the list");
}
the includes
function, that has been available since ES2016, is a much cleaner way of solving this, if you dont really care about giving the 'is after' or 'is before' prompts:
if (colors.includes(color_guessed))