Addition not functioning correctly [duplicate] - javascript

This question already has answers here:
Addition operation issues?
(5 answers)
Addition is not working in JavaScript
(7 answers)
Closed 7 years ago.
Somebody please help me. I'm not sure what I'm doing wrong here:
I'm trying to add up some numbers.
Instead the adding the 2 numbers it is just placing the 2 numbers next to each other; as in:
3 + 3 = 33 (instead of 6) !!!
If I replace the + with a * then it returns the correct result (9)
What is happening?
<p id="A">3</p>
<p id="B">3</p>
<p id="score"></p>
<script>
var AA = document.getElementById("A").innerHTML;
var BB = document.getElementById("B").innerHTML;
var result = AA + BB;
document.getElementById("score").innerHTML = result;
</script>

That's because they're strings so you're getting string concatenation. Convert them to numbers before performing addition.
AA = parseInt(AA, 10);
BB = parseInt(BB, 10);

What you are getting back from the html are strings, not numbers. You need to parse them as numbers first:
var AA = parseInt(document.getElementById("A").innerHTML, 10);
var BB = parseInt(document.getElementById("B").innerHTML, 10);
parseInt will convert the string to a number, and make your addition work correctly. The second parameter in the parseInt call is the radix, which will help prevent confusing mishaps when it comes to actually parsing the numbers

Related

Why does summation of string and number output a string but multiplication of them output a number in javascript? [duplicate]

This question already has answers here:
Why does JavaScript handle the plus and minus operators between strings and numbers differently?
(7 answers)
Closed last year.
string + number
const a = "5";
const b = 7;
console.log(a + b);
Output is -
57
string * number
const a = "5";
const b = 7;
console.log(a * b);
Output is -
35
it's based on what the creators of JS thought would be more useful. In every other language you can concatenate the strings, numbers and other stuff using the +. So in JS they thought that would be a nice solution as well. But then... what does it mean to multiply a string? The creators of JS had no answer, so assumed this must be a mathematical operation.

Multiply a string in JS [duplicate]

This question already has answers here:
Repeat a string in JavaScript a number of times
(24 answers)
Closed 4 years ago.
i want to display a string as many times I have a generated variable. Therefore I'd like to do something like that, which doesn't work
var shower_total = 7; // this gets generated, but for simplification...
var uhrzeit = "<p class='clock_item'>Foo</p>";
document.getElementById("uhrzeit_block").innerHTML =5*uhrzeit;
That's why I tried looping it but that doesn't work neither
document.getElementById("uhrzeit_block").innerHTML =
for(b=0, b <= shower_total; b++){
uhrzeit
};
What do I do wrong or what would be a possible - beginner-compatible - solution. Thanks!
You could use String#repeat instead of a multiplication of a string. This does not work for value who can not converted to a number.
var uhrzeit = "<p class='clock_item'>Foo</p>";
console.log(uhrzeit.repeat(5));

Javascript: Getting last few characters of a string [duplicate]

This question already has answers here:
How can I get last characters of a string
(25 answers)
Closed 5 years ago.
This is a trivial problem if we are storing string in a variable. But out of curiosity, I need to understand how to achieve this without storing string in variable?
// To get first few characters of a string, we can do something like this:
var x = "amangupta".substring(0,7); //amangup
// How to get last 7 characters
var x = "amangupta".substring(this.length - 7, this.length); // does not work, looking for similar approach
var str = "amangupta";
var x = str.substring(str.length - 7, str.length); // will work fine
How to get last 7 characters
Try
"amangupta".slice(-7)
Without an extra variable you need to use the string again
"amangupta".substring("amangupta".length - 7, "amangupta".length);

how to round a number to 2 decimal place? javascript [duplicate]

This question already has answers here:
Formatting a number with exactly two decimals in JavaScript
(32 answers)
Closed 7 years ago.
Im trying to round a number to 2 decimal place. I have tried the following but not having any luck? Can somebody please help me and tell me where im going wrong??
var winPercentage = totalWins/(totalWins+totalLost)*100;
winPercentage.toFixed(2);
document.getElementById('win-percentage').innerHTML = winPercentage + " %";
i search and tried this but to be honest i have no idea what it is?
var winPercentage = totalWins/(totalWins+totalLost)*100;
expr {double(round(100*winPercentage))/100}
document.getElementById('win-percentage').innerHTML = winPercentage + " %";
Try to use the following syntax instead and alter it to your needs
var num = 5.1;
num.toFixed(2); //will become 5.10
You had the right idea with toFixed(2). The problem is that it returns the formatted number, it does not alter the variable it was called on. In other words, you just need to assign it back the the variable you were using:
winPercentage = winPercentage.toFixed(2);

Variables and math operators [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Strange javascript addition problem
I know there's obviously a solution for this, and I've done it before, but I can't remember it and now I can't find it.
​<div>1</div>​​​​​​​​
$(function() {
var number = $('div').text();
var math = number + 2;
$('body').text(math);
});​
http://jsfiddle.net/G5zdx/
number is not being treated as an integer so math's value is "12" instead of "3". How can I correct this?
There are many ways, but
var math = (1 * number) + 2;
is a simple one. Whether you should be detecting possible ill-formed non-numbers depends on the nature of the rest of your code.
The parseInt() function is useful, but it probably should be called with 10 as its second argument to avoid interpreting numbers that begin with zero as octal constants instead of decimal. Also, parseInt() will not treat a string like "23skidoo" as an error, which may or may not be OK in your application.
​<div>1</div>​​​​​​​​
$(function(){
var number = parseInt($('div').text(), 10);
var math = number+2;
$('body').text(math);
});​
You can parse a string as an int using parseInt(string[, radix]).
Your code would look as follows
$(function(){
var number = $('div').text();
var math = parseInt(number, 10)+2;
$('body').text(math);
});
Source: http://www.javascripter.net/faq/convert2.htm

Categories

Resources