Skip to content

Instantly share code, notes, and snippets.

@kcemenike
Created March 16, 2021 13:53
Show Gist options
  • Save kcemenike/1fd643e8f24ef1345a4bf776f3b1ac24 to your computer and use it in GitHub Desktop.
Save kcemenike/1fd643e8f24ef1345a4bf776f3b1ac24 to your computer and use it in GitHub Desktop.
Udacity React Nanodegree appReducer.js
/* Create A Reducer
*
* You need to create a reducer called "appReducer" that accepts two arguments:
* - First, an array containing information about ice cream
* - Second, an object with a 'DELETE_FLAVOR' `type` key
* (i.e., the object contains information to delete the flavor from the state)
*
* The action your reducer will receive will look like this:
* { type: 'DELETE_FLAVOR', flavor: 'Vanilla' }
*
* And the initial state will look something like this (as such, refrain
* from passing in default values for any parameters!):
* [{ flavor: 'Chocolate', count: 36 }, { flavor: 'Vanilla', count: 210 }];
*/
function appReducer (state, action){
if (action.type === 'DELETE_FLAVOR' ){
let newState = [];
state.forEach(function(element){
if (element.flavor!== action.flavor){
newState.push(element);
}
});
return newState;
}
return state;
}
console.log(
appReducer([{ flavor: 'Chocolate', count: 36 }, { flavor: 'Vanilla', count: 210 }], { type: 'DELETE_FLAVOR', flavor: 'Vanilla' })
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment