/    Sign up×
Community /Pin to ProfileBookmark

Javascript Problem

Can someone help? I am supposed to write a page that has write a small web page that uses JavaScript functions to play a number guessing game. The page should have two buttons, one labeled “Play”, and the other labeled “Guess” and an input control for the user to type a number.

When the user presses the “Play” button, a function should get and store a random number between 1 and 100. The user will try to guess this number. You may want to display this number on the status bar when testing the page. So write a line of code to do that, but comment out the line before you turn in the assignment. (That means, leave the line in, but make it a programming comment so that it does not run.)

When the user presses the “Guess” button, you should use a function to compare the number they entered (via the input control) to the random number picked earlier. The page should display one of three messages as appropriate. The messages are: “Too Low”, “Too High”, or “You Guessed It!”.

I’ve written the page and it works except for the IF function. Can someone take a look at it and see if they can figure out where I went wrong? Thanks ahead of time. ?

<?xml version=”1.0″ encoding=”iso-8859-1″?>
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“>
<html xmlns=”http://www.w3.org/1999/xhtml“>
<head>
<title>Untitled Document</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />

<script language=”JavaScript”>

var x = (Math.floor(Math.random()* (100 + 1)));
var y;
function changeStatusBar(){

window.status = x
}

function calculate(){
if (x < y)

{
document.write(“<b>Too High</b>”);
}
else if (x > y)
{
document.write(“<b>Too Low</b>”);
}
else
{
document.write(“<b>You Guessed It</b>”);
}

}
</script>
</head>

<body>
<form>
<input type=”button”
value=” Play “
onClick=”changeStatusBar();”>
</form>

<form>
<input type=”text” name=”y” size=”15″>
</form>

<form>
<input type=”button”
value=” Guess “
onClick=”calculate()”>
</form>

</body>
</html>

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERSep 19.2007 — I think this may be closer to what you want.

See if you can spot the differences.
[code=php]
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Guessing Game</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<script language="JavaScript">
var x = 0;
function changeStatusBar() {
x = (Math.floor(Math.random()* (100 + 1)));
window.status = x;
}

function calculate() {
var y = document.getElementById('y').value;
// alert('x:'+x+' y:'+y); // for testing purposes
if (x < y) { alert("Too High");
} else { if (x > y) { alert("Too Low");
} else { alert("You Guessed It"); }
}
}
</script>

</head>
<body>

<form>
<input type="button" value=" Initialize Game " onClick="changeStatusBar();">
<input type="text" name="y" id="y" value='' size="15">
<input type="button" value=" Guess " onClick="calculate()">
</form>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@sparkyjoe34authorSep 19.2007 — Sweet! Ok I see the differences. Can you explain what you did so that I will understand.
Copy linkTweet thisAlerts:
@JMRKERSep 19.2007 — 
  • 1. Your 'changeStatusBar()' function doesn't really change anything. I just used it to initialize the computer generated number to guess (x)


  • 2. You cannot do a document.write after the page has been displayed, so I changed the actions to 'alert'. You could use some <div> or <span> area and do a 'innerHTML="message"', but the alerts are more dynamic.


  • 3. You don't need multiple <form> tags, just adds to complexity.


  • 4. Used the ID tag to identify the input value as the NAME tag will not supply what you want as directly as the 'getElementByID()' function.


  • Hope that helps.

    Good luck.
    Copy linkTweet thisAlerts:
    @sparkyjoe34authorSep 19.2007 — Ok I got that but the only thing I don't understand is

    var y = document.getElementById('y').value;

    Also why wont it display the number in the status bar in Mozilla?
    Copy linkTweet thisAlerts:
    @JMRKERSep 19.2007 — document.getElementById('y').value

    gets the current user guess for comparison to 'x' value generated in initialization.

    I don't know enough about the status bar to answer this as I don't tend to ever change it from the default display. You might try a google search on the use and modification of this region of the screen or try a search of this forum where it may have been answered before. Besides, I'm not sure why you want to display the 'x' value if you want the user to guess it, but since it is your game and your script you can do whatever you want in the game!

    Also note that you will not get a good game if the user enters a non-numeric value into the guess section. If it is important to you, consider adding validation checks on the input number guessed.
    ×

    Success!

    Help @sparkyjoe34 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.24,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...