-2

I am totally clueless as to why this code isn't running. It is supposed to be a color guessing game. It ran once and then it just shows blank screen.

It is supposed to pick a color at random from an array or colors and then it asks for user input and compares it with the chosen color and outputs accordingly.

<!doctype html>
<html>
<head>
    <title>JavaScript Guessing Game</title>
    <script>
        var target;            
        var guess_input_text;   
        var guess_input;      
        var finished = false;   
        var guesses = 0;
        var colors = ["blue","cyan","gold","grey","green","magenta", "orange", "red","white","yellow"];
        var scolors = colors.sort();

        function do_game() {
            var random_number = Math.random() * 10;
            var random_number_integer = Math.floor(random_number);
            target_index = random_number_integer;
            target = colors[target_index];

            while (!finished) {
                guess_input = (prompt("I am thinking of one of these colors " + colors.join(", ") + ".\n\n What color am I thinking of?")).toLowerCase();
                guesses += 1;   
                finished = check_guess();
            }
        }

        function check_guess() {
            if (!(colors.indexOf(guess_input) >= 0)) {
                alert("Sorry I don't recognize your color\n\n" +
                      "Please try again.");
                return false;
            }
            if (scolors.indexOf(guess_input) > (scolors.indexOf(target))) {
                alert("Sorry, your guess is not correct\n\n" + "Hint: Your color is alphabetically higher than mine\n\n"+"Please try again.");
                return false;
            }
            if (scolors.indexOf(guess_input) < (scolors.indexOf(target))) {
                alert("Sorry, your guess is not correct\n\n" + "Hint: Your color is alphabetically lower than mine\n\n"+"Please try again.");
                return false;
            }
            else{
            alert("Congratulations! You've guessed the color!+ 
                  ".\n\nIt took you " + guesses + 
                  " to finish the game!\n\n You can see the color in the background");
            return true;   
        }
        }
    </script>
   
</head>
<body onload="do_game()">

</body>
</html>

1
  • 1
    Load the page into your browser. Press the F12 key and then select the console tab. Press F5 to rerun your code. If there are any errors then they will appear in the console tab. Commented Mar 26, 2016 at 11:35

2 Answers 2

2

You are missing double quote:

alert("Congratulations! You've guessed the color!"+ 
//-----------------------------------------------^

I would recommend you to open console in your browser (in chrome ctrl + shift + i), run your code and look for errors first next time.

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

1 Comment

Thanks @madox2, will definitely make use of your advice.
-1

If you use a JavaScript IDE, e.g. atom,webstorm,and sublime. you can easily find the errors.

enter image description here

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.