Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    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.

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 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)

  • #3
    New to the CF scene
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 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.

  • #6
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,075
    Thanks
    38
    Thanked 498 Times in 492 Posts

    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.

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,918
    Thanks
    203
    Thanked 2,531 Times in 2,509 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.


  •  

    Tags for this Thread

    Posting Permissions

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