Ruriko

05-25-2009, 11:00 AM

Hi I'm a newbie to javascript and I made this script

<script>

var num = parseInt(prompt("Enter the number of integers to follow"));

var sum = 0;

for (i = 0; i < num; i++){

sum += parseInt(prompt("Enter a number"));

}

if (isNaN(num)) {

alert("Invalid");

}

else {

if (sum < 0) {

document.writeln("The sum is 0 and the average is 0");

}

else {

document.writeln("The sum is " + sum + " and the average is " + sum/num);

}

}

</script>

The scenario : Create in javascript that will read a series of integers at the terminal. The first integer is special, as it indicates how many more integers will follow. Your javascript is to calculate the sum and average of the integers, excluding the first integer, and display these values to the screen.

If the total is not greater than 0 then display "The sum is 0 and the average is 0"

Did I write the script correctly? am I missing anything that a dumb person might do? for example the person might type in letters instead of numbers.

<script>

var num = parseInt(prompt("Enter the number of integers to follow"));

var sum = 0;

for (i = 0; i < num; i++){

sum += parseInt(prompt("Enter a number"));

}

if (isNaN(num)) {

alert("Invalid");

}

else {

if (sum < 0) {

document.writeln("The sum is 0 and the average is 0");

}

else {

document.writeln("The sum is " + sum + " and the average is " + sum/num);

}

}

</script>

The scenario : Create in javascript that will read a series of integers at the terminal. The first integer is special, as it indicates how many more integers will follow. Your javascript is to calculate the sum and average of the integers, excluding the first integer, and display these values to the screen.

If the total is not greater than 0 then display "The sum is 0 and the average is 0"

Did I write the script correctly? am I missing anything that a dumb person might do? for example the person might type in letters instead of numbers.