-
-
Save codecademydev/07dd2229394a5ff7b5de7722e98caca2 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const initialWagonState = { | |
supplies: 100, | |
distance: 0, | |
days: 0, | |
}; | |
const intialWagonStateReducer = (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 + action.payload, | |
} | |
} | |
case "tippedWagon": { | |
return { | |
...state, | |
supplies: state.supplies - 30, | |
days: state.days + 1 | |
} | |
} | |
default: { | |
return state; | |
} | |
} | |
} | |
let wagon = intialWagonStateReducer(undefined, {}); | |
console.log(wagon); | |
wagon = intialWagonStateReducer(wagon, {type: 'travel', payload: 1}) | |
console.log(wagon); | |
wagon = intialWagonStateReducer(wagon, {type: 'gather', payload: 0}) | |
console.log(wagon); | |
wagon = intialWagonStateReducer(wagon, {type: 'tippedWagon', payload: 0}) | |
console.log(wagon); | |
wagon = intialWagonStateReducer(wagon, {type: 'travel', payload: 3}) | |
console.log(wagon) |
Isn't travel supposed to be x amount of days?
Just so:
case 'travel': {
return {
...state,
supplies: state.supplies - (20 * action.payload),
distance: state.distance + (10 * action.payload),
days: state.days + action.payload
};
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your code looks good but I think you forgot to Print the new state.
Example:
let wagon = intialWagonStateReducer(undefined, {});
console.log('initial state')
console.log(wagon);
wagon = intialWagonStateReducer(wagon, {type: 'travel', payload: 1})
console.log('travel for 1 day')
console.log(wagon);
Give that a go.