JavaScript Addition Operators not work - javascript

I try using javascript but the addition is not correct, live code here
function hitung2() {
var a = $(".a2").val();
var b = $(".b2").val();
c = a * b; //a kali b
d = c + a;
$(".c2").val(c);
$(".d2").val(d);
}

function hitung2() {
var a = parseInt($(".a2").val());
var b = parseInt($(".b2").val());
c = a * b; //a kali b
d = c + a;
$(".c2").val(c);
$(".d2").val(d);
}
Or if you going to work with fractions then use parseFloat() function in same way as it described above.

You need to change the string that you get from .val to ints with parseInt()
var a = parseInt($(".a2").val());
var b = parseInt($(".b2").val());
Updated fiddle. http://jsfiddle.net/b39Lz/32/

use parseInt()
var a = $(".a2").val();
var b = $(".b2").val();
temp1=parseInt(a);
temp2=parseInt(b);
c = temp1 * temp2;
d = c + temp1;

Your variables are String, so it uses String + operator, does concatenating the value. Convert string to int using: parseInt("10")
function hitung2() {
var a = $(".a2").val();
var b = $(".b2").val();
c = a * b; //a kali b
d = parseInt(c) + parseInt(a);
$(".c2").val(c);
$(".d2").val(d);
}

Avoid using parseInt() if you dont want to stick to integer calculations only.Try casting to general Number.
function hitung2() {
var a =Number( $(".a2").val());
var b = Number($(".b2").val());
var c = a * b; //a kali b
d =Number(c)+Number(a);
$(".c2").val(c);
$(".d2").val(d);
}

Related

After Addition two variables in javascript returning some garbage values

Hi I have two variables in JS Like:
var a = 223620.42
var b = 1200.1234
I am using Calculation Like:
var c = parseFloat(a) + parseFloat(b);
So the result should be = 224820.5434
But it returning 224820.54340000002
Please Suggest me what I am doing wrong here. Thanks in advance
Just Round-off the last Values. See the Code Below:
var a = 223620.42
var b = 1200.1234
var c = parseFloat(a) + parseFloat(b);
var numb = c.toFixed(4);
alert(numb);
hope it will solve you problem.
Try Using "toPrecision" method, and specify the desired length of the number.
var a = 223620.42
var b = 1200.1234
var c = parseFloat(a) + parseFloat(b);
c= c.toPrecision(3)

How to create variable with concatenation in javascript?

I have very long code and want to simplify it. I don't think so we can use string concatination to create different variable name. Is there any other way.
If(Something){
var a = some.Test1.x;
var b = some.Test1.y;
var c = some.Test1.z;
var d = some.Test1.p;
}
If(SomethingElse){
var a = some.Test2.x;
var b = some.Test2.y;
var c = some.Test2.z;
var d = some.Test2.p;
}
If(OneMore){
var a = some.Test3.x;
var b = some.Test3.y;
var c = some.Test3.z;
var d = some.Test3.p;
}
May be create some function like this, i know this is not right but anything similar to that.
function test(s){
a = some.s.x;
b = some.s.y;
c = some.s.z;
d = some.s.p;
}
You can use array-like syntax
function test(s) {
var a,b,c,d;
if (s === Something) {
test = "Test1";
}
if (s === SomethingElse) {
test = "Test2";
}
if (s === OneMore) {
test = "Test3";
}
a = some[test].x;
b = some[test].y;
c = some[test].z;
d = some[test].p;
}
This is a much improved version of the functionally equivalent, but never-to-be-used, eval syntax:
eval("a = some."+test+".x");
function test(index){
t = some["Test" + index];
a = t.x;
b = t.y;
c = t.z;
d = t.p;
}
and the execution:
test(1); test(2); test(3)

Counting a hrefs then subtracting

$(function() {
var getName = $('#fa_welcome').text();
var myName = getName.replace('Welcome ',"");
var a = $('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length;
var b = $('#recent_topics').find('a[href*="/u"]').length;
var c = a-b;
if(c <= 1) {
$('.topics_name').append('<div title="'+c+' New Post" id="newTops">'+c+'</div>');
}
});
Just for randomness I will give you the lengths of var a and b
a= 15
b= 25
I want to subtract these two as in var c
15-25
Though I get -25 parsed?
Any suggestions
Try with parseInt like
var a = parseInt($('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length);
var b = parseInt($('#recent_topics').find('a[href*="/u"]').length);
If you get these values then, use parseInt to convert it in integer,
var c = parseInt(a)-parseInt(b);
use parseFloat for getting result
$(function() {
var getName = $('#fa_welcome').text();
var myName = getName.replace('Welcome ',"");
var a = parseFloat($('#recent_topics').find('a[href*="/u"]').filter(':contains("'+myName+'")').length);
var b = parseFloat($('#recent_topics').find('a[href*="/u"]').length);
var c = a-b;
if(c <= 1) {
$('.topics_name').append('<div title="'+c+' New Post" id="newTops">'+c+'</div>');
}
});

Adding together values of jQuery.html()?

How would I add together the values pulled from two separate jQuery.html() calls? Example:
<div id="a">27</div>
<div id="b">3</div>
$(document).ready(function(){
var a = $("#a").html();
var b = $("#b").html();
var c = a + b;
});
All the above does is concatenate var a and b into c (i.e. c = 273) because a & b are strings. How do I get the actual values so that I can properly add them?
You can do either
var a = +$("#a").html();
or
var a = parseInt($("#a").html(), 10);
both of which cast the string to an integer. For more info, see How do I convert a string into an integer in JavaScript?.
Use the parseInt() function:
$(document).ready(function(){
var a = parseInt($("#a").html(), 10);
var b = parseInt($("#b").html(), 10);
var c = a + b;
});
You can use + in both operands
var c = +a + +b;
use text() instead of html().
var x = parseInt( $(el).text(), 10 );

JQuery multiply and add

Let's say I have some numbers that I want to multiply and add.
var a = 5;
var b = 10;
var c = 2;
var d = 3;
I want to sum b and c then multiply that by a, then add d. Easy right?
If if were a normal equation the formula would look like: (a * (b + c) + d)
But how do I do that in JQuery?
(Note: the reason for JQuery is that I'll be getting these numbers from fields and divs... and placing a total elsewhere, etc.)
By default script language does not know type as int or float. So you can fix that by multiplying 1 to the value you expect to be a number.
var a = 5;
var b = 10;
var c = 2;
var d = 3;
var total = a*1 * (b*1 + c*1) + d*1;
convert your value into float or integer first before you do calculation. Example:
var a = parseFloat($(this).val());
var b = parseInt($("#b").attr("data"));
var c = (a+10)*b;
$("#result").text(c.toFixed(2));
Just store the values in variables before you apply them to the equation:
var a = +$("#input1")[0].value;
var b = +$("#input2")[0].value;
var c = +$("#input3")[0].value;
var d = +$("#input4")[0].value;
$("#output")[0].value = a*(b+c) + d;
The plus sign before the jquery function is there to force the string value into a Number value.
Correct JQuery is 100% javascript.
Although, worth mentioning just use parseint() for the values that you get from text field
You can still do the calculation using normal javascript, just refer to the contents using jQuery selectors if necessary.

Categories