Skip to content

Instantly share code, notes, and snippets.

@AdithyaBhat17
Last active August 1, 2018 14:40
Show Gist options
  • Save AdithyaBhat17/68607def7647d018ddaddd197a4ecec4 to your computer and use it in GitHub Desktop.
Save AdithyaBhat17/68607def7647d018ddaddd197a4ecec4 to your computer and use it in GitHub Desktop.
Create A Reducer
/* 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 }];
*/
const DELETE_FLAVOR = 'DELETE_FLAVOR';
var appReducer = function(state, action){
if(action.type === DELETE_FLAVOR){
var newState = [];
state.forEach(function(element){
if(element.flavor !== action.flavor){
newState.push(element);
}
});
return newState;
}
return state;
}
let iceCreams = [{ flavor: 'Chocolate', count: 36 }, { flavor: 'Vanilla', count: 210 }];
let action = {
type:DELETE_FLAVOR,
flavor:'Vanilla'
};
const nS = appReducer(iceCreams,action);
console.log(nS);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment