Skip to content

Shared Prototypes Problem

You are given an array A consisting of N integers.

You task is to return an array of N objects that share the same prototype, each of whom should have a value() method, which called on i-th objects should return the integer A[i].

The value() method of all objects must be the same method and it cannot be an own property of those objects (for an object Obj the following Obj.hasOwnProperty('value') should be false)

Write a function function solve(A), that, given an array A of N integers, returns an array B of N Javascript objects.

For any two indices i, j < N, the following conditions should be true:

B[i].value() === A[i];
B[j].value() === A[j];
B[i].value === B[j].value;
!B[i].hasOwnProperty("value");
!B[j].hasOwnProperty("value");

where B = solve(A)

Example

For example, given A = [4, 2], your function should return such array B, that

B[0].value() === 4;
B[1].value() === 2;
B[0].value === B[1].value;
!B[0].hasOwnProperty("value");
!B[1].hasOwnProperty("value");

Restrictions

Solution

First, let’s understand the problem statement by breaking down it into smaller parts:

  1. We need to create an array of objects that share the same prototype
    Object.getPrototypeOf(obj1) === Object.getPrototypeOf(obj2);
    
  2. Each object should have a value() method that returns the integer stored in the object
    B[0].value() === A[0];
    
  3. The value() method should not be an own property of the object
    B[i].hasOwnProperty("value"); // should be false
    

Let’s solve each sub-problem individually.

1. How to create an objects with a shared prototype?

We can create an object with a shared prototype using Object.create(proto).

The proto object will be the prototype of the object created using Object.create(proto).

We can check that two objects have the same prototype by using the Object.getPrototypeOf() method.

const proto = {};

const obj1 = Object.create(proto);
const obj2 = Object.create(proto);

console.log(Object.getPrototypeOf(obj1) === Object.getPrototypeOf(obj2)); // true

2. How to create an object with the value() method that returns the integer stored in the object?

We can create an object with the value() method that returns the integer stored in the object by defining the value() method in the prototype object proto.

const proto = {
  value: function () {
    return this._val;
  },
};

const obj = Object.create(proto);
obj._val = 4;

console.log(obj.value()); // 4

3. How to avoid the value() method to be an own property of the object?

We can avoid the value() method to be an own property of the object by creating the object using Object.create(proto). The value() method will be inherited from the prototype object proto.

const proto = {
  value: function () {
    return this._val;
  },
};

const obj = Object.create(proto);
obj._val = 4;

console.log(obj.hasOwnProperty("value")); // false

Full Code

function solution(A) {
  // Create the prototype object
  const proto = {
    value: function () {
      return this._val;
    },
  };

  // Create array of objects sharing the same prototype
  return A.map(num => {
    const obj = Object.create(proto); // Create object with proto as prototype
    obj._val = num; // Store the value in a property
    return obj;
  });
}

Test

const A = [4, 2];
const B = solution(A);

console.log(B[0].value()); // 4
console.log(B[1].value()); // 2
console.log(B[0].value === B[1].value); // true
console.log(B[0].hasOwnProperty("value")); // false
console.log(B[1].hasOwnProperty("value")); // false

Summary