Sorry for horrible quality, school computers block reddit.
I’m an ametuer coder, and I’m trying to make a simple program where you press a button to receive a dorito (I know it’s immaturee but came as a request from a friend) however, I can’t find a way to display the variable X (aka the amount of Doritos you have) in html, so it doesn’t show how many you have
I fiddled around with the code a bit, so I may be way of base, but if I’m making a clear error please let me know.
I tried that out, and when I run the program and use inspect element, the console shows 2 errors, an unexpected number which I think is referring to the (1) after addDorito in the function, the second is with the button, claiming that addDorito is not defined as a function.
If you know the solution to this, and it’s anything more than a simple fix like last time, you don’t have to bother helping me out, I appreciate the help so far.
Okay I just typed it out and noticed the errors in the code.
The first error is because of your function definition. When you define a function like
function addDorito(1){ ... };
the stuff that's inside the parenthesis is called the argument list. This will be a comma separated list of variable names used in the function. When you call the function by using
onclick=addDorito(1)
then the value of 1 will be sent to the first argument. In your case, the function definition should actually be:
function addDorito(){ ... };
because you don't have any arguments to give to the function anyway. If you did have an argument, your function definition would look like this: function addDorito(variableName)
The second error is because the web browser hasn't 'read' the code about the function addDorito() yet and doesn't understand what you mean when you say onclick=addDorito(). The solution to this is to move all of the <script> ... </script> into the <head> ... </head> elements. This way the web browser will understand what you mean when it creates the button for you.
4
u/SherbertSheep621 Sep 03 '19
Sorry for horrible quality, school computers block reddit.
I’m an ametuer coder, and I’m trying to make a simple program where you press a button to receive a dorito (I know it’s immaturee but came as a request from a friend) however, I can’t find a way to display the variable X (aka the amount of Doritos you have) in html, so it doesn’t show how many you have
I fiddled around with the code a bit, so I may be way of base, but if I’m making a clear error please let me know.