Save and parseInt user input in one line of code? (Javascript) [duplicate] - javascript

This question already has answers here:
Why is parseInt() not working? [duplicate]
(4 answers)
Closed 3 years ago.
Here's my code:
function add() {
var num1 = prompt("Enter 1st number")
var num2 = prompt("Enter 2nd number")
parseInt(num1);
parseInt(num2);
var result = num1 + num2;
alert(result);
}
add();
I'm trying to build a simple addition calculator. parseInt wasn't working and an answer here said to declare the variable like var num1 = parseInt(num1);. However since I'm only getting "num1" through user input (var num1 = prompt..."), not sure how to store it as integer, or parseInt, in the same line. Any advice? Thanks.

All you have here are standalone, unused expressions:
parseInt(num1);
parseInt(num2);
Those evaluate to numbers, but you don't use them, so they're useless. Either assign them to variables, eg
const actualNum1 = parseInt(num1);
const actualNum2 = parseInt(num2);
and then use those variables, or just wrap the prompt in parseInt:
var num1 = parseInt(prompt("Enter 1st number"))
var num2 = parseInt(prompt("Enter 2nd number"))
Unless you're intending to only accept integers, consider using Number instead:
var num1 = Number(prompt("Enter 1st number"))
var num2 = Number(prompt("Enter 2nd number"))

Related

Need help continuing a function in java in appstudio

i have a set of radio buttons, each holding a number as their value (odd/even) and upon clicking a button an output is shown in a label and have managed to do that much, but now i am not sure how to reference the value of the button to output in the label this message:'mary the number [number] is [odd/even]'
this image shows this code:
let num1 = 176
let num2 = 345
let num3 = 1037
let num4 = 3421
let num5 = 2289
let num6 = 3044
rdoOddEven.onclick=function(){
if Number($"input[name=rdoOddEven]:checked").prop("value")%2==0
lblOddEven.className=''
lblOddEven.style.color='black'
lblOddEven.value = `Mary, the number ${$("input[name=rdoOddEven]:checked").prop("value")} is even`
else if Number($"input[name=rdoOddEven]:checked").prop("value")%2==1
lblOddEven.className=''
lblOddEven.style.color='black'
lblOddEven.value = `Mary, the number ${$("input[name=rdoOddEven]:checked").prop("value")} is odd`
}
First of all, check your syntax. JavaScript follows the core syntax of the C language. The if construct requires that you enclose the whole expression to be evaluated between (). And when you need to conditionally execute several lines, you must enclose them between {}.
You may compare this code with yours.
let num1 = 176
let num2 = 345
let num3 = 1037
let num4 = 3421
let num5 = 2289
let num6 = 3044
rdoOddEven.onclick=function() {
if (Number($"input[name=rdoOddEven]:checked").prop("value")%2==0) {
lblOddEven.className=''
lblOddEven.style.color='black'
lblOddEven.value = `Mary, the number ${$("input[name=rdoOddEven]:checked").prop("value")} is even`
}
else if (Number($"input[name=rdoOddEven]:checked").prop("value")%2==1) {
lblOddEven.className=''
lblOddEven.style.color='black'
lblOddEven.value = `Mary, the number ${$("input[name=rdoOddEven]:checked").prop("value")} is odd`
}
}

Output come as string [duplicate]

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} `);

Prompts when numbers are added together it just puts the number and the other number together [duplicate]

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>

Why toFixed() in Javascript acts like that? [duplicate]

This question already has answers here:
toFixed function not working properly ( please give a reason not an alternative)
(3 answers)
Why are floating point numbers inaccurate?
(5 answers)
Closed 5 years ago.
In this example/behaviour is something very strange.
Why does the function toFixed for the first two examples work and for this last one not?
//example 1
var num = 554.956;
var n = num.toFixed(2)
console.log(n);
var num2 = 554.955;
var n2 = num2.toFixed(2)
console.log(n2);
//output 554.96 and 554.96
//example 2
var num5 = 5.956;
var n5 = num5.toFixed(2)
console.log(n5);
var num6 = 5.955;
var n6 = num6.toFixed(2)
console.log(n6);
//output 5.96 and 5.96
//example 3
var num3 = 55.956;
var n3 = num3.toFixed(2)
console.log(n3);
var num4 = 55.955;
var n4 = num4.toFixed(2)
console.log(n4);
//output 55.96 and 55.95
Related to: php round vs javascript toFixed
EDIT: about the duplicates especially this one: toFixed function not working properly ( please give a reason not an alternative)
The answer is very good and helps me to understand the difference between x.955 and x.956, but doesn't answer why this happens only to the 55.955 in my example and not to the 5.955 or 554.955.

Unexpected output in javascript

I am beginner to javascript and i am getting unexpected output
here is the code
<script type="text/javascript">
function add(a,b)
{
x = a+b;
return x;
}
var num1 = prompt("what is your no.");
var num2 = prompt("what is another no.")
alert(add(num1,num2));
</script>
it should give output as a sum of two number entered by us on prompting but it is simply concatenating the two number and popping the output
This is because the prompt function returns a String and not a Number. So what you're actually doing is to request 2 strings and then concatenate them. If you want to add the two numbers together you'll have to convert the strings to numbers:
var num1 = parseFloat(prompt("what is your no."));
var num2 = parseFloat(prompt("what is another no."));
or simpler:
var num1 = +prompt("what is your no.");
var num2 = +prompt("what is another no.");
prompt returns a string, not a number. + is used as both an addition and concatenation operator. Use parseInt to turn strings into numbers using a specified radix (number base), or parseFloat if they're meant to have a fractional part (parseFloat works only in decimal). E.g.:
var num1 = parseInt(prompt("what is your no."), 10);
// radix -----^
or
var num1 = parseFloat(prompt("what is your no."));
When you prompt the user, the return value is a string, normal text.
You should convert the strings in numbers:
alert(add(parseInt(num1), parseInt(num2));
The return value of prompt is a string. So your add function performs the + operator on 2 strings, thus concatenating them. Convert your inputs to int first to have the correct result.
function add(a,b)
{
x = parseInt( a ) + parseInt( b );
return x;
}
In addition to the already provided answers: If you're using parseInt() / parseFloat(), make sure to check if the input in fact was a valid integer or float:
function promptForFloat(caption) {
while (true) {
var f = parseFloat(prompt(caption));
if (isNaN(f)) {
alert('Please insert a valid number!');
} else {
return f;
}
}
}
var num1 = promptForFloat('what is your no.');
// ...

Categories