-
-
Save codecademydev/d57c9189f81ed33ceac345bf70dd81f7 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, | |
distanceTravelled: 0, | |
daysOnTheRoad: 0 | |
}; | |
const reducer = (state = initialWagonState, action) => { | |
switch (action.type) { | |
case 'gather': { | |
return { | |
...state, | |
supplies: state.supplies + 15, | |
distanceTravelled: state.distanceTravelled, | |
daysOnTheRoad: state.daysOnTheRoad + 1 | |
} | |
} | |
} | |
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] | |
} | |
} | |
} | |
switch(action.type) { | |
case 'tippedWagon': { | |
return { | |
...state, | |
supplies: state.supplies - 30, | |
distanceTravelled: state.distanceTravelled, | |
daysOnTheRoad: state.daysOnTheRoad + 1 | |
} | |
} | |
} | |
switch(action.type){ | |
default: { | |
return state; | |
} | |
} | |
} | |
let wagon = reducer(undefined, {}); | |
console.log(wagon); | |
const firstDay = { type: 'travel', payload: 1 }; | |
wagon = reducer(wagon, firstDay); | |
console.log(wagon); | |
const secondDay = { type: 'gather' }; | |
wagon = reducer(wagon, secondDay); | |
console.log(wagon); | |
const thirdDay = { type: 'tippedWagon' }; | |
wagon = reducer(wagon, thirdDay); | |
console.log(wagon); | |
const threeMoreDays = { type: 'travel', payload: 3 } | |
wagon = reducer(wagon, threeMoreDays); | |
console.log(wagon); | |
const oneMoreDay = { type: 'travel', payload: 1 } | |
wagon = reducer(wagon, oneMoreDay); | |
console.log(wagon); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment