tareq
tareq

Reputation: 21

How can I check if a string (is not) exist in an array?

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

Upvotes: 2

Views: 7387

Answers (4)

Lea de Groot
Lea de Groot

Reputation: 307

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))

Upvotes: 0

tareq
tareq

Reputation: 21

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");

        }

Upvotes: 0

Marcin Pevik
Marcin Pevik

Reputation: 173

I 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);
}

Upvotes: 3

Kapsonfire
Kapsonfire

Reputation: 1033

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

Upvotes: 0

Related Questions