Last active
November 25, 2025 14:51
-
-
Save cassidoo/f3e5a01815a647b23183ffe5c7021aa3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| let array = [1, 2, 3, 4, 5]; | |
| let add = (x, y) => x + y; | |
| let sum = array.reduce(add, 0); | |
| // This will do: | |
| // 0 + 1 | |
| // 1 + 2 | |
| // 3 + 3 | |
| // 6 + 4 | |
| // 10 + 5 | |
| const initialState = { count: 0, user: {} } | |
| const actions = [ | |
| { type: 'ADD', by: 2 }, | |
| { type: 'MINUS', by: 4 }, | |
| { type: 'LOG_IN', name: 'fish' } | |
| ]; | |
| function reducer(state, action) { | |
| if (action.type === 'ADD') { | |
| return { ...state, count: state.count + action.by }; | |
| } else if (action.type === 'MINUS') { | |
| return { ...state, count: state.count - action.by }; | |
| } else if (action.type === 'LOG_IN') { | |
| return { ...state, user: { name: action.name } }; | |
| } | |
| } | |
| console.log(actions.reduce(reducer, initialState)); // count is -2, user's name is fish | |
Author
cassidoo
commented
Nov 20, 2025
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment