This code is supposed to calculate the sum, substract, multiply, divide at the same time when the user types in x and y but it’s showing Nan
function perform(x, y) {
x = prompt("enter first no. ")
y = prompt(" enter second no. ")
function sum(x, y) {
console.log(parseInt(x + y));
}
sum();
function minus(x, y) {
console.log(parseInt(x - y));
}
minus();
function multiply(x, y) {
console.log(parseInt(x * y));
}
multiply();
function divide(x, y) {
console.log(parseInt(x / y));
}
divide();
}
2
Answers
Both X and Y are strings, when entered via the prompt dialog. You need to parse them separately before adding them, e.g.:
In addition, you are calling all the methods without providing the variable inputs, so instead of
sum()
, you should callsum(x,y)
prompt
will returnA string containing the text entered by the user, or null.
as stated in the mdn docs: https://developer.mozilla.org/en-US/docs/Web/API/Window/promptSo instead of
parseInt(x - y)
you should first parse each input into a integer (or float) and then do the addition, multiplication etc.and please read the so link commented by @Ivar