Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Pure Function (no mutation)

Modern JavaScripteasy

Course · Section 17: Modern JavaScript Development: Modules, Tooling, and Functional · Lecture 294: Declarative and Functional JavaScript Principles

Implement `addItemPure(arr, item)` returning a new array with `item` appended, without mutating the input. This is a pure function.

Sample tests

Input: addItemPure([1,2], 3)
Output: [1,2,3]
Input: addItemPure([], 1)
Output: [1]

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • push mutates the argument, making the function impure.
Visualize this concept: ES Modules →
Approach & explanation (try first)

Returning a new array keeps the function pure: same input, same output, no side effects.

Loading...
⌘/Ctrl + Enter

Run your code to see results.