Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Problem with addition

1. ## Problem with addition

Hi,
I'm just learning JS by going through a book, while trying one of the examples in the book I ran into a problem with " + " operator. It think it's best I show by example (don't worry it's a fairly short script):

Script
Code:
```/* This is an attempt to do some math with javascript */
var a;
var b;
var c;

a = prompt("Pick your favorite number");
b = prompt("Pick your second favorite number");
c = a + b

document.write("a = " + a + ", b = " + b + ", c = " + c + "<br />");

document.write("<br />a + b = " + c +  "<br />");
c += 6

document.write("<br />c + 6 = " + c + "<br />");
c -= 5

document.write("<br />c - 5 = " + c + "<br />");
c *= 3

document.write("<br />c * 3 =" + c + "<br />");
c /= 2

document.write("<br />c / 2 = " + c + "<br />");
c %= 7

document.write("<br />c % 7 = " + c + "<br />");

if (c > 0) {
document.write("<br /> Wrong!");
}
else {
document.write("<br /> You, I like")
}```
Output

a = 3, b = 4, c = 34

a + b = 34

c + 6 = 346

c - 5 = 341

c * 3 =1023

c / 2 = 511.5

c % 7 = 0.5

Wrong!
As you can see the script doesn't execute the mathematical operation for addition. It instead strings it together. What am I doing wrong?

• Originally Posted by errantwriter
Hi,
I'm just learning JS by going through a book, while trying one of the examples in the book I ran into a problem with " + " operator. It think it's best I show by example (don't worry it's a fairly short script):

Script
Code:
```/* This is an attempt to do some math with javascript */
var a;
var b;
var c;

a = prompt("Pick your favorite number");
b = prompt("Pick your second favorite number");
c = a + b

document.write("a = " + a + ", b = " + b + ", c = " + c + "<br />");

document.write("<br />a + b = " + c +  "<br />");
c += 6

document.write("<br />c + 6 = " + c + "<br />");
c -= 5

document.write("<br />c - 5 = " + c + "<br />");
c *= 3

document.write("<br />c * 3 =" + c + "<br />");
c /= 2

document.write("<br />c / 2 = " + c + "<br />");
c %= 7

document.write("<br />c % 7 = " + c + "<br />");

if (c > 0) {
document.write("<br /> Wrong!");
}
else {
document.write("<br /> You, I like")
}```
Output

As you can see the script doesn't execute the mathematical operation for addition. It instead strings it together. What am I doing wrong?
+ do both, string concatanation and addition based on the type of the arguments as you already guess,
you need to be sure that type of a and b is number and not string. one way is to use parseInt, some people use to multiply with 1.
Code:
```c = parseInt(a) + parseInt(b);
c = 1*a + 1*b;```
best regards

• ## Users who have thanked oesxyl for this post:

errantwriter (12-29-2010)

• Thank You very much!

• Originally Posted by errantwriter
Hi,
I'm just learning JS by going through a book, while trying one of the examples in the book I ran into a problem with " + " operator. It think it's best I show by example (don't worry it's a fairly short script)
Out of interest, what book on Javascript are you going through? Is it any good/useful for beginners?

• Originally Posted by Hashim1
Out of interest, what book on Javascript are you going through? Is it any good/useful for beginners?
I have a few Javascript books and I rarely use them. It's easier to search the web. Most search hits have explanations, code snippets and other useful stuff.

And you can't cut-n-paste from a book into your code editor!

•

#### Posting Permissions

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