Search⌘ K
AI Features

Solution Review: Let's Add Properties

Explore how to transform impure functions into pure functions by returning a new object that includes both original and new properties. Understand the use of arrow functions with object literals and the spread syntax to prevent modification of the original object. This lesson equips you with practical skills in functional programming and immutability for JavaScript coding interviews.

We'll cover the following...

Solution #

Node.js
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()

Explanation #

In this challenge, you had to convert the following impure function into a pure function.

Node.js
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()

As you can see, the function updates ...