Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 8, 2021 07:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/07dd2229394a5ff7b5de7722e98caca2 to your computer and use it in GitHub Desktop.
Save codecademydev/07dd2229394a5ff7b5de7722e98caca2 to your computer and use it in GitHub Desktop.
Codecademy export
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)
@lordmatic
Copy link

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.

@buzzdead
Copy link

buzzdead commented Dec 15, 2021

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