jeudi 19 septembre 2019

How do I display multiple random integers in JavaScript?

I need to write the following game. The game starts with just two buttons “Start Game” and “Stop Game”.

When the user clicks on the button “Start Game”, a prompt window appears and asks the user to choose an integer between 0 and 9.

Then after that, every one second, four random numbers (between 0 and 9) will be generated and flashing on the screen. The user has to click on the number that was previously chosen to win 1 point.

If the user clicks on the wrong number then 1 point is deducted. Therefore, the game score can become negative if the user clicks on many wrong numbers.

The number that the user chosen is displayed in green. The game score is displayed in red.

The four random numbers must be in red, orange, blue and purple color.

The game score must be updated in real-time as the user playing the game.

Here's what I have so far, I'm new to JavaScript so go easy guys x

<html>
<head>
</head>
<body>


<button onclick="myFunction()">Start Game</button>
<button>Stop Game</button>
<p id="gamedisplay"></p>

<script>
function myFunction() {
var integer = prompt("Please enter an integer between 1 and 9");
if (integer != null) {
  document.getElementById("gamedisplay").innerHTML =
  Math.floor(Math.random() * 10);
  +
  Math.floor(Math.random() * 10);
  +
  Math.floor(Math.random() * 10);
  +
  Math.floor(Math.random() * 10);

  }
}
</script>



</body>
</html>




Aucun commentaire:

Enregistrer un commentaire