Operations with objects in a function in JavaScript

Let's add a function to the object that will perform some useful operation. For example, it will find the sum of the object elements.

Let's have an object:

javascriptlet obj = {a: 1, b: 2, c: 3};

Let's create a symbol:

javascriptlet sym = Symbol();

And write a function:

javascriptobj[sym] = function() {
  
};

In a function bound to an object, this will refer to the object itself:

javascriptobj[sym] = function() {
  console.log(this); // {a: 1, b: 2, c: 3}
};

Using our function, we will find the sum of the object elements:

javascriptobj[sym] = function() {
  let sum = 0;
  
  for (let key in this) {
    sum += this[key];
  }
  
  return sum;
};

Let's invoke our function, getting the sum of the object elements:

javascriptlet sum = obj[sym]();
console.log(sum); // shows 6
Задание 1

Given an array:

javascriptlet arr = [1, 2, 3];

In the way described in the lesson, add a function to the array that will return the sum of the array elements.

Задание 2

Call the function you created and make sure it finds the sum correctly.

Задание 3

Add several elements to the array using the push method. Make sure the function will find the sum given the new elements.