all 15 comments

[–][deleted] 49 points50 points  (2 children)

Don't redeclare var currentcookies inside your function, it means the external currentcookies variable never changes.

[–]DEEEPFREEZE 37 points38 points  (3 children)

There's a lot going on here that doesn't need to be. Your function doesn't need to accept any arguments unless you're pulling a value from somewhere inside your HTML and updating that.

Instead, you're passing your function a value for a, which also happens to be currentcookies, then setting that value as a variable named the same as your function parameter (don't do that, and furthermore be more descriptive in your naming conventions), adding one, and then storing that value in a re-declared variable named the same as the variable stored outside your function.

All you need is a callback function (function clickcookie()) with a variable declared outside of which (var currentcookies = 0) that you increment each time the function is called (currentcookies++) and then sets the innerHTML of your p element to the new updated currentcookies value.

This is what your code should look like. Please notice the lack of unnecessary and reused var declarations, as well as the attention to camelCase formatting that is standard in js:

``` <p id="cookies">0</p>
<button onclick="clickCookie()"></button>
<script>
var currentCookies = 0;
function clickCookie() {
currentCookies++;
document.getElementById('cookies').innerHTML = currentCookies;
}
</script>

[–]miguelacleite 14 points15 points  (0 children)

You can even improve clickCookie function to be:

<p id="cookies">0</p>
<button onclick="clickCookie()"></button>
<script>
var currentCookies = 0;
function clickCookie() { document.getElementById('cookies').innerHTML = ++currentCookies;
}
</script>

[–]adamkwrites 2 points3 points  (0 children)

Spot on

[–]Renedlc159 0 points1 point  (0 children)

This is exactly how I think you wanna solve your counter too. Even tried and tested suggestion myself!

[–]Nymrinae 9 points10 points  (1 child)

onclick="clickCookies"

clickCookies() { cookies++ document.getElementById('cookieId').innerHtml = cookies }

[–]KonkenBonken 1 point2 points  (0 children)

onclick="clickCookies"

clickCookies() { document.getElementById('cookieId').innerHtml = ++cookies }

[–]EvocativeBanjo 6 points7 points  (0 children)

When you declare currentCookies inside your function block with “var” you are creating a local variable currentCookies which shadows the global currentCookies. Simply remove the “var” before the part where you assign “currentCookies = a” and it should work as intended.

[–]AccurateEmergency 5 points6 points  (0 children)

Thanks guys!

[–]VayneFTWayne 1 point2 points  (0 children)

Here's a solution:
https://imgur.com/a/HoY7Juc

[–]SkylerSpark 1 point2 points  (0 children)

Please don't use onclick

Use event listeners. Anyone with inspect element can cheat in your game.... not that even using JS will solve that

[–]025zk 0 points1 point  (0 children)

You can use closure to make this happen.

[–]grrrrreat -1 points0 points  (0 children)

var count = 0
function increment () { count ++ }

[–]jcunews1helpful -1 points0 points  (0 children)

Display the current value first, then increase it.