• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript Can this glitch be fixed?

Omnomc

New Coder
So, I've been trying to make a cookie clicker game, and it is all working perfectly, until I tried saving and loading.
Basically, saving and loading works perfectly, but after when I click the cookie (let's say I have 8), it goes 81, 811, 8111, 81111, and when I make it so that it you get 2 cookies per click instead of 1, it goes 82, 822, 8222.
Here is the script:
Code:
<!DOCTYPE html>
<html>
<head><title>hey</title>
<script>
var Cookies = 0
var autoClickers = 0
var loopRunningAutoClickers = false
var bakers = 0
var loopRunningBakers = false
var farms = 0
var loopRunningFarms = false

function save() {
   localStorage.cookies = Cookies
}
function load() {
   Cookies = localStorage.cookies
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
}
function addCookie() {
   Cookies += 1
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
}
 
function autoClickerBrought() {
   if (Cookies > 49) {
   Cookies -= 50
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
      autoClickers += 1
      if (loopRunningAutoClickers == false) {
         autoClickerLoop()
      }
      loopRunningAutoClickers = true
   }
}
function autoClickerLoop() {
   loopRunningAutoClickers = true
   Cookies += (autoClickers * 1)
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
   setTimeout(autoClickerLoop, 1000)
}
function bakerBrought() {
   if (Cookies > 1999) {
      Cookies -= 2000
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
      bakers += 1
      if (loopRunningBakers == false) {
       bakerLoop()
      }
      loopRunningBakers = true
   }
}
function bakerLoop() {
   loopRunningBakers = true
   Cookies += (bakers * 30)
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
   setTimeout(bakerLoop, 1000)
}
function farmBrought() {
   if (Cookies > 350000) {
      Cookies -= 350000
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
      farms += 1
      if (loopRunningFarms == false) {
         farmLoop()
      }
      loopRunningFarms = true
   }
}
function farmLoop() {
   loopRunningFarms = true
   Cookies += (farms * 2000)
   if (Cookies < 1000) {
      document.getElementById("CookieCounter").value = Cookies + " Cookies!"
   }
   if (Cookies >= 1000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000).toFixed(3).replace(/\.0$/, ' ') + "K Cookies!"
   }
   if (Cookies >= 1000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000).toFixed(3).replace(/\.0$/, ' ') + "M Cookies!"
   }
   if (Cookies >= 1000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000).toFixed(3).replace(/\.0$/, ' ') + "B Cookies!"
   }
   if (Cookies >= 1000000000000) {
      document.getElementById("CookieCounter").value = (Cookies / 1000000000000).toFixed(3).replace(/\.0$/, ' ') + "T Cookies!"
   }
   setTimeout(farmLoop, 1000)
}
</script>
<style>
</style>
</head>
<body>
<input type="button" id="CookieCounter" value="0 Cookies!">
<input type="button" value="Save" onclick="save()">
<Input type="button" value="Load" onclick="load()">
<img src="Cookie.jpg" onclick="addCookie()">
<input type="button" value="Auto-Clicker. Cost: 50 Cookies, + 1 Cookies Per Second" onclick="autoClickerBrought()">
<input type="button" value="Baker. Cost: 2,000 Cookies, + 30 Cookies Per Second" onclick="bakerBrought()">
<input type="button" value="Farm. Cost: 250,000 Cookies, + 2,000 Cookies Per Second" onclick="farmBrought()">
</body>
</html>
Any help is unbelievably appreciated!
 

OldMan

Coder
The += operator makes no distention between a number or a string. You have to do that with Number(). A better way is to use
++Cookies; to do that automatically.

FYI: use the semicolon to end 'ALL' lines in js - it will keep you out of trouble.
 

Top