Sobes.tech
Junior — Senior

Creating pure functions for user generation and addition execution

livecode

Task condition

It is required to rewrite the functions createUser and add so that they become pure: they do not affect external state and do not cause side effects, but instead return new data.

// 1
const produceId = () => Math.floor(Math.random() * 10000);
const createUser = (userName: string, userAge: number) => ({
  id: produceId(),
  name: userName,
  age: userAge
});
console.log(createUser("Alex", 28));
console.log(createUser("Alex", 28));
console.log(createUser("Alex", 28));

// 2
let counter = 2;
const add = (value: number) => {
  counter += value;
};

add(4);
console.log(counter);

The task is to eliminate the modification of the global variable counter and the hidden use of the random generator inside createUser. The user creation function should return an object without changing external data, and the addition function should return a new value instead of modifying counter.