This question already has answers here:
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
Closed 1 year ago.
if x,y,z are all 1 its giving the answer 111 rather than 3
x=prompt ("Enter first Number");
y=prompt ("Enter second Number");
z=prompt ("Enter third Number");
p= x + y + z ;
You could use something like this
x = parseInt(prompt("Enter first Number"), 10);
y = parseInt(prompt("Enter second Number"), 10);
z = parseInt(prompt("Enter third Number"), 10);
p = x + y + z;
console.log(p)
Related
This question already has answers here:
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
Closed 2 years ago.
I just want to add two numbers in javascript using prompt but the output comes as a string.
var num1 =prompt("enter a number");
var num2=prompt("enter a number");
var sum =num1+num2;
console.log(`the of ${num1} and ${num2} ${sum} `);
The return type of prompt is string so it is needed to convert string to number to do sum operation.
var num1 = Number(prompt("enter a number"));
var num2 = Number(prompt("enter a number"));
var sum =num1 + num2;
console.log(`the of ${num1} and ${num2} ${sum} `);
This question already has answers here:
Sum of two numbers with prompt
(10 answers)
Adding two numbers concatenates them instead of calculating the sum
(24 answers)
How to force JS to do math instead of putting two strings together [duplicate]
(11 answers)
How to add two strings as if they were numbers? [duplicate]
(20 answers)
Closed 4 years ago.
I am working on a geometry style like calculator using javascript, and I need user input. But when the user, for example, inputs 55 and the other also 55 the sum of the number will be 5555 when I'd like it to be 110.
Javascript Code:
function ftmad2(){
let angle1 = prompt("Please enter the first angle:");
let angle2 = prompt("Please enter the second angle:");
sumAngle = angle1 + angle2;
console.log(sumAngle);
let result = 180-sumAngle;
document.getElementById("result").innerHTML = result;
}
Common issue; it is reading 55 as a string, rather than a number, do:
sumAngle = parseInt(angle1, 10) + parseInt(angle2, 10);
Current senerio
"55"+"55" //which will return 5555 obviously
Use parseInt to convert string to int because your both value angle1 and angle2 coming in string format, you should convert into int before sum.
required senerio
55 + 55 //which will return 110
function ftmad2(){
let angle1 = prompt("Please enter the first angle:");
let angle2 = prompt("Please enter the second angle:");
angle1 = parseInt(angle1);
angle2 = parseInt(angle2);
sumAngle = angle1 + angle2;
console.log(sumAngle);
let result = 180-sumAngle;
document.getElementById("result").innerHTML = result;
}
ftmad2();
<p id="result"></p>
prompt() returns a string.
When you do angle1 + angle2 you're joining two strings instead of summing two numbers.
In order for this to work, you'll have to transform the strings in numbers first. Like this:
function ftmad2(){
let angle1 = prompt("Please enter the first angle:");
let angle2 = prompt("Please enter the second angle:");
sumAngle = parseFloat(angle1) + parseFloat(angle2);
console.log(sumAngle);
let result = 180-sumAngle;
document.getElementById("result").innerHTML = result;
}
ftmad2();
<div id="result"></div>
This question already has answers here:
Pad a number with leading zeros in JavaScript [duplicate]
(9 answers)
How can I pad a value with leading zeros?
(76 answers)
Closed 5 years ago.
Suppose,
a=0;
then the result should be 00
a=10
result=10
a=2
result=02
like all values needs to round in 2 decimal point.
Note: No need to round the values having more than 2 digits.
Are you looking for something like that;
var int = 3;
var intStr = ("0" + int).slice(-2);
Output : 03
For any number of digits
var temp = 9;
if(temp < 10){
var temp = ("0" + temp).slice(-2);
}
For only two digit simply append zero if it is one digit number :-
var temp = 19;
if(temp < 10){
var temp = "0" + temp;
}
This question already has answers here:
How to round to at most 2 decimal places, if necessary
(91 answers)
Closed 7 years ago.
I am trying to limit the returned number to be only 2 decimal places but this code isn't working for me;
function myFunction() {
var x = document.getElementById("mySelect").value;
document.getElementById("demo").innerHTML = "Result is: " + x * 1.09; value = valToRound.toFixed(2);
}
What am I doing wrong?
Typing in the JS Browser console
x = 2.71828
x.toFixed(2)
"2.72"
it is clear that .toFixed(2) works
What you did wrong was rounding after printing the answer, and not using the correct variables.
document.getElementById("demo").innerHTML = "Result is: " + x * 1.09; value = valToRound.toFixed(2);
It is also a good idea to get in the habit of converting strings to numbers with parseFloat(). In JS, '2'*'2' is '4' but '2'+'2' is '22', unless you first convert to number.
If you do it this way it will work:
function myFunction() {
var x = parseFloat(document.getElementById("mySelect").value);
var valToRound = x * 1.09;
var value = valToRound.toFixed(2);
document.getElementByID("demo").innerHTML = "Result is: " + value;
}
This question already has answers here:
How to convert a string to an integer in JavaScript
(32 answers)
Closed 9 years ago.
I was trying to do some simple mathematical calculations in HTML and jQuery and JavaScript, so I wanted to get input from user.
For input I tried doing this :
var x = prompt("Enter a Value","0");
var y = prompt("Enter a Value", "0");
But I am not able to perform any kind of calculations as these values are strings.
Please, can any one show me how to convert them into integers.
parseInt() or parseFloat() are functions in JavaScript which can help you convert the values into integers or floats respectively.
Syntax:
parseInt(string, radix);
parseFloat(string);
string: the string expression to be parsed as a number.
radix: (optional, but highly encouraged) the base of the numeral system to be used - a number between 2 and 36.
Example:
var x = prompt("Enter a Value", "0");
var y = prompt("Enter a Value", "0");
var num1 = parseInt(x);
var num2 = parseInt(y);
After this you can perform which ever calculations you want on them.
JavaScript will "convert" numeric string to integer, if you perform calculations on it (as JS is weakly typed). But you can convert it yourself using parseInt or parseFloat.
Just remember to put radix in parseInt!
In case of integer inputs:
var x = parseInt(prompt("Enter a Value", "0"), 10);
var y = parseInt(prompt("Enter a Value", "0"), 10);
In case of float:
var x = parseFloat(prompt("Enter a Value", "0"));
var y = parseFloat(prompt("Enter a Value", "0"));
var xInt = parseInt(x)
This will return either the integer value, or NaN.
Read more about parseInt here.
You can use parseInt() but, as mentioned, the radix (base) should be specified:
x = parseInt(x, 10);
y = parseInt(y, 10);
10 means a base-10 number.
See this link for an explanation of why the radix is necessary.
Working Demo Reading more Info
parseInt(x) it will cast it into integer
x = parseInt(x);
x = parseInt(x,10); //the radix is 10 (decimal)
parseFloat(x) it will cast it into float
Working Demo Reading more Info
x = parseFloat(x);
you can directly use prompt
var x = parseInt(prompt("Enter a Number", "1"), 10)
You have to use parseInt() to convert
For eg.
var z = parseInt(x) + parseInt(y);
use parseFloat() if you want to handle float value.