I am making a game that says a random number to click and then it tells you when it is right or wrong. I want the user to decide how many rounds they want and input it into the program.
//takes the number of rounds and puts it into a variable so it shows the amount of questions you want
var numberOfRounds = parseInt( document.getElementById( "numberOfRounds" ).value ) //puts the input numberOfRounds in a variable.
console.log(numberOfRounds);
randomNumber = ( Math.floor(Math.random() * 6));
console.log(randomNumber);
buttonColors();
<font size="6"><center><strong><p id="startScreen">Welcome to the Random Color Game.</p></strong></center></font>
<font size="4"><center><p id="startScreen2">How many many rounds of the game would you like?</p>
<form id="numberOfRounds"><p id="startScreen3">I would like <input id="numberOfRounds" type="number" min="1" max="20" name="numberOfRounds" style = "width:100px; height:50px;"> rounds.</p>
<p id="startScreen5">To start playing the game, push begin.</p></center></font>
Your code is more-or-less correct - document.getElementById("numberOfRounds").value will get the value from the input element, assuming that's the only element with id="numberOfRounds" (ids should be unique in the document).
I've refactored your code a bit and added a "Begin" button, which gets the value from the <input> element and displays it to the user.
//puts the input numberOfRounds in a variable.
function getNumberOfRounds() {
var setNumberOfRoundsElement = document.getElementById("setNumberOfRounds");
var myNumberOfRoundsElement = document.getElementById("myNumberOfRounds");
var numberOfRounds = parseInt(setNumberOfRoundsElement.value);
myNumberOfRoundsElement.innerHTML = "You chose " + numberOfRounds + " rounds!";
}
body {
text-align: center;
}
<h1>Welcome to the Random Color Game.</h1>
<div id="myNumberOfRounds">
How many many rounds of the game would you like?
<p>
I would like
<input id="setNumberOfRounds" type="number" min="1" max="20" name="numberOfRounds">
rounds.
</p>
To start playing the game, push begin.
<p>
<button onclick="getNumberOfRounds()">Begin</button>
</p>
</div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With