Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript numbers concatenate instead of adding but typeof is number not string

I am new to javaScript. I am building a calculator here

I have stored the input values in variables so that I can eventually manipulate the results to perform calculations based on input. For now I just want all of the values to add together.

However, rather than adding, they are concatenating. I used parseInt to prevent javascript from viewing the numbers as strings, and typeOf reveals that they are numbers.

Here is my javascript:

$(document).ready(function() {

var theTerm = $("#theTerm").val();
var theRate = $("#theRate").val();
var thePrice = $("#thePrice").val();
var theTax = $("#theTax").val();
var theDown = $("#theDown").val();
var theTrade = $("#theTrade").val();
var theResult = parseInt(theTerm + theRate + thePrice + theTax + theDown + theTrade, 10);

$("#calculate").click(function(){
    alert(theResult);
    alert(typeof(theResult));
});

}); 

and the HTML:

<div id="calculator">
<span id="calculatorHeader">Monthly Payment Calculator</span>
<table style="margin:0 auto;">
    <tr>
    <td style="width:40px;">Term
        <input id="theTerm" size="5" value="7" name="term" style="width:35px" />
    </td>
    <td style="width:40px;">Rate(%)
        <input id="theRate" size="5" value="7" name="apr" style="width:35px" />
    </td>
    <td style="width:55px;">Price($)
        <input id="thePrice" size="6" maxlength="7" name="price" style="width:50px" value="7" />
    </td>
    <td style="width:40px;">Tax(%)
        <input id="theTax" size="4" maxlength="7" name="tax" style="width:35px" value="7" />
    </td>
    <td style="width:40px;">Down($)
        <input id="theDown" size="5" maxlength="7" name="downPmt" style="width:35px" value="7" />
    </td>
    <td style="width:40px;">Trade($)
        <input id="theTrade" size="5" maxlength="7" name="trade" style="width:35px" value="7" />
    </td>
    <td style="width:78px;">Est.Monthly Pmt
        <input id="theResult" size="7" maxlength="7" name="result" style="width:75px" value="0" />
    </td>
    </tr>
</table>
<button type="button" id="calculate">Add Boxes!</button>
</div>
like image 411
HelloWorld Avatar asked Mar 15 '14 03:03

HelloWorld


People also ask

Why are my numbers concatenating instead of adding?

The + sign concatenates because you haven't used parseInt(). The values from the textbox are string values, therefore you need to use parseInt() to parse the value.

Can you concatenate numbers in JavaScript?

To concatenate two numbers in JavaScript, add an empty string to the numbers, e.g. "" + 1 + 2 . When using the addition operator with a string and a number, it concatenates the values and returns the result.

Can we concatenate string and number in JavaScript?

In javascript the "+" operator is used to add numbers or to concatenate strings. if one of the operands is a string "+" concatenates, and if it is only numbers it adds them.

Can we concat number and string?

To concatenate a String and some integer values, you need to use the + operator. Let's say the following is the string. String str = "Demo Text"; Now, we will concatenate integer values.


2 Answers

Change line and apply parseInt to each obj as follow

var theResult = parseInt(theTerm) + parseInt(theRate) + parseInt(thePrice) + parseInt(theTax) + parseInt(theDown) + parseInt(theTrade);
like image 153
joker Avatar answered Oct 19 '22 23:10

joker


Instead of using parseInt, you can multiply number by 1. Its much faster and easier method to covert datatype.

$(document).ready(function () {
    var theTerm = $("#theTerm").val() * 1;
    var theRate = $("#theRate").val() * 1;
    var thePrice = $("#thePrice").val() * 1;
    var theTax = $("#theTax").val() * 1;
    var theDown = $("#theDown").val() * 1;
    var theTrade = $("#theTrade").val() * 1;
    var theResult = theTerm + theRate + thePrice + theTax + theDown + theTrade;

    $("#calculate").click(function () {
        alert(theResult);
        alert(typeof (theResult));
    });
});

JSFiddle: http://jsfiddle.net/RqzPk/14/

like image 42
Mohit Pandey Avatar answered Oct 20 '22 00:10

Mohit Pandey