The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Random Number Game
Corey Cody
post Mar 9 2012, 11:05 AM
Post #1





Group: Members
Posts: 9
Joined: 8-September 11
Member No.: 15,352



I am a 18 year old student in my a Web design 2 class and I have been assigned a project to make a number game.

I created the first version of the game as instructed, using the number 7. but now I have to use math.random() and the function I have doesn't call the variable to print in the box.

I am attaching it to this and hope that you can help me fix it.


Attached File(s)
Attached File  guessinggame2.htm ( 724bytes ) Number of downloads: 98
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 9 2012, 11:22 AM
Post #2


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 15,478
Joined: 9-August 06
Member No.: 6



Let's see...

1. You can't nest script blocks. You probably didn't mean to, but you do. And one of them aren't closed.

2. The var luckynumber line needs a semicolon at the end.

3. JavaScript is case sensitive. It's Math, not math.

That's about it.

Tip: Most browsers let you turn on script debugging. The debugger would find at least the two last errors.


--------------------
"Never go to excess, but let moderation be your guide."
- Cicero

IPB Image
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 9 2012, 11:57 AM
Post #3


.
********

Group: WDG Moderators
Posts: 6,087
Joined: 10-August 06
Member No.: 7



The variable luckynumber shouldn't be quoted here:

CODE
if(document.guessform.inputbox.value=="luckynumber")

--currently the value to guess is the string "luckynumber", not a number. tongue.gif

Also, since the variable luckynumber is set when the page is loaded, it will not change by clicking the button, so (after you've removed the quotes above) the user can guess the right number within ten attempts. Don't know if that's what you want. unsure.gif

Side-note: you may want to add a space between an HTML attribute and a previous attribute's value, like this:

CODE
<input type="text" name="inputbox" size="2" />


BTW, it's to not 100% clear that the user should insert his number guess in the first text field, and that the second field is just for the response. You might give the latter a DISABLED or READONLY attribute to avoid confusion. Or you could put the response in some non-form element, using the innerHTML property.

This post has been edited by Christian J: Mar 9 2012, 01:51 PM


--------------------
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Mar 9 2012, 12:08 PM
Post #4


Don't like donuts. Don't do MySpace.
********

Group: WDG Moderators
Posts: 15,478
Joined: 9-August 06
Member No.: 6



That's why I never won. tongue.gif


--------------------
"Never go to excess, but let moderation be your guide."
- Cicero

IPB Image
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
XP1
post Mar 17 2012, 10:51 AM
Post #5





Group: Members
Posts: 7
Joined: 14-March 12
Member No.: 16,709



You should use
CODE
document.getElementById
and
CODE
===
.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Mar 18 2012, 11:40 AM
Post #6


.
********

Group: WDG Moderators
Posts: 6,087
Joined: 10-August 06
Member No.: 7



QUOTE(XP1 @ Mar 17 2012, 04:51 PM) *

You should use
CODE
document.getElementById

and
CODE
===
.

I prefer getElementById too (since you don't need any <form> start- and end tags with it), but it's not necessary.

Regarding the === operator, the type of the OP's variable luckynumber is "number", and since browsers regard the types of form field values as strings the === operator will not work.




--------------------
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
XP1
post Mar 19 2012, 02:11 AM
Post #7





Group: Members
Posts: 7
Joined: 14-March 12
Member No.: 16,709



QUOTE(Christian J @ Mar 18 2012, 12:40 PM) *
I prefer getElementById too (since you don't need any <form> start- and end tags with it), but it's not necessary.

Regarding the === operator, the type of the OP's variable luckynumber is "number", and since browsers regard the types of form field values as strings the === operator will not work.
Yes, it doesn't have to work like that, but it should. I'm not saying to make the changes as-is. Some code needs to be rewritten to follow good style. In the case of ===, it is good style to rewrite the expression so that the variables being compared were explicitly converted first. Implicit type coercion causes confusion especially among new programmers.

It is also a great idea to use a lint program to check for syntax and style correctness. For example, JSLint forces you to always write ===.
JSLint is one of the most useful tools when I am writing JavaScript.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 1st October 2014 - 05:17 AM