+ 2

Please why isn't this code working

please i have a JavaScript code here why doesn't it work https://code.sololearn.com/W6StyhDxdjhR/?ref=app

31st May 2018, 5:08 PM
King Frankostein®
King Frankostein® - avatar
7 odpowiedzi
+ 9
https://code.sololearn.com/Wzaybz42pl6K/?ref=app
31st May 2018, 5:23 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 5
Try the following: function print() { var metre=document.getElementById("metre").value; var result=metre/1000; var print=document.getElementById ("output").innerHTML=result; } Two things with the way you had it. 1) Uppercase 'V' on .value 2) I believe the JS was trying to read the value before the html had loaded, therefore the element didn't exist yet.
31st May 2018, 5:36 PM
Duncan
Duncan - avatar
+ 4
function print() { var metre=document.getElementById("metre").value; var result=metre/1000; var print=document.getElementById ("output").innerHTML=result; } ^Move your variables into your function. It's checking the value before it even has a value, so it'll always return undefined. As well, make it .value instead of .Value to correct the syntax error. That should do the trick.
31st May 2018, 5:28 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
tankz guys thats really helped me a lot😎😎😊
31st May 2018, 8:28 PM
King Frankostein®
King Frankostein® - avatar
+ 3
Me too Fata1 Err0r, I was working it out whilst you two posted. 3x the service here on SoloLearn 😎 At least I know my reasoning was right.
31st May 2018, 5:40 PM
Duncan
Duncan - avatar
+ 3
tankz guyz once more here is my full code https://code.sololearn.com/W6StyhDxdjhR/?ref=app
31st May 2018, 8:44 PM
King Frankostein®
King Frankostein® - avatar
+ 2
I was too slow. :D NGBrains has it typed up for you so you can see it in action.
31st May 2018, 5:29 PM
Fata1 Err0r
Fata1 Err0r - avatar