Jump to content
mickeymouse

Addition Problem

Recommended Posts

byyr contains 20. 

If I add 1, I get 201 (wrong result).  (See first set of code below)

If I multiply by 5, I get 100 (correct result).  (See second set of code below) 

If I multiply by 1, I get 100 (correct result). 

According to documentation, a + in Java Script is addition.

I have spent over 2 hrs trying to figure out what was wrong with my (more complicated) coding.

Please, please, tell me why addition doesn't work.

 

function mytab(byyr,bymo,mo,fld,to)
{with (byyr, bymo, mo, fld, to)
var x = byyr.value
var y = x + 1;
alert(y);}

function mytab(byyr,bymo,mo,fld,to)
{with (byyr, bymo, mo, fld, to)
var x = byyr.value; 
var y = x * 5;
alert(y);}

 

Share this post


Link to post
Share on other sites

"+" is used for string concatenation. If the values are strings then they will use the string version of the "+" operator. You have to convert the value to a number before operating in order for it to work as you intend it to.
 

var x = Number(byyr.value);
var y = x + 1;

 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...