1Day 1Algorithm

[DAY 2] Simple Array Sum

walk_through_me 2019. 10. 2. 18:30

Given an array of integers, find the sum of its elements.

For example, if the array ar=[1, 2, 3],  1 + 2 + 3 = 6, so return 6.

 

Function Description

Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.

simpleArraySum has the following parameter(s):

  • ar: an array of integers
 

Simple Array Sum | HackerRank

Calculate the sum of integers in an array.

www.hackerrank.com

 

소요시간 : 4분

 

풀이

function simpleArraySum(ar) {
    let result = ar.reduce((acc, cur) => { return acc + cur });
    return result;
}

1. reduce() 함수를 이용하여 누산함

2. 누산 값을 result 변수에 담아 리턴함

 

 

다른 사람의 풀이

function simpleArraySum(ar) {
	return ar.reduce((a, b) => a + b)
}

아..! 이렇게 한줄로 리턴했어도 됐는데...😅