JSFiddle - React, Tailwind, and code Playground
HTML
<p id="output"></p>
<button id="btnCollect">collect wood</button>
JavaScript
window.addEventListener("DOMContentLoaded", function(){
var wood = +localStorage.getItem("woodSave");
wood = wood ? wood : 0;
var output = document.getElementById('output');
var btn = document.getElementById("btnCollect");
formatWood();
btn.addEventListener("click", collectWood);
function collectWood() {
wood +=1;
// You didn't have this in this function before, but
// you want to update your localStorage value when
// the wood value goes up.
localStorage.setItem("woodSave", wood);
// And, every time you click the button, you want the
// output to be not only updated, but also formatted:
formatWood();
}
function formatWood(){
// You don't want to update the wood variable to potentially
// contain any string characters. You only want to add those
// characters for formatting sake, so just add them to the
// output.
console.log( wood);
output.textContent = (wood >= 1000) ? (wood / 1000).toFixed(2) + "k": wood;
}
});