Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2

Thread: javascript

  1. #1
    New to the CF scene
    Join Date
    Mar 2004
    Location
    outer space
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry javascript

    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>


  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    As per the other "forum"

    <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 = (number1>= number2 ? 0 : number1)

    //Check to see if the first number is even
    evenNum = (number1 % 2==0 ? number1 : number1 + 1)
    evenNum2 = (number2 % 2==0 ? number2 : number2 - 1)

    for(tempNum = evenNum; tempNum <=evenNum2; tempNum += 2){
    sum += tempNum*1;
    }

    //Termination phase

    document.writeln( "<br/>The sum of the even integers " + "from " +
    number1 + " to " + number2 + " is " + sum );

    // -->
    </script>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •