I am new to JavaScript.
I have been creating a game- which includes the number of goes a person has had to guess a random number.
I have created the button however I am having problems trying to restart the game - making a new random number to be generated and the number of goes to return to 0
var randomno =Math.floor(Math.random() * 100 ) + 1;
let restart = document.getElementById("restart");
function restartGame(){
num();
}
}
restartGame();
This is only a small snippet of my code.
You want to assign a click handler to your restart element which is missing from the OP code snippet... https://developer.mozilla.org/en-US/docs/Web/Events/click
document.getElementById("restart").addEventListener("click", function() {
// restart the game
});
A quick fix which may or not be optimal for you is to simply refresh the page. Use something like 'location.reload();' to do this.
As I could understand you only need to re-assign some variables.
To do that, just put the variable name followed by the equals sign and the new value you want it.
var randomno = Math.floor(Math.random() * 100 ) + 1;
randomno = Math.floor(Math.random() * 100 ) + 1;
The second line the variable is being re-assigned.
If you also have a problem of binding an action for your button to call a javascript event(in your case restart the game), please read this article: https://developer.mozilla.org/en-US/docs/Web/Events/click
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.