Hello,
i have some problems with the updateProducerList function, here my code and the errors i get:
1) handlers.js:
import {
updateGold,
checkInitCompleted,
updateProducerList
} from './functions.js';
export const handleKeyPress = (term, state) => {
return (name, matches, data) => {
const key = String.fromCharCode(data.code);
if (key === 'g' || key === 'G') {
state.gold++;
} else {
for (let i = 0; i < state.producers.length; i++) {
if (key === String(state.producers[i].id)) {
state.gold -= state.producers[i].cost;
state.producers[i].cost *= state.producers[i].growthRate;
state.producers[i].count++;
state.productionRate += productionRate;
}
}
}
if (!state.isInitCompleted) {
for (let i = 0; i < state.producers.length; i++) {
if (state.gold >= state.producers[i].cost) {
state.isInitCompleted = true;
checkInitCompleted(term, state);
}
}
}
if (!state.isProducerListUpdated) {
updateProducerList(term, state);
}
}
}
export const handleStateChange = (term, state) => {
return () => {
updateGold(term, state);
}
}
2) 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.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, ...)\n GOLD: ${state.gold} \n 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) => {
for (let i = 0; i <= state.producers.length; i++) {
if (state.producers[i].cost > 0) {
term.moveTo(1, 5);
term(state.producers[i].title `: ${state.producers[i].count}` + `| Production per second: ${state.baseProduction.toFixed(1)} | Cost: ${state.producers[i].cost.toFixed(1)}`);
}
}
state.isProducerListUpdated = true;
}
Errors:
- updateProducerList should be called once from handleKeyPress if isProducerListUpdated is false
- updateProducerList should start printing at the beginning of the fifth line by calling moveTo(1, 5)
- handleKeyPress arguments should be forwarded into updateProducerList
- updateProducerList should print out the producer information in expected format using a single string
- updateProducerList should print out all the producers where the count is greater than 0
- updateProducerList should print one extra producer with the 0 count and next cost value
- updateProducerList should set the flag isProducerListUpdated to true once it finishes the update
I tried but i can figure out what i've done wrong..
Can anyone help me? Thanks