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?
 User Name Remember Me? Password

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,463 Thanks: 200 Thanked 2,468 Times in 2,446 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,463 Thanks: 200 Thanked 2,468 Times in 2,446 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

 Tags add, i=prt, interest, variable

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 04:54 AM.