관리 메뉴

Jerry

[HackerRank] Compare the Triplets.js (JavaScript) 본문

Problem Solving/Algorithm 문제 풀기

[HackerRank] Compare the Triplets.js (JavaScript)

juicyjerry 2020. 11. 9. 21:22

 


In this challenge, you are required to calculate and print the sum of the elements in an array, keeping in mind that some of those integers may be quite large.

 

Constraints

 

1 <= n <= 10,

0 <= ar[i] <= 10^10

 

Solution

 

function aVeryBigSum(ar) {
    let ret = ar.reduce((a, b) => a + b);
    return ret;
}

 

Solution 해설

 

  1. reduce 메서드를 이용하여  배열의 모든 값을 더해준다.
  2. ret변수를 리턴한다.

 

참고한 사이트

 

반응형