![]() |
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] |
You see those two sticky posts at the top of the forum? The polite thing would be to read those.
|
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 :) |
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. |
ok just a sec I'm investigating what you wrote ^^
|
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] |
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.
|
| All times are GMT -5. The time now is 2:31 AM. |
Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC