[HackerRank] Simple Array Sum

Jayoung Lee·2020년 8월 6일
0

Simple Array Sum

Easy

Problem

Given an array of integers, find the sum of its elements.
주어진 정수 배열에 대해 요소들의 합을 구해라.

Function Description

Complete the simpleArraySum function in the editor below. It must return the sum of the array elements as an integer.
아래 에디터에 있는 함수 simpleArraySum 를 완성해라. 정수로 배열의 합을 리턴해야한다.

simpleArraySum has the following parameter(s):

  • ar: an array of integers

Input Format

The first line contains an integer, n , denoting the size of the array.
The second line contains space-separated integers representing the array's elements.
첫번째 라인은 정수를 포함하고 n은 배열의 크기를 나타낸다.
두 번째 줄에는 배열의 요소를 나타내는 공백으로 구분 된 정수가 포함됩니다.

Constraints
0 < n, ar[i] <= 1000

Output Format

Print the sum of the array's elements as a single integer.
배열 요소의 합을 정수로 출력해라.

Sample Input

6
1 2 3 4 10 11

Sample Output

31

Explanation

We print the sum of the array's elements: 1+2+3+4+10+11=31

My Solution

...

/*
 * Complete the simpleArraySum function below.
 */
function simpleArraySum(ar) {
    /*
     * Write your code here.
     */
    return ar.reduce((acc, val) => acc + val);

}

...

Result

해커랭크는 처음인데, 기본으로 써있는 코드가 좀 길어서 놀랐다. 뭐든 for문을 먼저 쓰는 습관있어서 이번에는 reduce를 찾아서 해봤다.

profile
프론트엔드 개발 공부하면서 아카이빙 중입니다 ʕ•ﻌ•ʔ

0개의 댓글