Search⌘ K

Challenge: Let's Add Properties

Explore how to convert an impure function into a pure function that returns a new object with additional properties without modifying the original. This lesson helps you understand immutability and functional programming principles in JavaScript through a hands-on challenge.

We'll cover the following...

Problem statement #

Study the code below and then run it.

Javascript (babel-node)
const func = (key, value, object) => {
object[key] = value;
};
function test() {
const car = {
name: "Toyota"
};
const answer = func("model", 88, car);
console.log({ car, answer });
}
test();

func is an impure function. The reasoning ...