+ 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???

1st Jan 2021, 6:49 PM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
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
2nd Jan 2021, 7:03 AM
Javadev
+ 2
Javadev When I test it on Console.log while clicking the result btn, it is shown and then restarts and goes.
1st Jan 2021, 9:03 PM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
+ 2
Javadev I've got it. Now the problem is sth else. Why doesn't addEventListener work on sololearn?
2nd Jan 2021, 11:24 AM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
2nd Jan 2021, 11:39 AM
Saleh Ghazimoradi
Saleh Ghazimoradi - avatar
+ 1
share the real project . and you are not returning anything in that fn
1st Jan 2021, 7:07 PM
Med Amine Fh
Med Amine Fh - avatar
+ 1
add debugs, i.e. console.error, or console.log and you should see what is off.
1st Jan 2021, 7:40 PM
Javadev
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 :/
2nd Jan 2021, 4:58 PM
Javadev