PDA

View Full Version : Problem with addition

errantwriter
12-25-2010, 02:42 AM
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

/* 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?

oesxyl
12-25-2010, 03:00 AM
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

/* 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.

c = parseInt(a) + parseInt(b);
c = 1*a + 1*b;

best regards

errantwriter
12-29-2010, 09:07 AM
Thank You very much!

Hashim1
12-29-2010, 04:22 PM
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?

Krupski
12-29-2010, 04:31 PM
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! :)