Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com JavaScript fails to add properly, adds as if strings are present?

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 10-26-2011, 12:03 AM PM User | #1 tanayp5 New to the CF scene   Join Date: Oct 2011 Posts: 2 Thanks: 1 Thanked 0 Times in 0 Posts JavaScript fails to add properly, adds as if strings are present? Sorry for my beginner question but I am having some trouble getting this to work. I am trying to create a simple I=PRT calculator so it can calculate interest. But when you run it it adds the digits. Ex. 5+100=5100 instead of 105. Give it a try. My code is bellow.[CODE] // JavaScript Document alert("This is a simple calculator which will allow you to calculate the interest on a principle."); var principle = prompt("What is the amount you would like to calculate interest for?"); var rate = prompt("What is the interest rate. eg. For 3.9% write 3.9"); var term = prompt("How long in the period under calculation going to be? eg. For 1 year write 1"); var interest = principle*(rate/100)*term; var result = principle + interest; var a = "This means the interest is going to be \$"; var b = " And the total to be paid is \$"; var c = a + interest + b + result; alert(c); [CODE] Thanks in advance.
 10-26-2011, 02:04 AM PM User | #2 jmrker Senior Coder     Join Date: Aug 2006 Location: FL Posts: 2,935 Thanks: 36 Thanked 477 Times in 471 Posts If ANY variable is a string, then the + of the intended math is treated as concatenation. You could try: Code: ```var c = Number(a) + Number(interest) + Number(b) + (Number(principle) + Number(interest)); // or var result = (principle*1) + (interest*1); var c = (a*1) + (interest*1) + (b*1) + result; // or a few other variations on the theme // parseFloat() or parseInt() come to mind.```
 Users who have thanked jmrker for this post: tanayp5 (10-26-2011)
10-26-2011, 02:24 AM   PM User | #3
tanayp5
New to the CF scene

Join Date: Oct 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
Quote:
 Originally Posted by jmrker If ANY variable is a string, then the + of the intended math is treated as concatenation. You could try: Code: ```var c = Number(a) + Number(interest) + Number(b) + (Number(principle) + Number(interest)); // or var result = (principle*1) + (interest*1); var c = (a*1) + (interest*1) + (b*1) + result; // or a few other variations on the theme // parseFloat() or parseInt() come to mind.```
Thanks so much for your help, I got it to work right away. By the way, if possible can you please simplify what you did to help me solve the problem.

10-26-2011, 03:41 AM   PM User | #4
jmrker
Senior Coder

Join Date: Aug 2006
Location: FL
Posts: 2,935
Thanks: 36
Thanked 477 Times in 471 Posts

Quote:
 Originally Posted by tanayp5 Thanks so much for your help, I got it to work right away. By the way, if possible can you please simplify what you did to help me solve the problem.
Which one?

 10-26-2011, 08:56 AM PM User | #5 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,475 Thanks: 200 Thanked 2,470 Times in 2,448 Posts principle != principal __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.
10-26-2011, 04:07 PM   PM User | #6
jmrker
Senior Coder

Join Date: Aug 2006
Location: FL
Posts: 2,935
Thanks: 36
Thanked 477 Times in 471 Posts

Quote:
 Originally Posted by Philip M principle != principal
You can stand on your principle while others will take no interest in your principal.

 10-26-2011, 04:19 PM PM User | #7 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,475 Thanks: 200 Thanked 2,470 Times in 2,448 Posts Keep you hands off my principal! __________________ All the code given in this post has been tested and is intended to address the question asked. Unless stated otherwise it is not just a demonstration.

 Bookmarks