...

View Full Version : sum won't compute



hmcka
03-04-2012, 07:25 PM
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>

hmcka
03-04-2012, 07:26 PM
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>

Philip M
03-04-2012, 07:35 PM
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):-




<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

hmcka
03-04-2012, 07:46 PM
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.

Philip M
03-04-2012, 07:54 PM
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.

Philip M
03-04-2012, 08:17 PM
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;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum