+ 3
Storing input value as a number in a variable
What is wrong with it? const height = Number(document.queryselecter(".height").value); const weight= Number(document.queryselecter(".weight").value); const result = document.queryselecter(".result").addEventListener("click", function () { const output = weight / height ĂĂ2 }); It shows NaN!!!!!! Why? I've stored the input value in variables. Why should it show NaN???
7 Answers
+ 3
yeah like ODLNT mentioned. height and weight are set at the start of the program instead of during the click event. So they will get what ever value is on the output.
One possible solution could be to read the height and weight during the click event.
I replicated your issue with a potential fix here. Program consists of 2 buttons one with height/weight outside of click event and one inside click event with appropriate console.log:
https://code.sololearn.com/W5219C7kCP02/?ref=app
side note: console.error does nothing in sololearn web app :( so was stuck using console.log
+ 2
Javadev When I test it on Console.log while clicking the result btn, it is shown and then restarts and goes.
+ 2
Javadev I've got it. Now the problem is sth else. Why doesn't addEventListener work on sololearn?
+ 1
share the real project .
and you are not returning anything in that fn
+ 1
add debugs, i.e. console.error, or console.log and you should see what is off.
0
wierd. I can't seem to get your code to load. I keep getting internet connection error and when I click the link. It won't show me the code. I get 404 error when just pasting it in browser as well :/