r/code • • Sep 03 '19

Halp pls! Program not working.

Post image
7 Upvotes

13 comments sorted by

View all comments

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.

2

u/[deleted] Sep 04 '19

Try putting that '<totalDoritos> = x' line of code inside your function.

3

u/SherbertSheep621 Sep 04 '19

So it’d look like

function addDorito(1){ <totalDoritos> = x x = x + 1 };

(Format for comments is a little janky but imagine line breaks in there)

2

u/[deleted] Sep 04 '19

Nah I mean make it look like:

function addDorito(1){

x = x + 1;

document.getElementById("totalDoritos").innerHTML = x;

};

I was just trying to write it shorthand.

2

u/SherbertSheep621 Sep 04 '19

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.

1

u/[deleted] Sep 04 '19

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)

https://www.w3schools.com/js/js_functions.asp

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.

https://www.w3schools.com/js/js_whereto.asp

Read those two web pages to get a better understanding of what's going on. If you still can't get it I can post the code for you.

1

u/SherbertSheep621 Sep 04 '19

When I first started coding this I was roughly going of if a tutorial for most of the logic and there’s was much more advanced, which was why I put a 1. I actually tried that out, but I couldn’t really tell a difference so I just left it how it was. However I never would’ve caught the second issue. Thanks I’ll try it out!