Coderslang_Master I cannot figure out what do you mean by the following code
state.updateProducerList = false;
There is no updateProducerList variable in the state object. We do have isProducerListUpdated.
I fail to understand your instructions above. Can you be a little more specific please?
Let me provide the handlers.js and functions.js files again. The main thing I can't figure out is that checkpoint 2 fails while 3 passes. Checkpoint 2: updateProducerList should be called once from handleKeyPress if isProducerListUpdated is false
Can you help me with analyzing why checkpoint 2 fails for me?
handlers.js
import { checkInitCompleted, updateGold, updateProducerList } from "./functions.js";
export const handleKeyPress = (term, state) => {
return (name, matches, data) => {
if (String.fromCharCode(data.code) === 'G' || String.fromCharCode(data.code) === 'g') {
state.gold = state.gold + 1;
} else {
for (let i = 0; i < state.producers.length; i++) {
if (String.fromCharCode(data.code) === `${state.producers[i].id}` && state.gold >= state.producers[i].cost) {
state.gold = state.gold - state.producers[i].cost;
state.producers[i].cost = state.producers[i].cost * state.producers[i].growthRate;
state.producers[i].count = state.producers[i].count + 1;
state.productionRate = state.productionRate + state.producers[i].baseProduction;
}
}
}
if (!state.isInitCompleted) {
for (let i = 0; i < state.producers.length; i++) {
if (state.gold >= state.producers[i].cost) {
checkInitCompleted(term, state);
state.isInitCompleted = true;
}
}
}
if (!state.isProducerListUpdated) {
updateProducerList(term, state);
}
};
};
export const handleStateChange = (term, state) => {
return () => {
updateGold(term, state);
};
};
functions.js
export const init = (term) => {
term('Welcome to the mining game!');
term.clear();
term.hideCursor();
term.grabInput();
};
export const updateGold = (term, state) => {
term.moveTo(25, 2);
term.eraseLineAfter();
term.bold.yellow(state.gold + ' ');
state.gold = state.gold + state.productionRate;
};
export const checkInitCompleted = (term, state) => {
term.moveTo(25, 2);
term.eraseLineAfter();
term(`You can purchase producers by clicking the number button (1, 2, 3, ...)`);
term(`GOLD: ${state.gold}`);
term(`PRODUCTION RATE: ${state.productionRate}`);
term.moveTo(25, 2);
term.eraseLineAfter();
term(`GOLD: ${state.gold}`);
term.moveTo(25, 2);
term.eraseLineAfter();
term(`PRODUCTION RATE: ${state.productionRate}`);
state.isProducerListUpdated = false;
};
export const updateProducerList = (term, state) => {
let i = 0;
let isOneZeroCountPrinted = false;
for (let j = 0; j < state.producers.length; j++) {
if (state.producers[j].count > 0 || (!isOneZeroCountPrinted && state.producers[j].count === 0)) {
term.move(1, 5 + i);
term(`${state.producers[j].title}: ${state.producers[j].count} | Production per second: ${state.producers[j].baseProduction.toFixed(1)} | Cost: ${state.producers[j].cost.toFixed(1)}`);
term.eraseLineAfter();
if (state.producers[j].count === 0) {
isOneZeroCountPrinted = true;
}
i = i + 1;
}
}
state.isProducerListUpdated = true;
};
Error Stack Trace
2. updateProducerList should be called once from handleKeyPress if isProducerListUpdated is false
4. updateProducerList should start printing at the beginning of the fifth line by calling moveTo(1, 5)
5. handleKeyPress arguments should be forwarded into updateProducerList
7. updateProducerList should print out the producer information in expected format using a single string
8. updateProducerList should print out all the producers where the count is greater than 0
9. updateProducerList should print one extra producer with the 0 count and next cost value
10. updateProducerList should set the flag isProducerListUpdated to true once it finishes the update