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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    sum won't compute

    Hello. Writing Javascript is new to me, and I've been reviewing this code for awhile and I can't find what's different than the printed example that I'm referring to. Why won't it work? I want a sum to compute after the user types in two integers. I've even gone as far a counting the characters in each line--everything is the same. Thank you.


    [CODE]
    <script type = "text/javascript">
    <!--
    var firstNumber; // first string entered by user
    var secondNumber; // second string entered by user
    var number1; //first number to add
    var number2; //second number to add
    var sum; //sum of number1 and number2

    // read in first number from user as a string
    firstNumber = window.prompt("Enter first integer" );

    // read in secondnumber from user as a string
    secondNumber = window.prompt("Enter second integer" );

    //convert numbers from strings to integers
    number1 = parseInt( firstNumber );
    number2 = parseInt( secondNumber );

    sum = number1 + number2; // add the numbers

    //display the results
    document.writeIn( "<h1>The sum is " + sum + "</h1>");
    // -->
    </script>

  • #2
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    more specific info for post

    Oh, I should mention that I think the error is in the end portion of this as I am able to get it to enter in numbers.

    [CODE]
    //convert numbers from strings to integers
    number1 = parseInt( firstNumber );
    number2 = parseInt( secondNumber );

    sum = number1 + number2; // add the numbers

    //display the results
    document.writeIn( "<h1>The sum is " + sum + "</h1>");
    // -->
    </script>

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    It is not document.writeIn() but document.writeln() if you wish to use this long-obsolete method of displaying a result. Learn from this that some characters 0 and O and 1 and l and I are easily confused, and best avoided.

    You can greatly simplify your code (there is no need to declare your variables separately):-


    Code:
    <script type = "text/javascript">
    
    // read in first number from user as a number
    var firstNumber = parseInt(window.prompt("Enter first integer",""));
    
    // read in secondnumber from user as a number
    var secondNumber = parseInt(window.prompt("Enter second integer","" ));
    
    var sum = firstNumber + secondNumber;  // add the numbers
    
    //display the results
    document.write( "<h1>The sum is " + sum + "</h1>");
    </script>

    "Always carry a bottle of whisky in case of snakebite and furthermore always carry a small snake. " - W. C. Fields
    Last edited by Philip M; 03-04-2012 at 07:40 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #4
    New to the CF scene
    Join Date
    Mar 2012
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    thank you and follow-up question

    Ok. I think I'm picking up what you're putting down. You are basically shortening the code by grouping and inserting variables--simple substitution. Or, looks a lot like algebra.

    What's the ,"" doing in the below code. n other words, what's that doing?

    [CODE]
    // read in first number from user as a number
    var firstNumber = parseInt(window.prompt("Enter first integer",""));

    Thank you.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Try it with and without the quotes, and observe the difference.

    You can improve the script with this construct

    firstNumber = parseInt(window.prompt("Enter first integer" )) || 0;

    || means or, and || 0 traps invalid entries so that if parseInt(() cannot evaluate the input value as an integer, it is replaced by 0.
    Last edited by Philip M; 03-04-2012 at 08:00 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • Users who have thanked Philip M for this post:

    hmcka (03-04-2012)

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    I should have said that if you use parseInt() you should specify the radix (10), as otherwise if the user enters a number with a 0 prefix it will be interpreted as octal, not decimal.

    firstNumber = parseInt(window.prompt("Enter first integer", "" ),10) || 0;

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

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