JavaScript

Moderators: None (Apply to moderate this forum)
Number of threads: 2059
Number of posts: 5157

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
newb question, experts plz come in Posted by wyz0020 on 21 Apr 2004 at 11:37 AM
My first attempt to add two variables and output the result ended in failure.
Example Code:
var num1 = prompt("Number 1","Enter");
var num2 = prompt("Number 2","Enter");

var num3 = num1 + num2;
alert("Number 3 is "+num3);

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:
function AddValues(term1,term2)
{
    var returnvalue = term1;
       
    for(var i = 0; i < term2; i++)
        {
            returnvalue++;
        }
    return returnvalue;
}

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.




Report
Re: newb question, experts plz come in Posted by zibadian on 21 Apr 2004 at 3:50 PM
: My first attempt to add two variables and output the result ended in failure.
: Example Code:
:
: var num1 = prompt("Number 1","Enter");
: var num2 = prompt("Number 2","Enter");
: 
: var num3 = num1 + num2;
: alert("Number 3 is "+num3);
: 

: 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:
:
: function AddValues(term1,term2)
: {
:     var returnvalue = term1;
:        
:     for(var i = 0; i < term2; i++)
:         {
:             returnvalue++;
:         }
:     return returnvalue;
: }
: 

: 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:
  var num1 = prompt("Number 1","Enter") + 0;

This way the addition will follow the numerical rules.
Report
Re: newb question, experts plz come in Posted by wyz0020 on 23 Apr 2004 at 10:14 AM
Thx for replying, but the method u said doesn't work. Is there any other type-cast methods?





: : My first attempt to add two variables and output the result ended in failure.
: : Example Code:
: :
: : var num1 = prompt("Number 1","Enter");
: : var num2 = prompt("Number 2","Enter");
: : 
: : var num3 = num1 + num2;
: : alert("Number 3 is "+num3);
: : 

: : 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:
: :
: : function AddValues(term1,term2)
: : {
: :     var returnvalue = term1;
: :        
: :     for(var i = 0; i < term2; i++)
: :         {
: :             returnvalue++;
: :         }
: :     return returnvalue;
: : }
: : 

: : 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:
:
:   var num1 = prompt("Number 1","Enter") + 0;
: 

: This way the addition will follow the numerical rules.
:

Report
Re: newb question, experts plz come in Posted by Weirdofreak on 24 Apr 2004 at 4:39 AM
Multiply by one. You can add to a string, so with + 0 it probably converts the 0 to a string instead of the string to a number, but since you can't multiply a string, it'll have to convert it to a number.

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.



 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.