How can I add 2 variables' numerical values together in Javascript - javascript

When I do:
var answer=a+b
if a was 4 and b was 5 then my answer comes out as 45. How can i get them to numerically add. I can do all other operations(*/-) but not add. Ik its a stupid question but im new and trying to lean
var prea,a,answer2,answer4,answer3,b,preb,answer1;
prea=document.getElementById("form1") ;
a=prea.elements["first"].value;
preb=document.getElementById("form1") ;
b=preb.elements["second"].value;
answer1=a*b;
answer2=a-b;
answer3=a/b;
answer4=a+b;
document.write("Multiplication:"+answer1);
document.write("<br>");
document.write("Subtraction:"+answer2);
document.write("<br>");
document.write("Division:"+answer3);
document.write("<br>");
document.write("Add:"+answer4);

Use Number(a) + Number(b) to calculate them. If you using strings instead of numbers, you just concatinate them instead of adding.
https://www.w3schools.com/jsref/jsref_number.asp

The variables are being declared as strings. You need to type cast them as an integer value to properly add them using parseInt(string).
The reason all the other operators work is because they will try to type juggle. However, javascript uses + for both numerical addition and string concatenation. So you have to explicitly use integer types if you want the result to be a summation.

Related

wanted to add two floating number which gets concatenated as string

i want to add two float number with fixed two decimal but its converted to string and get concatenated.I know its simple question but actually i'm in hurry
var a=parseFloat("15.24869").toFixed(2)
var b=parseFloat("15.24869").toFixed(2)
Update when i enter input as
var a=parseFloat("7,191");
var b=parseFloat("359.55");
c=(a+b).toFixed(2)
O/P:NAN
why so?
The .toFixed() method returns a string. Call it after you've performed the addition, not before.
var a=parseFloat("15.24869");
var b=parseFloat("15.24869");
var c=(a+b).toFixed(2);
After that, c will be a string too, so you'll want to be careful.
As to your updated additional question, the output is not NaN; it's 366.55. The expression parseFloat("7,191") gives the value 7 because the , won't be recognized as part of the numeric value.
Just add parenthesys to parse float the whole result string
var a=parseFloat((15.24869).toFixed(2));
var b=parseFloat((15.24869).toFixed(2));
c=a+b
doing c = a + b adds the two answers together. You might just want to turn them into a string then concatenate them.
var a=parseFloat("15.24869").toFixed(2)
var b=parseFloat("15.24869").toFixed(2)
var c = (a.toString() + b.toString());

I can't add two variables in javascript. It either ignores the second variable or it concatenates if I change the order of the variables to be added.

I can't add two variables in javascript. It either ignores the second variable or it concatenates if I change the order of the variables to be added.
var total = zbv[0] + zval + zter;
total = total.toFixed(2);
document.getElementById("ztotalvalue").innerHTML = total;
var nfa = Number(googForm.nfa.value);
alert(total);
alert(nfa)
var equity = nfa + total;
alert(equity);
.toFixed() return string, you can
var equity = nfa + +total // +total will cast string to number
Well your question is poorly worded. Second if something is being concatenated, than that must mean it's a string (so that answers your question). You need to convert to integer and clean up your code. To debug your code use console.log, open element inspector and go to 'console'.

how to use Javascript "document.getelementbyid" [duplicate]

This question already has answers here:
Why won't my inputs value sum?
(5 answers)
Closed 8 years ago.
i am trying to make a maths engine with JavaScript and HTML.
Here is (http://i.stack.imgur.com/NCEa4.jpg)
The alert from the js alert() function works but the output from it is wrong:
'HTMLObject'
Value of the input field is always a string. So when you use + operator with two strings it concatenates them. If you want to add two numbers you need first to convert strings to numbers. There are multiple ways to do it, for example:
var plus = parseInt(one) + parseInt(two);
Or you can use Number(one), or another unary + operator: +one + +two, but this might look confusing.
Use 'parseInt()' function to convert those values to integers first and then add the values.
make your variable plus like this:
var plus = parseInt(one) + parseInt(two);
You need to enclose your function code in curly braces { & }.
So Use:
function Maths(){
var one=document.getElementById("fid").value;
var two=document.getElementById("sid").value;
var plus=Math.parseInt(one)+Math.parseInt(two);
alert(plus);
}
Also use parseInt() to make data type conversion to convert to int in JavaScript.
Hope it'll help you. Cheers :)!!
Will be better if you use second argument in parseInt for be sure that value will be in decimal system.
function Maths() {
var one = document.getElementById("fid").value,
two = document.getElementById("sid").value,
plus = Math.parseInt(one, 10) + Math.parseInt(two, 10);
alert(plus);
}

Fill Javascript 2d Array with passed Variable

I have an 2d array created thats 50 by 2 and want to fill it with a passed array. I know the array works and the passed variables. But I can't get the passed variable to fill up the array, it just fills with plain text. Is my syntax wrong?
for (i=0; i <50; i++){
basket[i]=new Array(2);
}
function addtobasket(itemname, itemvalue){
basket[itemcount][itemcount]='itemname itemvalue;'
}
TIA!
'itemname itemvalue' will just fill the array with 'itemname itemvalue'
So you need to write:
basket[itemcount][itemcount]=itemname+' '+itemvalue;
Don't forget to put the semicolon AFTER the string.
for (i=0; i <50; i++){
basket[i]=new Array(2);
}
function addtobasket(itemname, itemvalue){
basket[itemcount][itemcount]= itemname + " " + itemvalue;
}
I believe that's what you want, assuming you're trying to get the items into the array in the format "itemname itemvalue" as in your example code.
The reason you're currently seeing the names of the variables in your array, rather than their values, is that you're using the string literal "itemname itemvalue". Anything within a string literal - that is, inside the quotation marks - is left unchanged when the code executes.

jQuery + parseDouble problem

I have an AJAX-request that returns a json object containing a few values with two decimals each, but since it's json these values are strings when returned. What I need to do is to perform addition on these values. Just a simple a+b = c, but they concatenate instead becoming ab.
I was hoping I could use parseDouble in jQuery just like I can use parseInt but apparantly I can't. At least not what I've found. So the question remains, is there any way I can add these two string values into a double or float value? Or should I just calculate this on the server side and send the already additioned value back to the browser and jQuery.
Example:
This is what happens
5.60 + 2.20 = 5.602.20
This is what should happen
5.60 + 2.20 = 7.80
Thankful for answers.
Just use parseFloat():
var c = parseFloat(a) + parseFloat(b);

Categories