/    Sign up×
Community /Pin to ProfileBookmark

New here, really need some help with this javascript code

I’m supposed to write the code for this:

[FONT=”Arial Black”][SIZE=”2″]Create a new document named guess.html. Write a number guessing game that
prompts the visitor for a number and keeps prompting until the guess matches the
target. Keep a count of how many guesses the visitor makes. Display a message after
each guess as follows:
a. If the guess is within three, either higher or lower, than the target number, display
the message “You’re Red Hot!”
b. If the guess is more than three away, but less than 10 away from the target
number, say “You’re getting warm.”
c. If the guess is more than 10 away but less than 20, say “You’re getting cold.”
d. If the guess is more than 20 away, say “You’re Ice Cold!”
e. If the guess is correct, say “Success! You Win!” in an alert box. On the second line
of the alert box, display the appropriate message. If the number of guesses was
i. less than five: “You did an awesome job, it only took you ___ tries to guess
the number!”
ii. less than or equal to 15, but more than five: “Goodness. It took you _
__
tries
to guess the target number.”
iii. greater than 15: “Yikes! It took you ___ tries to guess the target number.”[/SIZE]
[/FONT]

to post a comment
JavaScript

14 Comments(s)

Copy linkTweet thisAlerts:
@thraddashNov 12.2009 — Have you attempted this task yet? It seems pretty straight forward.
Copy linkTweet thisAlerts:
@prissanna06authorNov 12.2009 — i may seem straight forward to someone who knows javascript very well. I, on the other hand, don't have much experience with it at all. If I knew how to do it, I would, I just need a starting point and maybe I can get it from there.
Copy linkTweet thisAlerts:
@FreejackNov 12.2009 — I think the issue is that this looks like a class assignment. Generally folks don't want to do your work for you. If you're having a problem with an assignment, you should discuss it with your teacher.

In any case, most folks won't help until you exert some effort in figuring how how to do the task. Just posting your requirements and expecting a response is more how you'd contract work and there'd be discussion about payment and expectations on when you want the results.

Good luck.

Carl
Copy linkTweet thisAlerts:
@prissanna06authorNov 12.2009 — I not asking someone to do it for me, and I did discuss it with the teacher. I just need more of an explanation that what he gave. All he did was tell us how to solve the problem in real life, not how to do it in javascript. Like I said before just a starting point and can probably do it from there. I have most of it figured out, it's just getting all together to make it work.
Copy linkTweet thisAlerts:
@thraddashNov 12.2009 — Freejack is right, normall an attempt is expected before answers are given.

However, tonight I am feeling bored so:

[code=html]<!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" xml:lang="en" lang="en">
<head>
<title>bionoid - Guessing Game</title>
</head>
<body>

<script type="text/javascript">
var random_number = parseInt(Math.random() * 100, 10);
var guesses = 0;
var last_guess = 0;
var message = "";

while (last_guess != random_number) {
guesses++;
if (last_guess == 0) {
message = "What number am I thinking of?";
} else if (last_guess >= (random_number - 3) && last_guess <= (random_number + 3)) {
message = "You're Red Hot!";
} else if (last_guess >= (random_number - 10) && last_guess <= (random_number + 10)) {
message = "You're getting warm.";
} else if (last_guess >= (random_number - 20) && last_guess <= (random_number + 20)) {
message = "You're getting cold.";
} else {
message = "You're Ice Cold!";
}
last_guess = parseInt("0" + prompt(message + " (" + random_number + ")", last_guess), 10);
}

message = "Success! You Win!n";
if (guesses < 5) {
message += "You did an awesome job, it only took you " + guesses + " tries to guess the number!";
} else if (guesses <= 15) {
message += "Goodness. It took you " + guesses + " tries to guess the target number.";
} else {
message += "Yikes! It took you " + guesses + " tries to guess the target number.";
}
alert(message);
</script>

</body>
</html>[/code]


It should be easy enough to understand (I guess).
Copy linkTweet thisAlerts:
@FreejackNov 12.2009 — What sort of starting point are you looking for? A reference to a suggested book? I like O'Reilly published books specifically so I'd recommended the Learning Javascript book. From a general on-line help standpoint, the http://www.w3schools.com/ website has lots of help for all sorts of web stuff including Javascript.

And since you indicate that you've done a bunch of work already, post up what you've figured out and we'll be happy to help debug the problem with you.

Carl
Copy linkTweet thisAlerts:
@prissanna06authorNov 12.2009 — as I said before the teacher only showed us how to solve the porblem in real life. This is an internet class, don't have a teacher to sit down with every week and show us what to do, and the book we have just gives examples of scripts and expects us to go by them. I'm not genius, but i'm not stupid either, as long as someone can show me how to do it, then I shouldn't have a problem. this is what he gave us to do.

this is the formula for us to start with

vartarget =Math.floor(Math.random(*10000+1)

We first have to get the target number, then we have to get the used to guess what it is.

we are using loops that have to be

initialized

testing the conditions

then updated each time

to initialize - we have to get the user to guess a number

for each guess, we have to tell if it's right or wrong

If it wrong >=3 - they are red hot

>=10 - they are warmer

>=20 - getting cold

if else >20 - ice cold.

While guessing each time we have to keep track of the number of times, which I have no idea how to do that.

for each guess we have to figure out if its right or wrong

difference=|guess-target|

math.abs(guess-target)if(difference!=0)


If the user guesses correctly

(guess==target)

then update with a prompt telling them that they win.

Now as far a putting it togther to make it work I'm not so sure but I would really appreciate all the help I can get. I'm not trying to get anyone to do my work for me, I just need help getting it started and then I if I need more help, I will ask.
Copy linkTweet thisAlerts:
@prissanna06authorNov 12.2009 — Freejack is right, normall an attempt is expected before answers are given.

However, tonight I am feeling bored so:

[code=html]<!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" xml:lang="en" lang="en">
<head>
<title>bionoid - Guessing Game</title>
</head>
<body>

<script type="text/javascript">
var random_number = parseInt(Math.random() * 100, 10);
var guesses = 0;
var last_guess = 0;
var message = "";

while (last_guess != random_number) {
guesses++;
if (last_guess == 0) {
message = "What number am I thinking of?";
} else if (last_guess >= (random_number - 3) && last_guess <= (random_number + 3)) {
message = "You're Red Hot!";
} else if (last_guess >= (random_number - 10) && last_guess <= (random_number + 10)) {
message = "You're getting warm.";
} else if (last_guess >= (random_number - 20) && last_guess <= (random_number + 20)) {
message = "You're getting cold.";
} else {
message = "You're Ice Cold!";
}
last_guess = parseInt("0" + prompt(message + " (" + random_number + ")", last_guess), 10);
}

message = "Success! You Win!n";
if (guesses < 5) {
message += "You did an awesome job, it only took you " + guesses + " tries to guess the number!";
} else if (guesses <= 15) {
message += "Goodness. It took you " + guesses + " tries to guess the target number.";
} else {
message += "Yikes! It took you " + guesses + " tries to guess the target number.";
}
alert(message);
</script>

</body>
</html>[/code]


It should be easy enough to understand (I guess).[/QUOTE]




Thank you very much. When running the script, I noticed that the number it is guessing shows up eac time how do I get the number from showing up each time?
Copy linkTweet thisAlerts:
@thraddashNov 12.2009 — ? I only put it there so I didn't really have to guess.

Change line:
[CODE]last_guess = parseInt("0" + prompt(message + " (" + random_number + ")", last_guess), 10);[/CODE]

To:
[CODE]last_guess = parseInt("0" + prompt(message, last_guess), 10);[/CODE]

To get rid of it.
Copy linkTweet thisAlerts:
@prissanna06authorNov 12.2009 — thank you once again. I'm sorry if I came off rude to you or anything. I didn't mean anything by it. I'm just really stressed with this stuff and just reading it from a book isn't helping me at all.?
Copy linkTweet thisAlerts:
@thraddashNov 12.2009 — I'm just really stressed with this stuff and just reading it from a book isn't helping me at all.[/QUOTE]

Sounds like me with C++ ?
Copy linkTweet thisAlerts:
@prissanna06authorNov 12.2009 — sorry i would be no help there, but i'm a whiz at photshop, illustrator, indesign and now quarkXpress.
Copy linkTweet thisAlerts:
@FreejackNov 12.2009 — See I'm great with a book and just don't do well in classes. That's just me though, not everyone is wired the same way.

Carl
Copy linkTweet thisAlerts:
@WillfulJun 11.2012 — I know this is an old post, but apparently I am using the same book for my class. Antiquated, I know. I had posted about this Here. Those folks were helpful but I ended up using most of this one, I see how it works now. The issue I am having is adding a for loop after teh game to list the guesses made. Here is my code.

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;

&lt;head&gt;
&lt;meta content="text/html; charset=utf-8" http-equiv="Content-Type" /&gt;
&lt;title&gt;You Guessed&lt;/title&gt;

&lt;/head&gt;

&lt;body&gt;
&lt;script language="javascript" type="text/javascript"&gt;&lt;!--
var target = Math.floor(Math.random() * 100 + 1);
var guesses = 0;
var guess = 0;
var message = "";
var guessesArray = new Array()
guessesArray = guesses
while (guess != target) {
guesses++;
if (guess == 0) {
message = "What number am I thinking of?";
} else if (guess &gt;= (target - 3) &amp;&amp; guess &lt;= (target + 3)) {
message = "You're Red Hot!";
} else if (guess &gt;= (target - 10) &amp;&amp; guess &lt;= (target + 10)) {
message = "You're getting warm.";
} else if (guess &gt;= (target - 20) &amp;&amp; guess &lt;= (target + 20)) {
message = "You're getting cold.";
} else {
message = "You're Ice Cold!";
}
guess = Math.floor("0" + prompt(message, guess), 10);
}

<i> </i> message = "Success! You Win!n";
<i> </i> if (guesses &lt; 5) {
<i> </i> message += "You did an awesome job, it only took you " + guesses + " tries to guess the number!";
<i> </i> } else if (guesses &lt;= 15) {
<i> </i> message += "Goodness. It took you " + guesses + " tries to guess the target number.";
<i> </i> } else {
<i> </i> message += "Yikes! It took you " + guesses + " tries to guess the target number.";
<i> </i> }
<i> </i> alert(message);
<i> </i>[COLOR="Red"]document.write("&lt;h2&gt;Your Guesses&lt;/h2&gt;")
<i> </i>for (i=0; i&lt;guessesArray.length; i++) {
<i> </i> document.write(i, ". ", guessesArray[i], "&lt;br&gt;")[/COLOR]
<i> </i>}

&lt;/script&gt;

&lt;/body&gt;

&lt;/html&gt;

the highlighted part is the part I cant seem to correct properly, Did i not declare my array properly?
×

Success!

Help @prissanna06 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.25,
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,
)...