Question:

Whats wrong with this java script code?

by  |  earlier

0 LIKES UnLike

<html>

<head>

<script type="text/javascript">

function gameThing() {

var num = Math.round(Math.random()*1001)

var counter= document.form.b.value

var text= document.form.a.value

if (text==num) {

document.form.c.value='You got it! How did you know that number? You

Guessed the number through' ' ' counter ' ' 'guesses'

}

if (text<num) {

document.form.c.value='That is too low!'

}

if (text>num) {

document.form.c.value='That is too high!'

}

}

function refreshPage() {

history.go()

}

function counTer() {

document.form.b.value ;

}

</script>

</head>

<body>

<br/><br/><br/><br/><br/><br/><br/><br...

<div align="center"><p>Ok input a number then click submit, if it is

too high the message will say too high, same with too low, keep trying

until oyu get it!</p>

<br/><br/><p>Click Refresh to get a new number.</p><br/><br/>

<form name="form">

<input type="text" name="c" size=100 />

<br/><br/><br/>

<input type="text" name="a" size=4 />

<input type="button" value="submit" onClick="gameThing(); counTer();" />

<input type="button" value="Refresh" onClick="refreshPage()" />

<br/><br/><input type="text" value=0 name="b" size=4 />

</div>

</form>

</body>

</html>

 Tags:

   Report

1 ANSWERS


  1. The logic of your game is incorrect:

    When the Submit button is pressed, your gameThing() function computes a new random number, thus ensuring that a player will never have more than one guess.

    Solution: you need to separate the new number generation from the comparing function:

    var num;

    function newNumber() {

      num = Math.round(Math.random()*1001);

    }

    and omit the Math statement from function gameThing()

    Include newNumber() in the body tag to start with a number

    &lt;body onload=&#039;newNumber();&#039;&gt;

    And make it the action of the Refresh button:

    &lt;input type=&quot;button&quot; value=&quot;Refresh&quot; onClick=&quot;newNumber()&quot; /&gt;

    Also, you are not incrementing the variable counter, but you can figure that out.

Question Stats

Latest activity: earlier.
This question has 1 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.