Home > katas > Square(n) Sum (8kyu) [JavaScript]

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


Chek kata on Codewars

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.

A few ways to square a number

1 number * number
2 Math.pow(number, 2)
3 number **2

Loop

function squareSum(numbers) {
    let sum = 0
    for( let i = 0; i < numbers.length; i++) {
        sum += numbers[i] **2
    }
    return sum
}

Reducer

function squareSum(numbers) {
    const multiply = number => number * number
    const reducer = (acc, cur) => acc + cur
    return Array.from(numbers, multiply).reduce(reducer, 0)
}

function squareSum(numbers) {
    return numbers.map(multiply).reduce(reducer, 0)
}
function multiply(number) {
    return Math.pow(number, 2)
}
function reducer(acc, cur) {
    return acc + cur
}

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

ForEach

function squareSum(numbers) {
    let sum = 0
    numbers.forEach( number => {
        sum += (number * number)
    })
    return sum
}
© 2021, Andrew Losseff