0

can you help me with this code. I wanna to create guessing game, where user should put a right color in prompt.

Computer guesses - a color, and user should give a right answer - which color is correct. I try to create a right code for it but is does not work correctly. Maybe the problem with variables or with indexOf, or smth else.... Thank you in advanced

    var target;
    var guess_input;
    var finished = false;
    var colors;  
    var presentOrNot;

    colors = ["aqua", "black", "white"];

    function do_game() {
        var random_color = colors[Math.floor(Math.random() * colors.length)];
        target = random_color; 
        alert (target);

        while (!finished) {
            guess_input = prompt("I am thinking of one of these colors:\n\n" + colors + "\n\n What color am I thinking of?");
            guesses += 1;
            finished = check_guess ();
        }
    }
    function check_guess () {
        presentOrNot = colors.indexOf(guess_input);
        if (presentOrNot == target) {
            alert ("It is my random color");
            return true;
        }
        else {
            alert("It isn't my random color");
            return false;
        }
     }
jsDev
  • 136
  • 2
  • 12

3 Answers3

1

indexOf returns the index (0,1,2..), but the target is the actual color (aqua, black, ..). Try this instead

function check_guess () {
    if (guess_input.toLowerCase() === target) {
        alert ("It is my random color");
        return true;
    }
    else {
        alert("It isn't my random color");
        return false;
    }
}

alternatively, this should work too

function check_guess () {
    presentOrNot = colors.indexOf(guess_input);
    if (presentOrNot === colors.indexOf(target)) {
        alert ("It is my random color");
        return true;
    }
    else {
        alert("It isn't my random color");
        return false;
    }
}

I changed == to === which is usually what you want in javascript.

Community
  • 1
  • 1
Matthias
  • 3,160
  • 2
  • 24
  • 38
0

The prompt command returns a string value.

In your while loop, where you declare guess_input, put all your prompt in a Number() function.

D. Belaev
  • 11
  • 1
0

I edited and corrected your code. Try this

var target;
    var guess_input;
    var finished = false;
    var colors;  
    var presentOrNot;
    var guesses = 0; /*initialized variable*/

    colors = ["aqua", "black", "white"];

    function do_game() {
        var random_color = colors[Math.floor(Math.random() * colors.length)];
        target = random_color; 
        alert (target);

        while (!finished) {
            guess_input = prompt("I am thinking of one of these colors:\n\n" + colors + "\n\n What color am I thinking of?");
            guesses += 1;
            finished = check_guess ();
            
            if(guesses === 3) {
             break;
            }
        }
        
    }
    function check_guess () {
        presentOrNot = colors.indexOf(guess_input);
        if (colors[presentOrNot] === target) {
            alert ("It is my random color");
            return true;
        }
        else {
            alert("It isn't my random color");
            return false;
        }
     }   
     //start the game---
     do_game();
     
     
ScrapCode
  • 2,109
  • 5
  • 24
  • 44