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

1st Nov 2017, 4:16 PM
Kylia Baumann
Kylia Baumann - avatar
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! */
1st Nov 2017, 4:27 PM
Dev
Dev - avatar
+ 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...
1st Nov 2017, 6:26 PM
Dev
Dev - avatar
+ 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) }
1st Nov 2017, 7:03 PM
Dev
Dev - avatar
+ 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
1st Nov 2017, 5:33 PM
Kylia Baumann
Kylia Baumann - avatar
+ 1
Thanks ^^
1st Nov 2017, 4:28 PM
Kylia Baumann
Kylia Baumann - avatar
+ 1
Yes but the problem my when I change the operator (whit -) He said the result of the +
1st Nov 2017, 6:04 PM
Kylia Baumann
Kylia Baumann - avatar
+ 1
Ok ^^ Good night
1st Nov 2017, 6:28 PM
Kylia Baumann
Kylia Baumann - avatar
+ 1
Thanks man I gona put a credit for you! ;)
1st Nov 2017, 8:29 PM
Kylia Baumann
Kylia Baumann - avatar