Finding Bugs within My JS Functions for CookieCrusher

I have a bug in my program that causes the Chip generation to increase exponentially, but I can’t seem to find it. Would you mind going through my code and telling me what’s wrong. You may need to try the game to see the problem for your self. It’ll make more sense then reading it on here


https://cookieclicker.killercl0wn.repl.co/

var chipOwned = 0;

const ChipProducer = {
  name: "Chip Producer",
  baseChipsPerSecond: 0.0,
  cost: 0,
  description: "+0.2 Chips/Second",
};

const MegaChipProcessor = {
	name: "Mega Chip Processor",
	baseChipsPerSecond: 0.0,
	cost:0,
	description: "+0.5 Chips/Second",
};


function chipProducer() {
  if (chipOwned >= ChipProducer.cost) {
    
    token = "true";
    chipOwned -= ChipProducer.cost;
    ChipProducer.cost = Math.round(ChipProducer.cost * 2);
		ChipProducer.baseChipsPerSecond += .2;
    document.getElementById("chipOwned").innerHTML = chipOwned;
    document.getElementById("chipProducer").innerHTML =
      ChipProducer.name +
      ":<br> Cost: " +
      ChipProducer.cost +
      ".<br> " +
      ChipProducer.description;

    generation(); // Start/restart the generation
  }
}

function megaChipProcessor() {
  if (chipOwned >= MegaChipProcessor.cost) {
    MegaChipProcessor.level += 1;
    token = "true";
    chipOwned -= MegaChipProcessor.cost;
    MegaChipProcessor.cost = Math.round(MegaChipProcessor.cost * 2);
		MegaChipProcessor.baseChipsPerSecond += .5;
    document.getElementById("chipOwned").innerHTML = chipOwned;
    document.getElementById("megaChipProcessor").innerHTML =
      MegaChipProcessor.name +
      ":<br> Cost: " +
      MegaChipProcessor.cost +
      ".<br> " +
      MegaChipProcessor.description;

    generation(); // Start/restart the generation
  }
}

function generation() {
	
  if (token === "true") {
    let totalChipGeneration = 0;
    let intervalID = setInterval(() => {
      let currentChipGeneration =
        ChipProducer.baseChipsPerSecond +
        MegaChipProcessor.baseChipsPerSecond;

      totalChipGeneration += currentChipGeneration;
      let chipsToAdd = Math.floor(totalChipGeneration);
      
      chipOwned += chipsToAdd;
      totalChipGeneration -= chipsToAdd;

      document.getElementById("chipOwned").innerHTML = chipOwned;

    }, 1000);
  }
}

You call generation in two separate functions, and within generation you have setInterval, so you likely have multiple intervals running at the same time. I don’t see a definition for token, and I’m also wondering why you’re using the string "true" instead of the boolean true. You might want to keep track of whether or not that interval is already running and see if that makes your clicker work as expected. :slight_smile:

1 Like

How would I do that?

Var token; i forgot to include that.

Well, a simple version could look something like this:

// store the interval (enables you to clear it later if neccessary)
let interval = null;
// remember if the interval is currently running or not
let intervalRunning = false;

// start the interval
function runInterval() {
	// if the interval is already running
	if (intervalRunning) {
		// exit
		return;
	}
	// remember that we are running the interval
	intervalRunning = true;
	// start the interval
	interval = setInterval(function() {}, 1000);
}

// stop the interval
function stopInterval() {
	// if the interval is not already running
	if (!intervalRunning) {
		// exit
		return;
	}
	// remember that we have stopped the interval
	intervalRunning = false;
	// stop the interval
	clearInterval(interval);
	// interval doesn't exist anymore
	interval = null;
}

Mind if I invite you to the program so you can see the whole picture and maybe walk me through it?
Thanks, by the way!

1 Like

Sure, I might not be able to right now, I’ve got a class soon and I needa do some work, but when I can I’ll try and help you out :slight_smile:

K you are invited to the program!

You’r pretty good at JS. I checked out your website and thought it was pretty good for the time spent

1 Like

Try it out now, I think it’s working the way you expect it to. I actually didn’t do what I suggested, instead I remove all calls to generation and called it immediately after it was defined (I also ignored token), so now when you purchase cookie producers etc it just adds to the base chips per sec of that object rather than starting another loop. What was happening was you were starting another loop each time any of the buttons were pressed and each loop was adding the full amount of base chips per second to the users chips per second so you were getting way more than expected. (That was a pretty rubish explanation, but hopefully you get the gist.) :slight_smile:

Yes I understood thanks for the help!

How would you feel about helping me create web games? I could use someone whos good with CSS and JS

I’m not great with CSS but I’d be happy to help :slight_smile:

K let’s talk in the program and not in the ask area

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.