English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

Javascript- So many errors in my script:(?
------------------------------...

Hi
can one of you javascript experts please help me

I am new to Javascript and have written this script but it is full of errors.
Can someone pleas point them out to me. It would be greatly appreciated
Thank you.
Now here is my script:

Code:








Press F1 or click the Refresh (or Reload) button to run this script again


Thank you so so much for any help you can give. thank you

2007-10-03 09:40:03 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

1. function begin(); should not have a semi-colon after it.

2. if(arrayCtr = 0) should read if(arrayCtr == 0)

3. fix your /* comments that never close

4.
switch(type)
{

case 1:
// call addition function
var result = add(num1, num2);
break;

should read

var result;
switch(type)
{
case 1:
// call addition function
result = add(num1, num2);
break;

(declaring var result in each case is bad, and you lose your value when you need it because of scope issues)

5.
if(arrayCtr = 0)
calcType[arrayCtr] = "addition";
else
if(arrayCtr == 1)
calcType[arrayCtr] = "subtraction";
else
if(arrayCtr == 2)
calcType[arrayCtr] = multiplication;
else
if(arrayCtr == 3)
calcType[arrayCtr] = "division";
else
if(arrayCtr == 4)
calcType[arrayCtr] = "modulus";

should use a switch instead (not a bug, but a design thing)

2007-10-03 11:43:53 · answer #1 · answered by Anonymous · 0 0

There are a few common errors. We scripters make them no matter how long we use Javascript. So please don't lose heart. :D

If you use Firefox, it has a handy error console that reports Javascript errors. It can be found in the tools menu. It's not very friendly, but it can be very helpful.

Here is a list the errors and fixes:
-- You don't need the ";" after "function begin()"
-- There should be quotes around "multiplication" in the first for loop.
(You could eliminate this whole loop by using Javascript's built in array declaration EG:
'var calcType = ["addition","subtraction","multiplication","division","modulus"];')
-- In the string ... "operation type:
1-Addition, "... use "\n" instead of "
"
-- "while (type > 0 && type < 6);" should be "while (!(type > 0 && type < 6));"
-- No "*/" after 'as a number (not as a string)'
-- 'parseFloat(window.alert("Pleas... enter the second number: ", "0"));' should be a prompt instead of an alert.
-- "switch(type)" should be "switch(parseInt(type))". (or else all the case statement numbers should be quoted 'case "1":' etc.)
-- The "num 1" in "subtract(num 1, num2)" should be "num1"
(I do this all the time!)
-- There should be a "}" after 'window.alert("Invalid operation type")'
(to close the "switch" block.)
-- "function multiply(num1, num 2){" should be "function multiply(num1, num2){
-- There should be a "" after ""
-- '' should be '
(otherwise it doesn't actually call the function.)
-- The second "" (at the very end) should be "".


I've tested these fixes, and here is the modified script (It seems to work):
vvvvvvvvvvvvvvvvvv









Press F1 or click the Refresh (or Reload) button to run this script again



^^^^^^^^^^^^^
Best of luck on your Javascripting!

2007-10-03 12:05:59 · answer #2 · answered by okonomiyaki 2 · 0 0

Well... I think is this:
/*
prompt user for the first number, validate that the first character in the string argument is a number,
parse the string from left to right until it reaches the end of the number, discard any characters that
occur after the end of the number input, and finally return the number as a number (not as a string)

As you can see... you never closed the multiple lines comment (*/). The correct way is this one:
/*
prompt user for the first number, validate that the first character in the string argument is a number,
parse the string from left to right until it reaches the end of the number, discard any characters that
occur after the end of the number input, and finally return the number as a number (not as a string) */

Remember the */
That's all I saw

2007-10-03 09:49:23 · answer #3 · answered by shadow 5 · 0 0

first, in accordance to standards, 'script' tag could desire to look like this: