This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-07-19 04:18:29

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

My JS isn't working... help, please?

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:

Code:

<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>&nbsp;&nbsp;&nbsp;<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

 

#2 2011-07-19 07:11:40

meew0
Scratcher
Registered: 2010-02-22
Posts: 1000+

Re: My JS isn't working... help, please?

You could just use:

Code:

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...


http://i.imgur.com/mJV3j.pnghttp://i.imgur.com/HwWAX.pnghttp://i.imgur.com/sZ7Ui.pnghttp://i.imgur.com/0y6yh.pnghttp://i.imgur.com/nOC4l.png

Offline

 

#3 2011-07-19 07:17:38

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: My JS isn't working... help, please?

Nope, it still doesn't display the number. :S

Offline

 

#4 2011-07-19 07:30:12

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: My JS isn't working... help, please?

Maybe set a variable, then echo the variable contents?


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#5 2011-07-19 07:42:22

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: My JS isn't working... help, please?

Hm? It's supposed to set the variable to a random number, then write it.

Offline

 

#6 2011-07-20 06:22:28

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: My JS isn't working... help, please?

Bump.

Offline

 

#7 2011-07-20 06:37:30

ssss
Scratcher
Registered: 2007-07-29
Posts: 1000+

Re: My JS isn't working... help, please?

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  hmm


Hey.  It's me SSSS, back from the dead!  smile

Offline

 

#8 2011-07-20 07:00:08

scimonster
Community Moderator
Registered: 2010-06-13
Posts: 1000+

Re: My JS isn't working... help, please?

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  hmm

Code:

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
    }

http://weebly.com/uploads/8/1/3/6/8136364/1727210.png

Code:

document.getElementById("num").innerHTML=number; //Maybe this doesn't work.  It should write the number into the "num" paragraph

Offline

 

Board footer