I want to make a game in JS, where you pick a number between two boundaries. You tell them to the computer, and it tries to guess them.
Choosing the number (or maybe printing it) isn't working.
Here's the code I'm using:
<html>
<head>
<title>Guess the number</title>
</head>
<body>
<script type="text/javascript">
function createBorders()
{
var highnumber=prompt("Choose the maximum number that can be chosen",10);
var lownumber=prompt("Choose the minimum number that can be chosen",0);
var number; //This is the number that the computer chooses.
document.getElementById("intro").innerHTML=""; //Removes the intro
document.getElementById("buttons").innerHTML="<button type=\"button\" onclick=\"tooHigh()\">Lower</button> <button type=\"button\" onclick=\"tooLow()\">Higher</button>"; //Creates the buttons to choose if your number is higher or lower than what JS chose
chooseNumber(); //This is what doesn't work
}
</script>
<script type="text/javascript">
function chooseNumber()
{
do
{
number=Math.round(Math.random()*highnumber); //Choose a random number from 0-1, multiply it by the highest possible number, and round
}
while (number<=lownumber); //Do that once, and then until the chosen number isn't less than the lowest possible number
document.getElementById("num").innerHTML=number; //Maybe this doesn't work. It should write the number into the "num" paragraph
}
</script>
<script type="text/javascript">
function tooHigh()
{
highnumber = number
}
</script>
<script type="text/javascript">
function tooHigh()
{
highnumber = number
}
</script>
<p id="intro">
In this game, you pick a number between two boundaries. Click the "Set Boundaries" button when you are ready.
<!-- Should be overwritten by the JS after the number is chosen -->
</p>
<p id="num"><!-- The number --></p>
<p id="buttons">
<!-- First have the set boundaries button, than replace it with the higher/lower buttons. This works. -->
<button type="button" onclick="createBorders()">Set Boundaries</button>
</p>
</body>
</html>I'm pretty new to JS, just started learning a couple days ago.
Offline
You could just use:
function chooseNumber()
{
number=Math.round((Math.random()*(highnumber-lownumber))+lownumber); //Choose a random number from 0-1, multiply it by the highest possible number, and round
document.getElementById("num").innerHTML=number; //Maybe this doesn't work. It should write the number into the "num" paragraph
}I don't know if that fixes it though...
Offline
Nope, it still doesn't display the number. :S
Offline
Hm? It's supposed to set the variable to a random number, then write it.
Offline
Bump.
Offline
ssss wrote:
scimonster wrote:
Hm? It's supposed to set the variable to a random number, then write it.
Yeah, but i can't find where it prints it to the screen
![]()
function chooseNumber()
{
do
{
number=Math.round(Math.random()*highnumber); //Choose a random number from 0-1, multiply it by the highest possible number, and round
}
while (number<=lownumber); //Do that once, and then until the chosen number isn't less than the lowest possible number
document.getElementById("num").innerHTML=number; //Maybe this doesn't work. It should write the number into the "num" paragraph
}
document.getElementById("num").innerHTML=number; //Maybe this doesn't work. It should write the number into the "num" paragraphOffline