How to create custom methods javascript?

hi campers,

can anyone help me to explain how to pass the test in codewar?

so far i only assume, that methods is a properties that call function inside object.

this the last code i tried, been circled around for hours. but can’t get thing right. i feel lost,
is there something that that i missed ?

var arr ={
sum: function() {
    return this.reduce(function(a, b) {
      a + b;
      });
    }
  }
  console.log(arr.sum());

thanks in advance

Hello there,

You are not " add[ing] a method ‘sum’ to all array objects". You are adding a property sum to one arr object literal.

As a hint, here are a few Array object methods: map, reduce, filter

Here is a spoiler, if that hint does not help:

You need to define an Array.prototype.sum = function() {...}

Also, have a look at the Mozilla Docs on Arrays, if you want to see how they implement methods. This is very useful

Hope this helps

2 Likes

hi @Sky020

for now i guess i need to learns about this array prototype function, that was confused me with methods in object proprerti function.

i will take a look at your reff, and i found something like this after read your reply and searching about array.prototype

maybe it would help me, before i try your hint.

thanks a lot Sky020.

regards.