0
Help me debug this, thanks.
/*please help me debug this, x+y isn't adding both but concentates them unlike the others, also <br> tags aren't working, thanks.*/ var x; var y=5; var x=prompt("what is x?"); var z= x + y; var a=x*y; var b=x-y; y document.write(z);"</br>" document.write(a);"</br>" document.write(b); https://code.sololearn.com/Wkb87MP3Hjvd/?ref=app
3 ответов
+ 2
In JS, Plus (+) is used as concat operater, by default if you add two numbers they will be parsed to strings and will be concatenated, in order to add two values you need to parse them
var x;
var y = 5;
var x = prompt("what is x?");
var z = parseFloat(x) + parseFloat(y);
var a = x * y;
var b = x - y;
document.write("Addition: " + z);
document.write("</br>");
document.write("Multiplication: " + a);
document.write("</br>");
document.write("Substraction: " + b);
+ 1
Here is the simplified example https://code.sololearn.com/Wz0Yh2dqVtvX#js
+ 1
wow, thanks I never knew about the parse property in js, we do of a truth learn everyday, and seems solo learn didn't mention it. and also the br tag now works, thanks.