How to force JS to do math instead of putting two strings together
You have the line
dots = document.getElementById("txt").value;
in your file, this will set dots to be a string because the contents of txt is not restricted to a number.
to convert it to an int change the line to:
dots = parseInt(document.getElementById("txt").value, 10);
Note: The 10
here specifies decimal (base-10). Without this some browsers may not interpret the string correctly. See MDN: parseInt
.
Javascript (+) sign concatenates instead of giving sum of variables
Use this instead:
var divID = "question-" + (i+1)
It's a fairly common problem and doesn't just happen in JavaScript. The idea is that +
can represent both concatenation and addition.
Since the + operator will be handled left-to-right the decisions in your code look like this:
"question-" + i
: since"question-"
is a string, we'll do concatenation, resulting in"question-1"
"question-1" + 1
: since"queston-1"
is a string, we'll do concatenation, resulting in"question-11"
.
With "question-" + (i+1)
it's different:
- since the
(i+1)
is in parenthesis, its value must be calculated before the first+
can be applied:i
is numeric,1
is numeric, so we'll do addition, resulting in2
"question-" + 2
: since"question-"
is a string, we'll do concatenation, resulting in"question-2"
.
Adding two numbers concatenates them instead of calculating the sum
They are actually strings, not numbers. The easiest way to produce a number from a string is to prepend it with +
:
var x = +y + +z;
javascript (+) sign concatenates instead of giving sum?
Here value
gives you a string, hence the concatenation. Try parsing it as an Number instead:
var sum = parseInt(numberOne) + parseInt(numberTwo);
See the demo fiddle.
How to add two strings as if they were numbers?
I would use the unary plus operator to convert them to numbers first.
+num1 + +num2;
This is adding 2 strings when i am trying to add to intergers?
You need to use parseInt on no1
and no2
like follows:
var noO = prompt("please enter a number:");
var noT = prompt("please enter a second number:");
var no1 = noO;
var no2 = noT;
var res = parseInt(no1, 10) + parseInt(no2, 10);
alert("the adiition of thoes 2 numbers is: " + res);
Prompts when numbers are added together it just puts the number and the other number together
Common issue; it is reading 55 as a string, rather than a number, do:
sumAngle = parseInt(angle1, 10) + parseInt(angle2, 10);
Related Topics
Explain the Encapsulated Anonymous Function Syntax
Getting the Id of the Element That Fired an Event
How to Clone an Array of Objects in JavaScript
What's the Difference Between '$(This)' and 'This'
Escaping HTML Strings With Jquery
Explanation of 'Let' and Block Scoping With For Loops
How to Get the Browser Viewport Dimensions
What Is Destructuring Assignment and Its Uses
How to Get a Subset of a JavaScript Object'S Properties
Why Does Changing an Array in JavaScript Affect Copies of the Array
How to Detect If Multiple Keys Are Pressed At Once Using JavaScript
How to Pass Props to {This.Props.Children}
Should All Jquery Events Be Bound to $(Document)
How Does "This" Keyword Work Within a Function