My first attempt to add two variables and output the result ended in failure.
Example Code:
[code]
var num1 = prompt("Number 1","Enter");
var num2 = prompt("Number 2","Enter");
var num3 = num1 + num2;
alert("Number 3 is "+num3);
[/code]
When I entered (as an example) 10 for number 1, and 15 for number 2, 1015 was the result in the alert box.
The second attempt was a success, where I created a function to add the two numbers:
[code]
function AddValues(term1,term2)
{
var returnvalue = term1;
for(var i = 0; i < term2; i++)
{
returnvalue++;
}
return returnvalue;
}
[/code]
There may be stupid errors in my code but you get the idea. Now, in the first example, why does it output the numbers as compounded character arrays? Is it because every variable defaults to a char array? If so, how do I change this? I doubt something this basic requires workaround functions like in the second example. Thanks in advance.
Comments
: Example Code:
: [code]
: var num1 = prompt("Number 1","Enter");
: var num2 = prompt("Number 2","Enter");
:
: var num3 = num1 + num2;
: alert("Number 3 is "+num3);
: [/code]
: When I entered (as an example) 10 for number 1, and 15 for number 2, 1015 was the result in the alert box.
:
: The second attempt was a success, where I created a function to add the two numbers:
: [code]
: function AddValues(term1,term2)
: {
: var returnvalue = term1;
:
: for(var i = 0; i < term2; i++)
: {
: returnvalue++;
: }
: return returnvalue;
: }
: [/code]
: There may be stupid errors in my code but you get the idea. Now, in the first example, why does it output the numbers as compounded character arrays? Is it because every variable defaults to a char array? If so, how do I change this? I doubt something this basic requires workaround functions like in the second example. Thanks in advance.
:
:
The Prompt() function always results in a string, which means that both num variables will also be type-cast as strings. You can force javascript to make them integers, by adding 0 to, when creating the variables:
[code]
var num1 = prompt("Number 1","Enter") + 0;
[/code]
This way the addition will follow the numerical rules.
: : My first attempt to add two variables and output the result ended in failure.
: : Example Code:
: : [code]
: : var num1 = prompt("Number 1","Enter");
: : var num2 = prompt("Number 2","Enter");
: :
: : var num3 = num1 + num2;
: : alert("Number 3 is "+num3);
: : [/code]
: : When I entered (as an example) 10 for number 1, and 15 for number 2, 1015 was the result in the alert box.
: :
: : The second attempt was a success, where I created a function to add the two numbers:
: : [code]
: : function AddValues(term1,term2)
: : {
: : var returnvalue = term1;
: :
: : for(var i = 0; i < term2; i++)
: : {
: : returnvalue++;
: : }
: : return returnvalue;
: : }
: : [/code]
: : There may be stupid errors in my code but you get the idea. Now, in the first example, why does it output the numbers as compounded character arrays? Is it because every variable defaults to a char array? If so, how do I change this? I doubt something this basic requires workaround functions like in the second example. Thanks in advance.
: :
: :
: The Prompt() function always results in a string, which means that both num variables will also be type-cast as strings. You can force javascript to make them integers, by adding 0 to, when creating the variables:
: [code]
: var num1 = prompt("Number 1","Enter") + 0;
: [/code]
: This way the addition will follow the numerical rules.
:
There's also the parseInt() function. It'll convert a string to an integer by stripping off everything past the first non-numeric character - so 123hello45 would just give 123, and hello12345 would give NaN (Not a Number). And the string 123 would return the number 123.