![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2007
Posts: 8
Rep Power: 0
![]() |
Need help with a very simple Javascript script
The point of this script, is that it will ask for a number. If the number is even, then it will add it to the "Even" stack of numberse, and it it's uneven, it'll add it to the "Uneven" stack of numbers. The script runs until the user types in no.
By the way, I know nothing about Javascript, I made it this far, but it doesn't work n I have no idea why.. ^^ Thanks for any assistance. :banana: [html] <html> <head> <script language="javascript"> var even=0; var uneven=0; var repeat=si; var resto=0; while ( repeat = "si" ) { number = prompt('Enter a number'); resto = numero % 2; if (resto != 0) { uneven = uneven + number; alert(uneven); } else { even = even + number; alert(even); } alert('sum of even numbers :" even "sum of uneven numbers:" uneven); repeat = prompt('Do you want to enter another number?') } </head> </html> [/html] Last edited by Pyrathian; Jun 1st, 2007 at 5:49 PM. Reason: Added [html] tags |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
You see those two sticky posts at the top of the forum? The polite thing would be to read those.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2007
Posts: 8
Rep Power: 0
![]() |
I did read the stickys. Well I tried to describe it as best I could, the topic name is as best I could come up with considering the script has no useful purpose.
The script is ment to count up as many un even and even numbers into 2 different stacks and display them when a user types no. The bug currently, is that nothing comes up at all when I open the HTML document. It should start off with the prompt. I know it says, don't just paste the code. But I figured that since it's small, and I've put everything I know about it, it could be ok. Like I said, I don't know what the error is since nothing comes up at all. Thanks ![]() |
|
|
|
|
|
#4 |
|
PFO Founder
![]() ![]() |
I believe DaWei was trying to get at was the use of
[code][/code] [php][/php] or [html][/html] tags when you post code. Anyway back on topic. ok, I found a number of things wrong with the original javascript you posted. Below is the script in working order. First thing that I changed was the <script language="javascript"> to <script type="text/javascript"> I don't know if this is a big deal or not but I changed it as that is the way I have always seen it done. Second is I added the </script> after the javascript since you were missing it. Another thing I noticed was that in your while condition you have it set repeat to "si" instead of checking to see if it is "si" you have to remember that you need 2 = signs instead of just 1. Then there is you had a spelling error as you are using number everywhere except you were using numero in the line "resto = numero % 2;" instead of number, also you weren't concatenating the variables to the strings in the alert at the bottom. [html] <html> <head> <script type="text/javascript"> var even=0; var uneven=0; var repeat="si"; var resto=0; while ( repeat == "si" ) { number = prompt('Enter a number'); resto = number % 2; if (resto != 0) { uneven = uneven + number; alert(uneven); } else { even = even + number; alert(even); } alert("sum of even numbers :" + even + "sum of uneven numbers:" + uneven); repeat = prompt('Do you want to enter another number?') } </script> </head> </html> [/html] One thing I think most people who write any javascript need is firebug, an extension for Firefox. It will point out a good amount of errors. It wouldn't have found the infinite loop error in the condition of the while but you would have figured that out when the javascript kept going and wouldn't stop for you.
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. Last edited by big_k105; Jun 1st, 2007 at 5:47 PM. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2007
Posts: 8
Rep Power: 0
![]() |
ok just a sec I'm investigating what you wrote ^^
Last edited by Pyrathian; Jun 1st, 2007 at 6:09 PM. |
|
|
|
|
|
#6 |
|
Newbie
Join Date: Jun 2007
Posts: 8
Rep Power: 0
![]() |
Ok, here's the new set of code, working fine now.^^
So far, it's just fine. But I have one small issue. You see, after you enter a number, if you leave it blank, it'll say, cannot be blank. ok good, but If I push ESC. it says even :O, Is there something I can do to fix that? Thanks so far, I've learned much more in a ½ hour than if I had spent 4 hours staring at my computer monitor :banana: By the way, what is the importance of the concatonation, or those plus signs you added into that Alert, is it like defining the usage of the variable or something? Thanks again [HTML] <html> <head> <script type="text/javascript"> var even=0; var uneven=0; var repeat="yes"; var resto=0; while ( repeat == "yes" ) { number = prompt('Enter a number',' '); if ( number == " " ) { alert('Cannot be blank') } else { resto = number % 2; if (resto != 0) { uneven = uneven + 1; alert("uneven"); } else { even = even + 1; alert("even"); } repeat = prompt('Do you want to enter another number? Yes to continue, any key to exit',' ') } } alert("sum of even numbers: " + even + " sum of uneven numbers: " + uneven); </script> </head> </html> [/HTML] |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jun 2007
Posts: 2
Rep Power: 0
![]() |
Please try the code in Netscape and enter "javascript:" in the url which will give you a JS debugger...what a great great help that's been.
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Start Learning Java script | smita | JavaScript and Client-Side Browser Scripting | 0 | Mar 15th, 2007 7:47 AM |
| A simple script isn't working correctly | scuzzman | Perl | 3 | Dec 23rd, 2005 6:42 AM |
| A simple perl script | satimis | Perl | 3 | Aug 15th, 2005 9:31 AM |
| A simple script to execute a command package | satimis | Bash / Shell Scripting | 3 | Aug 12th, 2005 11:28 PM |
| Simple menu script | trufla | Bash / Shell Scripting | 2 | Apr 4th, 2005 10:17 AM |