0
Why a+b gain me ab not realy a+b.? If i enter value of a and be as 5 and 3 the answer show 53 not 8 why?
var a=prompt ("type first figure ", ""); var sign=prompt("type sign", ""); var b=prompt ("type last figure ", ""); if (sign=="+") { alert (a+b);} else if (sign=="-"){ alert ("answer is="+sub);} else if (sign=="*"){ alert ("answer is="+mul);} else if (sign=="÷"){ alert ("answer is="+div);} else if (sign=="^2"){ alert ("answer is="+sq);}
3 odpowiedzi
+ 5
Hi Suberna Das
Like Abhay said earlier, prompts return a string, not numbers, so you need to convert that number to string!
Here is how you can do that
pasreInt("the string goes here", "the number base to convert it to")
E.g
var a=prompt ("type first figure ", "");
var sign=prompt("type sign", "");
var b=prompt ("type last figure ", "");
a=parseInt(a, 10)
b=parseInt(b, 10)
if (sign=="+") {
alert (a+b);}
else if (sign=="-"){
alert ("answer is="+sub);}
else if (sign=="*"){
alert ("answer is="+mul);}
else if (sign=="÷"){
alert ("answer is="+div);}
else if (sign=="^2"){
alert ("answer is="+sq);}
Good luck on your coding adventure ☕ 🍩!
+ 2
Prompts returns a string not an integer ,and + results in concatenation of strings 5+3=53
0
Most thanks to all