Hi all. The outputs are different than what other members of this community found out. Some returned to NaN too. What is wrong with this code and why do we use multiplications on state calculations? Can someone explain it please?
const initialWagonState = {
supplies: 100,
distance: 0,
days: 0,
};
const reducer = (state = initialWagonState, action) => {
switch (action.type) {
case "gather": {
return {
...state,
supplies: state.supplies + 15,
days: state.days + 1,
};
}
case "travel": {
return {
...state,
supplies: state.supplies - 20,
distance: state.distance + 10,
days: state.days + 1,
};
}
case "tippedWagon": {
return {
...state,
supplies: state.supplies - 30,
days: state.days + 1,
};
}
default: {
return state;
}
}
};
console.log(reducer(undefined, {}));
console.log(reducer("wagon", { type: "travel", payload: 1 }));
console.log(reducer("wagon", { type: "gather", payload: undefined }));
console.log(reducer("wagon", { type: "tippedWagon", payload: undefined }));
console.log(reducer("wagon", { type: "travel", payload: 3 }));