-2

I'm practicing html, css and javascript, and I have been trying to run this code but ended up failing to do so. I couldn't figure out where I went wrong. All your efforts are appreciated.

<!doctype html>
<html>

<head>
  <title>Example of a function</title>
  <script>
    var colors = {
      "blue", "green", "red"
    };
    var guessColor;
    var guesses = 0;
    var finished = false;
    var target_index;
    var targetColor;

    function do_game() {
      var randomNo = Math.random() * 3;
      var randomNo_integer = Math.floor(randomNo);
      target_index = randomNo_integer;

      targetColor = colors[target_index];

      while (!finished) {
        guessColor = prompt("I am thinking of one of these colors:\n\n" +
          "Blue, Green, Red\n\n" +
          "What color am I thinking of?");

        if (guessColor.toUpperCase() == targetColor.toUpperCase())
          finished = true;
      }
      document.body.style.backgroundColor = guessColor.toLowerCase();
    }
  </script>
</head>

<body onload="do_game()">
</body>

</html>
mplungjan
  • 169,008
  • 28
  • 173
  • 236
Asaad Maher
  • 101
  • 1
  • 1
  • 3

2 Answers2

0

You should change what you assign to colors. Currently you assign an object and not an array as it is expected.

Then you could remove the onload="do_game()" form the body element and add an event listener, which would listen to the window load event

Why we wrapped all code in a function called func?

We did so, in order to not pollute the global namespace .

function func(){
  var colors = ["blue", "green", "red"];
  var guessColor;
  var guesses = 0;
  var finished = false;
  var target_index;
  var targetColor;
  function do_game(){
    var randomNo = Math.random() * 3;
    var randomNo_integer = Math.floor(randomNo);
    target_index = randomNo_integer;

    targetColor = colors[target_index];

    while(!finished){
      guessColor = prompt("I am thinking of one of these colors:\n\n" +
                          "Blue, Green, Red\n\n" +
                          "What color am I thinking of?");

      if( guessColor.toUpperCase() == targetColor.toUpperCase())
        finished = true;
    }
    document.body.style.backgroundColor = guessColor.toLowerCase();
  }

  // Here we call our function that will trigger the start of the game.
  // This function would be called when the DOMContentLoaded event is fired.
  do_game();
}

window.addEventListener("load", func);
<h1>Game Title</h1>
Community
  • 1
  • 1
Christos
  • 53,228
  • 8
  • 76
  • 108
  • It is still a typo question. `window.onload` is more compatible - and now complicate things with nested functions – mplungjan Jan 28 '17 at 11:38
0

Your Array declaration, it should be

var colors = [
  "blue", "green", "red"
];

<!doctype html>
<html>

<head>
  <title>Example of a function</title>
  <script>
    var colors = [
      "blue", "green", "red"
    ];
    var guessColor;
    var guesses = 0;
    var finished = false;
    var target_index;
    var targetColor;

    function do_game() {
      var randomNo = Math.random() * 3;
      var randomNo_integer = Math.floor(randomNo);
      target_index = randomNo_integer;

      targetColor = colors[target_index];

      while (!finished) {
        guessColor = prompt("I am thinking of one of these colors:\n\n" +
          "Blue, Green, Red\n\n" +
          "What color am I thinking of?");

        if (guessColor.toUpperCase() == targetColor.toUpperCase())
          finished = true;
      }
      document.body.style.backgroundColor = guessColor.toLowerCase();
    }
  </script>
</head>

<body onload="do_game()">
</body>

</html>
Vaibhav Kumar
  • 544
  • 5
  • 17