HomeChallengesRepsProblemsLeaderboardTake a challenge

The Mutation Trap

Three functions that all quietly change the data they were given. Two are broken. Work out which.

Beginner10 min on the clock5 graded checks · 100 pointsJavaScript + TypeScript
Graded by running your code against real cases.

What you build

  • applyDiscount must return a new array and leave prices alone
  • updateTheme must return a new user and leave the original theme alone
  • The nested settings object has to be copied too, not just the outer one
  • Work out why addBonus is already safe
  • After calling all three, user, prices and score are unchanged

Done means

All three return the right value, and none of the originals change.

How it is graded

Published in full, before you start — every point is one of these and there is nothing else. Each one runs your code; it is not a search for keywords.

  1. applyDiscount returns the discounted numbers+15Array methods
  2. applyDiscount leaves the original array alone+25Not mutating your inputs
  3. updateTheme returns a user with the new theme+15Reshaping API data
  4. The original user keeps its theme — including the nested object+25Not mutating your inputs
  5. addBonus adds ten and the original score is untouched+20Not mutating your inputs

What it teaches

The rest of JavaScript + TypeScript