0

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', 'indigo', '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){ //this works
        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){ //this is not working too
        alert('You are a bit off');
        return false;
      }
}
1
  • You can't compare a string guess_input_text with an array colors. It will always be unequal. Commented Aug 18, 2016 at 3:22

2 Answers 2

2

I saw you lost '}' in function do_game()

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();
  }//Add here
}

Change:

if (guess_input_text!=color && guess_input_text!=colors)

if (guess_input_text!=color && guess_input_text==colors)

to:

if (guess_input_text!=color && colors.indexOf(guess_input_text) < 0)

if (guess_input_text!=color && colors.indexOf(guess_input_text) >= 0)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but that wasn't the problem !
0

The main problem I see is that you are comparing a string value to an array here guess_input_text!=colors. This will always evaluate to true since the string entered by the user will not ever be equal to the array of colors. This means that any time a user enters a color that doesn't match the random color, they will get "Please choose your color among the colors listed below" even if they have chosen a color that is in the array.

What you need to do is check if the string entered by the user exists in the array. One way you can do that is to change

if (guess_input_text!=color && guess_input_text!=colors){  //why this is not working :(

to

if (guess_input_text!=color && colors.indexOf(guess_input_text)>=0){  //this will work if your browser/platform supports it :)

If indexOf isn't available to you, there are other ways you can do this.

Your last conditional is more or less unnecessary, because you already know that the color doesn't equal the random color and it is in the array. So it could just be omitted and you could always issue your final alert command at that point.

2 Comments

Thanks @Wake for your clear explanation, after I replaced your line browser doesn't run!
@Ardavan, it may be that indexOf isn't supported in your browser, or that there is some other issue. See this post for more details on indexOf and other ways you can check if your guess_input_text string is contained within your colors array: stackoverflow.com/questions/237104/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.