Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

115
Views
JavaScript local storage value NaN

My problem is the JavaScript Error NaN. Everything works perfectly in my LocalHost (file: ///), but as soon as I upload it, it says "NaN" everywhere. I don't understand what could be the cause, this is my first project with JavaScript.

I left out a lot of the HTML code, but that is also explained in the JavaScript.

HTML:

<div class="coinsdisplay">
    <input id="coins" type="text" disabled name="" value="0" class="coinInput">
</div>
<div class="cps">
    <p id="cps">0 Coins/s</p>
</div>

<script src="public/js/main.js"></script>

<script>
  window.onload= loadGame();
</script>

JS:

var coincount = 0;
var autoClick = 0;
var computer = 0;
var multiplier = 1;

// Single Click
function addCoin() {
  coincount = coincount + 1
  update()
}

// Updater
function update() {
  document.getElementById('coins').value = coincount;
  document.getElementById('cps').innerHTML = (((autoClick) + (computer*2))*multiplier) + " Coins/s";
  //AutoClicker
  document.getElementById('amountAutoClick').innerHTML = autoClick;
  document.getElementById('cosAutoClick').innerHTML = ((autoClick+1) * 12) + " Coins";
  //Computer
  document.getElementById('amountComputer').innerHTML = computer;
  document.getElementById('cosComputer').innerHTML = ((computer+1) * 15) + " Coins";
}

function timer() {
  coincount = coincount + autoClick;
  coincount = coincount + computer*2;
  update()
}
setInterval(timer, 1000)

// AutoClicker
function buyAutoClick() {
  if (coincount >= ((autoClick+1) * 12)) {
      coincount = coincount - ((autoClick+1) * 12);
      autoClick = autoClick + 1;
      update()
  }
}

// Computer
function buyComputer() {
  if (coincount >= ((computer+1) * 100)) {
      coincount = coincount - ((computer+1) * 50);
      computer = computer + 1;
      update()
  }
}

// SaveGame
function saveGame() {
  localStorage.setItem("coincount", coincount);
  localStorage.setItem("autoClick", autoClick);
  localStorage.setItem("computer", computer);
}
setInterval(saveGame, 1000)

function loadGame() {
  coincount = localStorage.getItem("coincount");
  coincount = parseInt(coincount);
  autoClick = localStorage.getItem("autoClick");
  autoClick = parseInt(autoClick);
  computer = localStorage.getItem("computer");
  computer = parseInt(computer);
  update()
}

I hope someone can help me, I wish you all a nice day.

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

do you call the function loadGame before the saveGame function? If so, the issue is probably that there is not yet anything saved in the localStorage. So localStorage.getItem("something") just returns null, and the parseInt() converts it into NaN. Make sure that you're localStorage is filled, or that you have default values like so (notice the || 0):

function loadGame() {
  coincount = localStorage.getItem("coincount") || 0;
  coincount = parseInt(coincount);
  autoClick = localStorage.getItem("autoClick") || 0;
  autoClick = parseInt(autoClick);
  computer = localStorage.getItem("computer") || 0;
  computer = parseInt(computer);
  update()
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!