When trying to display the index of the first element (violet) using an alert, I am getting -1 instead of the expected result. This unexpected outcome is hindering my progress in coding. As a newcomer to JavaScript, I'm facing this issue and seeking assistance to move forward. I hope someone can provide a solution promptly.
var colors = ["violet", "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 am thinking of a color " +
"violet,indigo,blue,green,yellow,orange,red" +
"What is the 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;
}