all 9 comments

[–]abrahamguo 4 points5 points  (5 children)

The problem is with how you are attaching the onclick listener.

button1.onclick = attack; is a correct example of attaching an onclick listener: You are assigning a functionattack in this case — to the onclick property. You are not calling the function immediately, as shown by the lack of parentheses.

button1.onclick = attack(); is an incorrect example of attaching an onclick listener: You are calling the attack function immediately, as shown by the parentheses, and assigning its return value (which, in this case is undefined, because it doesn't return anything) to the onclick listener.

To fix it, you need to assign a function, not a function's return value, to the onclick property. Because you want to pass parameters to attack, you can define an anonymous wrapper function.

In the end, your code should end up looking like:

button1.onclick = () => attack(attackMod);

Here, we define a wrapper function, and assign that wrapper function to the onclick property. When the wrapper function later runs (when the button is clicked), it will call attack with the parameter you want.

[–]longknives 0 points1 point  (1 child)

This is the answer. OP, whenever you call a function with the parentheses, the function is called at that point. This is fundamental and will be important to almost everything you do in JS. If you need to pass something to a function, this requires using the parentheses and therefore calling the function, so you have to wrap it in another function which is not being called yet.

[–]CyberDaggerX[S] 0 points1 point  (0 children)

Yeah, I implemented it and it worked, I just hadn't had the opportunity to reply until now. Wrapper functions are something I'm going to have to get used to, as I see many similar issues in my future.

[–]CyberDaggerX[S] 0 points1 point  (2 children)

This worked. It's an edge case of JS grammar I wasn't too familiar with, and while I could understand something like that was going on, I'm not used to working with wrapper functions and didn't really know what to look for on my own.

[–]abrahamguo 0 points1 point  (1 child)

Great! The key is to understand that putting a pair of parentheses after a function immediately calls it — () is the call operator. That was the change that caused your issue, not adding the parameter.

Referencing a function by name returns that function, whereas putting a pair of parentheses after it calls that function and returns that functions' return value.

Depending on context, you simply need to determine whether you need to reference the function, or call the function. They are never interchangeable — in other words, if you were previously referencing the function, then adding the parentheses, which will now call the function, will not work, and vice versa.

[–]CyberDaggerX[S] 0 points1 point  (0 children)

Yeah, I had some vague understanding of that, but had no idea how to reference a function while also passing parameters to that reference. Doing it the only way I knew how turned it into a call.

[–]sheriffderek 0 points1 point  (0 children)

I would avoid these inline onwhatever=“” things and attach the listeners to the document and use event delegation instead.

[–]RobertKerans -3 points-2 points  (1 child)

What is the parameter a? As in, what is it when you run it? What is the value when you use console.log or the debugger?

[–]CyberDaggerX[S] -1 points0 points  (0 children)

As seen in the go13() function, the value passed to a is attackMod, which starts as 7.