I cant get this numbers get sum or substract or divided

im using this code to ask for 2 numbers a 1 sign and then make the operation but my code is just concatenate all.

let num3 = parseInt(prompt("Give me one number"));

let math = prompt("Give me a sign like + - / * ");
let num4 = parseInt(prompt("Give me 1 last number"));

let result2 = num3 + math + num4;
console.log(result2);
console.log(typeof result2);


tank you in advance!

A quick fix is to use the eval function:

let result2 = eval(num3 + math + num4);

Note that this is bad (potentially dangerous) practice when used in a context where any user can enter anything into your prompt.

A cleaner way is to use an if/else statement, or a switch:

let result2;

if (math === '+'){
    result2 = num3 + num4
} else if (math === '-'){
    result2 = num3 - num4
}
...and so on
2 Likes

tank you i will investigate what eval() do, and you right just wanted to make a simply calculator i will have on mind for the next time :v:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.