Redux road

Hello, I am having issues under standing this instruction in the road to redux project:

A player may travel for any number of days, which costs 20 supplies for each day but adds 10 kilometers each day.

If the action.type is 'travel' , assume that the action.payload contains the number of days to travel. Return a new state object with:

  • 20 less supplies for each day
  • 10 more kilometers of distance traveled for each day
  • The number of days added to days

This is what I have for code:

const initialWagonState = { 
  suplies: [100], 
  distance: [0], 
  days: [0]
};

 const oreganTrail = (state = initialWagonState , action) => {
   switch (action.type) {
     case 'gather': {
       return {
         supplies: supplies.food +15,
         distance: distance.kil,
         days: days.days +1
       }
       ...state, 
     }
     default: {
       return state;
     }
   }
 }

Hey,

So I tried to think about it this way, if the player’s decision is to travel then they will also be deciding the number of days, that they want to travel, which would be our action.payload. We could also think of our action object as being:
{ type: 'travel', payload: x } where x is the number of days the player wants to travel.

So in our reducer function we would want to do something like this:

const initialWagonState = {
    supplies: 100, 
    distanceTravelled: 0,
    days: 0
}


const oreganTrail = (state = initialWagonState, action) => {
  switch(action.type) {
    // also I noticed an error in you 'gather' case statement.
    case 'gather' : {
        return {
            ...state, 
            supplies: state.supplies + (15),
            days: state.days + 1 
        }
    }
    case 'travel' : {
        return {
            ...state,
            supplies: state.supplies - (action.payload * 20),
            distance: state.distance + (action.payload * 10)
        }
    }
    default: {
        return state
    }
  }
}
// So lets say that our player decides to travel for 3 days,

// first we initialize our players wagon  
let wagon = oreganTrail(undefined, {})

// then we dispatch the action to our reducer function.
wagon = oreganTrail(wagon, {
    type: travel, 
    payload: 3
})

// Then we output the players state 
console.log(wagon)
// should print {supplies: 40, distance: 30}


I hope this helps. I also recommend going back over the Redux Core Concepts lessons,

Hye! So I did it differently than dakotah_godfrey but I got all the correct answers I believe.

Remember in the return statement you need to add what ever is already in the state plus the new input. Thats where the spread function comes in, then to add to the existing state in the object you need to call the state so for example supplies. You would do supplies: state.supplies + (whatever number to be added). Defiantly reviewing the past exercises helped me out a lot lol.

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": {

      if(state.supplies >= 0 ){

        return state;

      } else {

      return {

        ...state,

        supplies: state.supplies - 20,

        distance: state.distance + 10,

        days: state.days + action.payload,

      };

      }

    }

    case "tippedWagon": {

      return {

        ...state,

        supplies: state.supplies - 30,

        days: state.days + 1,

      };

    }

    default: {

      return state;

    }

  }

};

let wagon = reducer(undefined, {});

wagon = reducer(wagon, {type: 'travel', payload: 1})

wagon = reducer(wagon, {type: 'gather'})

wagon = reducer(wagon, {type: 'tippedWagon'})

wagon = reducer(wagon, {type: 'travel', payload: 3})

console.log(wagon)``

I hope this helped!

Thank You! I used dakotah_godfrey’s exaple and My code works, I am still having trouble understanding some of the code, I am going to go over the chapter again, I feel as if I have missed something. I understand setting up the switch statements, but when I start to make my actions, that is where I am having issues.

1 Like