javascript - indexOf() showing unexpected value -


while doing alert index of first element i.e, violet being shown -1.but unexpected getting correct result.hence not able expected result me can go further in coding.actually m new javascript facing such problem.hope answerme can.

var colors = ["voilet", "indigo", "blue", "green", "yellow", "orange", "red"];  var target;  var target_index;  var guess_input;  var finished = false;    function do_game() {    var random_number = math.random() * 7;    var random_number_integer = math.floor(random_number);    var target_index = random_number_integer;    target = colors[target_index];    alert(target);    while (!finished) {        var guess_input = prompt("i thinking of color " +        "violet,indigo,blue,green,yellow,orange,red" +        "what color?");      alert(colors.indexof(guess_input));      finished = check_guess();    }  }    function check_guess() {      if (colors.indexof(guess_input) < 0) {      alert('not present');      return false;    }    if (guess_input > target) {      alert('you gave large');      return false;    }    if (guess_input < target) {      alert('you gave small');      return false;    }    return true;  }

you said guess_input violet , returns -1

this normal behavior because violet not present in array

["voilet","indigo","blue","green","yellow","orange","red"]; 

there typo error : wrote voilet instead of violet


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -