0

So I'm looking to create a really simple function that +=1 on a number variable and also saves so that which ever number users get to it stores. My current code appears to be adding 1 on as a string each time. Can somebody help me understand where I'm going wrong?

document.cookie = "balance = 0"
var balance = document.cookie;

var moneyup = () => {
document. cookie = balance +=1;
document.getElementById("moneybox").innerHTML = "Money: £"+balance;
}
  • 1
    First you need to get the value of the `balance` cookie, because `document.cookie` can contain multiple cookies. See https://stackoverflow.com/questions/10730362/get-cookie-by-name – Barmar May 28 '20 at 20:13
  • 1
    Then you need to convert the cookie to a number with `parseInt()` so you can increment it numerically instead of concatenating. – Barmar May 28 '20 at 20:14
  • 1
    Then you need to save the new cookie with `document.cookie = "balance=" + newvalue` – Barmar May 28 '20 at 20:14

0 Answers0