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: 227 |
pandy |
Mar 9 2012, 11:22 AM
Post
#2
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,734 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. |
Christian J |
Mar 9 2012, 11:57 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,665 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. 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. 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 This post has been edited by Christian J: Mar 9 2012, 01:51 PM |
pandy |
Mar 9 2012, 12:08 PM
Post
#4
|
🌟Computer says no🌟 Group: WDG Moderators Posts: 20,734 Joined: 9-August 06 Member No.: 6 |
That's why I never won.
|
XP1 |
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 === . |
Christian J |
Mar 18 2012, 11:40 AM
Post
#6
|
. Group: WDG Moderators Posts: 9,665 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
#7
|
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: 16th May 2024 - 12:58 PM |