본문 바로가기
알고리즘/Codewars

[Codewars] Square(n) Sum (8 kyu) / JavaScript

by fluss 2022. 10. 26.

https://www.codewars.com/kata/515e271a311df0350d00000f

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

Complete the square sum function so that it squares each number passed into it and then sums the results together.

For example, for [1, 2, 2] it should return 9 because (/1^2 + 2^2 + 2^2 = 9/).

 

설명:

전달된 각 숫자를 제곱하고 그것을 모두 더하는 제곱 합 함수를 완성하세요.

예를 들어, [1, 2, 2]는 (/1^2 + 2^2 + 2^2 = 9/)이기 때문에 9를 반환해야 합니다.

 
 

풀이

reduce를 사용해서 값을 제곱하고 더해주었다.

 

코드

function squareSum(numbers){
  return numbers.reduce((acc, cur) => {
    return acc + cur * cur;
  }, 0);
}

댓글