mercredi 17 août 2016

javascript: if statement comparing string value not working

I'm totally new in javascript, and wondering why my lines not working here, here is the situation:

I've written a very simple guessing game where the user must guess the randomly generated word from a list of words.

I'm wondering why the code is not comparing user's input to see whether is from the list or not?

Thank you in advance :)

var target_index;
var guess_input_text;
var finished=false;
var number_of_guesses = 0;
var colors = ["pink", 'olive', 'lime', 'orange', 'yellow', 'purple', 'indo', 'blue', 'gray', 'black'];
var guesses = 0;
var color;

function do_game() {
    var random_number = Math.random()*9;
    var random_number_intiger = Math.floor(random_number);
    target_index = random_number_intiger;
    color = colors[target_index];
    alert(color);

    while (!finished) {
        guess_input_text = prompt('I am thinking of one of these colors.\n\n' + colors + '\n\n What do you think it is?');
        guesses++;
        alert(guess_input_text);
        finished = check_guess();
    }

    function check_guess(){
        if (guess_input_text===color){
            return true;
        }
        if (guess_input_text != color && guess_input_text != colors) {  //why this is not working :(
            alert('Please choose your color among the colors listed below:\n\n' + colors);
            return false;
        }
        if (guess_input_text != color && guess_input_text == colors)  {
            alert('You are a bit off');
            return false;
        }
    }

Aucun commentaire:

Enregistrer un commentaire