java

03-14-2004, 10:35 AM

Hello,

I am trying to write a javascript that allows a user to enter 2 numbers. The first and last numbers can be either even / odd numbers. If the first number is larger than the last number the answer will be 0. Only the even numbers will be added. For example:

1) if the first number entered is 2, and the last number entered is 8 the answer is 20.

(this is how it works: 2 + 4 + 6 + 8 = 20)

**that is because only the even numbers from 2 to 8 are added.

2) if the first number entered is 3, and the last number entered is 8 the answer is 18.

(this is how it works: 4 + 6 + 8 = 18)

**that is because only the even numbers from 3 to 8 are added.

I am able to get the script to work with the even numbers, like in example 1. However, I am having a problem getting it to work with example 2.

Here is a copy of the script that I am working on:

<script type = "text/javascript">

<!--

var sum = 0, //sum of the even numbers

first, //first number entered by the user

last, //last number entered by the user

evenNum, //for the even number

number1, //first number value

number2; //last number value

//prompt the user for the first number

first = window.prompt("Enter first number", "0");

//prompt the user for the last number

last = window.prompt("Enter last number", "0");

//convert the string value to integer

number1 = parseInt ( first );

number2 = parseInt ( last );

//check to see if the first number is greater than the last number

number1 >= number2 ? "0" : number1

//Check to see if the first number is even

evenNum = number1 % 2 ? number1 : number1 + 1;

for ( var evenNum = number1; evenNum <=number2; evenNum += 2)

sum += evenNum;

//Termination phase

document.writeln( "<br/>The sum of the even integers " + "from " +

number1 + " to " + number2 + " is " + sum );

// -->

</script>

:eek: :mad: :( :( :( :( :( :confused: :confused: :confused:

I am trying to write a javascript that allows a user to enter 2 numbers. The first and last numbers can be either even / odd numbers. If the first number is larger than the last number the answer will be 0. Only the even numbers will be added. For example:

1) if the first number entered is 2, and the last number entered is 8 the answer is 20.

(this is how it works: 2 + 4 + 6 + 8 = 20)

**that is because only the even numbers from 2 to 8 are added.

2) if the first number entered is 3, and the last number entered is 8 the answer is 18.

(this is how it works: 4 + 6 + 8 = 18)

**that is because only the even numbers from 3 to 8 are added.

I am able to get the script to work with the even numbers, like in example 1. However, I am having a problem getting it to work with example 2.

Here is a copy of the script that I am working on:

<script type = "text/javascript">

<!--

var sum = 0, //sum of the even numbers

first, //first number entered by the user

last, //last number entered by the user

evenNum, //for the even number

number1, //first number value

number2; //last number value

//prompt the user for the first number

first = window.prompt("Enter first number", "0");

//prompt the user for the last number

last = window.prompt("Enter last number", "0");

//convert the string value to integer

number1 = parseInt ( first );

number2 = parseInt ( last );

//check to see if the first number is greater than the last number

number1 >= number2 ? "0" : number1

//Check to see if the first number is even

evenNum = number1 % 2 ? number1 : number1 + 1;

for ( var evenNum = number1; evenNum <=number2; evenNum += 2)

sum += evenNum;

//Termination phase

document.writeln( "<br/>The sum of the even integers " + "from " +

number1 + " to " + number2 + " is " + sum );

// -->

</script>

:eek: :mad: :( :( :( :( :( :confused: :confused: :confused: