Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-25-2011, 11:03 PM   PM User | #1
tanayp5
New to the CF scene

 
Join Date: Oct 2011
Posts: 2
Thanks: 1
Thanked 0 Times in 0 Posts
tanayp5 is an unknown quantity at this point
Unhappy 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.
tanayp5 is offline   Reply With Quote
Old 10-26-2011, 01:04 AM   PM User | #2
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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.
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
tanayp5 (10-26-2011)
Old 10-26-2011, 01: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
tanayp5 is an unknown quantity at this point
Quote:
Originally Posted by jmrker View Post
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.
tanayp5 is offline   Reply With Quote
Old 10-26-2011, 02:41 AM   PM User | #4
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Question

Quote:
Originally Posted by tanayp5 View Post
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?
jmrker is offline   Reply With Quote
Old 10-26-2011, 07:56 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 10-26-2011, 03:07 PM   PM User | #6
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,763
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Smile

Quote:
Originally Posted by Philip M View Post
principle != principal
You can stand on your principle while others will take no interest in your principal.
jmrker is offline   Reply With Quote
Old 10-26-2011, 03:19 PM   PM User | #7
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,033
Thanks: 197
Thanked 2,410 Times in 2,388 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Reply

Bookmarks

Tags
add, i=prt, interest, variable

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

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 Jump


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


Advertisement
Log in to turn off these ads.