My code doesn't work as expected

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 }));

I think what’s missing here is saving state to a variable.

instead of console.log(reducer(undefined,{}));

Save state with const wagon = reducer(undefined, {}));

It looks like the other solutions you found told you to put “wagon” into the next statements. But you can change them to the wagon variable you created above.

Then, instead of console.log(reducer... - you can change them to:

wagon = reducer(wagon, { type: ...

Then console.log(wagon) after each step!

That should get you closer to your solution.