+ 1
why undefined?
9 Respostas
+ 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"
+ 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
+ 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
0
Airree I updated the code please do check it because it still doesn't work
0
Umm i don't see that you updated it
0
Airree Please can you edit the code and share the link with me? Because I tried that before it didn't work! Thanks 🙏🙇
0
Thank you so much please explain the me the x you pasted in the function please
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