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

22nd Sep 2020, 11:33 AM
RD:programmer
RD:programmer - avatar
3 Respuestas
+ 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 ☕ 🍩!
22nd Sep 2020, 11:47 AM
Vachila64☕
Vachila64☕ - avatar
+ 2
Prompts returns a string not an integer ,and + results in concatenation of strings 5+3=53
22nd Sep 2020, 11:35 AM
Abhay
Abhay - avatar
0
Most thanks to all
22nd Sep 2020, 12:23 PM
RD:programmer
RD:programmer - avatar