why undefined? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
5th May 2019, 1:16 PM
Glory222
Glory222 - avatar
9 Answers
+ 1
the x could be anything, at the call of the function it is "Num(this.innerHTML)" and the x is a way to easily refer to it in the code: "display.innerHTML += x"
5th May 2019, 4:36 PM
Airree
Airree - avatar
+ 2
The problem is that you defined the variables while the page was loading. You can solve this issue by putting the variables inside this: window.onload = function() { display = ... NU = ... } also note that you shouldn't use the 'var' keyword, because then the variable will only exist at the moment the page has loaded
5th May 2019, 3:53 PM
Airree
Airree - avatar
+ 1
The problem was, that you referred to it in the button as "Num(this)", which is already wrong, because you wanted just the innerHTML, so it should be like this: "Num(this.innerHTML)". In the function you made it like this: "function Num() { display.innerHTML += this.innerHTML }" its output was "undefined", because this.innerHTML referred to the functions innerHTML, which is obviously nothing. The code should be like this: "Num(x) { display.innerHTML += x; }" I also made the code more readable
5th May 2019, 4:33 PM
Airree
Airree - avatar
0
Airree I updated the code please do check it because it still doesn't work
5th May 2019, 4:11 PM
Glory222
Glory222 - avatar
0
Umm i don't see that you updated it
5th May 2019, 4:15 PM
Airree
Airree - avatar
0
Airree Please can you edit the code and share the link with me? Because I tried that before it didn't work! Thanks 🙏🙇
5th May 2019, 4:16 PM
Glory222
Glory222 - avatar
0
Thank you so much please explain the me the x you pasted in the function please
5th May 2019, 4:33 PM
Glory222
Glory222 - avatar
0
Thank you so much I feel so dumb here because I think I can't do what my fellow learners are doing! I've completed js course but still can't define a function correctly? I love coding but it's not just for me ☹️ Thank you again
5th May 2019, 4:36 PM
Glory222
Glory222 - avatar