最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论
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

Share Improve this question edited May 16, 2017 at 11:36 George 6,7492 gold badges30 silver badges36 bronze badges asked May 16, 2017 at 11:34 tareqtareq 211 silver badge5 bronze badges 4
  • 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
Add a ment  | 

4 Answers 4

Reset to default 3

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

发布评论

评论列表(0)

  1. 暂无评论