Redux Road Step 12: eliminating negative values

Hello everyone, I’ve completed the Redux Road project but I am stuck on the extension:

Step 12
Currently, the player can continue traveling even if their supplies are negative! Fix this in the 'travel' case.

If there are not sufficient supplies to travel the given number of days, return the current state.

Link to exercise: Core Concepts in Redux | Codecademy

Here is my code: https://gist.github.com/d57c9189f81ed33ceac345bf70dd81f7

The issue lies in my switch statement for travel. Any advice would be appreciated. I can’t figure out how to return the current state if the supplies are less than 0.

Thank you in advance,

Matt

  switch(action.type) {
    case 'travel': {
      if (state.supplies > 0) {
          return {
          ...state, 
            supplies: state.supplies - (20 * action.payload),
            distanceTravelled: state.distanceTravelled + (10 * action.payload),
            daysOnTheRoad: state.daysOnTheRoad + (1 * action.payload)
        } 
      } else if (state.supplies <= 0) {
        return [...state]
      }
    }
  }

Hi @blog0376503659
currently, if there are more than 0 supplies, you return an object. In case your condition isn’t met, you return an array. The item passed as the initial state to the reducer is an object. So an object should be returned. Have a look at your default case.
If you have 10 supplies and want to travel 3 days, you’d end up with -50 supplies right now. Therefore the condition must be changed in a way that you check if the supplies would be below zero after state change – not if they currently are below zero.

1 Like

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