We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
This is my Solution . I tried to use the reduce() method to sum the element , in this case the numbers in the give array. the method takes a callback function as well as the array itself(optional) for which we call the reduce() , the callback function takes 3 param. (previousValue , currentIndexValue , currentIndex(optional) ) and return the result (a number) .
class Polygon{
constructor(lenghts){
this.lenghts = lenghts; }
perimeter(){ return this.lenghts.reduce((total , currentValue)=>{return total + currentValue }) }
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 4: Classes
You are viewing a single comment's thread. Return to all comments →
This is my Solution . I tried to use the
reduce()
method to sum the element , in this case the numbers in the give array. the method takes a callback function as well as the array itself(optional) for which we call the reduce() , the callback function takes 3 param. (previousValue , currentIndexValue , currentIndex(optional) )
and return the result (a number) . class Polygon{ constructor(lenghts){this.lenghts = lenghts; } perimeter(){ return this.lenghts.reduce((total , currentValue)=>{return total + currentValue }) } }