0
Why my code don't work?
Im just testing basic javascript but I don't understand what is wrong in this : https://code.sololearn.com/WPznMIGiv7H0/?ref=app https://code.sololearn.com/WPznMIGiv7H0/?ref=app
8 Answers
+ 10
function hey(){
var x = parseFloat(document.getElementById('in1').value);
var y = parseFloat(document.getElementById('in2').value);
var z = x + y;
alert(x+ "+" +y+ "=" +z);
}
/* The above code is an updated version of your JS code.
What's new:
⢠You have to use single or double quotes when writing the id (in JS while getting id of an HTML element).
⢠You should get the input value using the value statement to make calculations on it (otherwise it's null).
⢠The value which you get is in the form of string initially (which is not actually good to make calculations to), thus you use the parseFloat function to convert it to float.
Now, you can make any type of calculations on your numbers! */
+ 9
@Kyle: Possible solution -
'=' can't be used for comparison. Use '==' or '===' instead.
It's midnight here atm, will try to work on it in the morning...
+ 9
@Kyle: I think now's the time to get a better sleep, I've fixed everything! ^^
// Just take the value of the select tag!
var op = document.getElementById('op').value;
if(op == "+"){
var z = x + y;
alert(z)
}
else if(op == "-"){
var z = x - y;
alert(z)
}
else if(op == "*"){
var z = x * y;
alert(z)
}
else if(op == "/"){
var z = x/y;
alert(z)
}
+ 2
Another question.(sorry)
What is wrong in that when I put calc I said the 2 (+ and -)
https://code.sololearn.com/WWQmgg4L54Wq/?ref=app
+ 1
Thanks ^^
+ 1
Yes but the problem my when I change the operator (whit -) He said the result of the +
+ 1
Ok ^^ Good night
+ 1
Thanks man I gona put a credit for you! ;)