lundi 21 août 2017

Triggering an alert if the element that is clicked matches the array item in my random function

I am making a flexbox grid game. Each square has its own id. The point of the game is that the computer will think of a random square(this is a randomized array function where each square id is an array element), and if you clicked the same one the computer thought of, an alert message pops up telling you you picked the right one. Otherwise it tells you its wrong.

I cant figure out how to link the clicked item into the if else statement so i can check to see if it matches up with the random number. This is what I have so far.

<body onload ="pickNumber()">

<div class="container"">
<button id="flex1" onclick="showAnswer()"></button>
<button id="flex2" onclick="showAnswer()"></button>
 ...and so on and so forth
</div>

</body>




function pickNumber() {

var grid = ["flex1", "flex2", "flex3", "flex4", "flex5", "flex6", "flex7", 
"flex8", "flex9"];
var randomNumber = grid[Math.floor(Math.random() * values.length)];

}

function showAnswer() {

if (id.target == randomNumber) {
alert("Correct!");
} else {
alert("Sorry, try again!");

}

}




Aucun commentaire:

Enregistrer un commentaire