Random Number Game |
Random Number Game |
Corey Cody |
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) guessinggame2.htm ( 724bytes ) Number of downloads: 216 |
XP1 |
Mar 17 2012, 10:51 AM
Post
#2
|
Group: Members Posts: 7 Joined: 14-March 12 Member No.: 16,709 |
You should use
CODE document.getElementById and CODE === . |
Christian J |
Mar 18 2012, 11:40 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,628 Joined: 10-August 06 Member No.: 7 |
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. |
XP1 |
Mar 19 2012, 02:11 AM
Post
#4
|
Group: Members Posts: 7 Joined: 14-March 12 Member No.: 16,709 |
I prefer getElementById too (since you don't need any <form> start- and end tags with it), but it's not necessary. 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.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. 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. |
Lo-Fi Version | Time is now: 18th March 2024 - 10:41 PM |