COLOR GUESSING GAME
Hello to all… I have been trying to improve my skllls on JS with all that I have learned so far in this awesome website!.. I found this pretty interesting excercise on the internet… but I don`t know how to make it work properly!
The game is essentially the user has to guess a color that is inside an array of colors that is set randomly, if the user gets wrong he will have another opportinity until he gets right!.. at the end the code will show the user how many guesses he did!.. the problem that I have is the next one:
- The code has to show the user three things: 1 - if the user do not enter a color that is in the array it will show a message “That is not a color in the range!” 2 - if the color that the user introduces is either lower or higher alphabetically it will show a message with hints to the user to guess properly.
with the lower and higher color I have not problem… but I cannot get that error message properly that shows (“That is not a color range!”) … I don’t get what is wrong with my code, I always get “that color is not in the range!” even though I put a color name that IS in the array, so I finish in a infinite loop!.. how can I do to get that message correctly when I run the game?.. you can check the code below!..
<script type="text/javascript">
var target;
var guess_input;
var colors=["aqua", "black", "blue", "cyan", "darkgold", "gold", "grey", "orange", "purple", "yellow"];
var finished = false;
var guesses = 0;
function do_game() {
var random_number = Math.random() * 10;
var random_number_integer = Math.floor(random_number);
target = colors[random_number_integer];
alert(target);
while(!finished) {
guess_input = prompt("I'm thinking of a color between "+ colors.join (", ") +"\n\n Can you guess what?" );
guesses += 1;
finished = check_guess();
}
};
function check_guess() {
if (guess_input !== colors) {
alert("That color is not in the range!");
return false;
}
if (guess_input < target) {
alert("Sorry that is not the color! \n \n Hint: Your color is alfabetically lower");
return false;
}
if (guess_input > target) {
alert("Sorry that is not the color! \n \n Hint: Your color is alphabetically higher");
return false;
}
alert("Your did it! It took you " + guesses + " guesses to take it right!");
return true;
};
</script>