0
0
Angularframework~20 mins

Actions and reducers pattern in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Actions and Reducers Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this reducer function?

Consider this Angular reducer function handling a counter state. What will be the new state after dispatching the action { type: 'increment' } when the current state is { count: 5 }?

Angular
function counterReducer(state = { count: 0 }, action: { type: string }) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    default:
      return state;
  }
}

const currentState = { count: 5 };
const newState = counterReducer(currentState, { type: 'increment' });
console.log(newState);
Aundefined
B{ count: 5 }
C{ count: 4 }
D{ count: 6 }
Attempts:
2 left
💡 Hint

Think about what the reducer does when it receives the 'increment' action.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines an action creator function in Angular?

Choose the correct syntax for an action creator function that returns an action object with type 'addItem' and a payload containing an item string.

Afunction addItem(item: string) { return { actionType: 'addItem', data: item }; }
Bfunction addItem(item: string) { return { type: 'addItem', payload: item }; }
Cfunction addItem(item: string) { return { type: addItem, payload: item }; }
Dfunction addItem(item: string) { return { type: 'addItem', item }; }
Attempts:
2 left
💡 Hint

Look for the standard keys used in action objects.

🔧 Debug
advanced
2:00remaining
Why does this reducer cause an error?

Examine the reducer code below. Why will it cause an error when dispatched with any action?

Angular
function todoReducer(state = [], action: { type: string, payload?: string }) {
  switch (action.type) {
    case 'add':
      state.push(action.payload);
      return state;
    default:
      return state;
  }
}
AIt does not handle the 'remove' action type.
BIt uses an invalid switch statement syntax.
CIt mutates the state array directly, which is not allowed in reducers.
DIt returns undefined instead of a state object.
Attempts:
2 left
💡 Hint

Reducers should never change the original state directly.

state_output
advanced
2:00remaining
What is the final state after these actions?

Given the reducer and the sequence of dispatched actions below, what is the final state?

Angular
function counterReducer(state = { count: 0 }, action: { type: string }) {
  switch (action.type) {
    case 'increment':
      return { count: state.count + 1 };
    case 'decrement':
      return { count: state.count - 1 };
    case 'reset':
      return { count: 0 };
    default:
      return state;
  }
}

let state = { count: 0 };
state = counterReducer(state, { type: 'increment' });
state = counterReducer(state, { type: 'increment' });
state = counterReducer(state, { type: 'decrement' });
state = counterReducer(state, { type: 'reset' });
state = counterReducer(state, { type: 'increment' });
console.log(state);
A{ count: 1 }
B{ count: 2 }
C{ count: 0 }
D{ count: 3 }
Attempts:
2 left
💡 Hint

Follow each action step by step and update the count.

🧠 Conceptual
expert
2:00remaining
Which statement best describes the role of actions in the reducer pattern?

Choose the most accurate description of what actions do in the actions and reducers pattern in Angular.

AActions describe what happened and carry data to update the state accordingly.
BActions store the entire application state for the reducer to use.
CActions are functions that return the new state after processing.
DActions directly modify the state to reflect user interactions.
Attempts:
2 left
💡 Hint

Think about the purpose of actions as messages.