Skip to content Skip to sidebar Skip to footer

How To Make 1 + 1 = 2 Instead Of 1 + 1 = 11

I am trying to add the number mathematically, but it keeps adding the number after it. It takes the id number (begen), then it gets the number inside another div (kacbegen). var

Solution 1:

Convert it to number via adding a +:

var toplam = (+kacbegen + 1);

Unary plus (+)

The unary plus operator precedes its operand and evaluates to its operand but attempts to converts it into a number, if it isn't already.

Solution 2:

It looks like you're working with Strings (and thus a + b is the concatenation operator) when you want to be working with Number (so x + y would be addition)

Perform your favorite way to cast String to Number, e.g. a unary +x

var kacbegen = +$("#math" + begen).text();

Solution 3:

You need to use parseInt to convert kacbegen, which is a String instance, to a Number:

var begen = $(this).attr('id');
var kacbegen = $("#math" + begen).text();
var toplam = (parseInt(kacbegen) + 1);

alert(toplam);

The + operator, when used with a String on either side, will serve as a concatenation, calling Number.prototype.toString on 1.

Solution 4:

You need to cast the contents to a number:

var contents = $("#math" + begen).text();
var kacbegen = parseFloat(contents);

Solution 5:

You use kacbegen as a string. Please use as a integer use parseInt(kacbegen) + 1

Post a Comment for "How To Make 1 + 1 = 2 Instead Of 1 + 1 = 11"