Two Simple Ways to Sum an Array in JavaScript

For Loop Rick and Morty Meme
HackerRank Problem
HackerRank Problem

For Loop

function sumSimpleArray(array) {}
function sumSimpleArray(array) {
let sum = 0;
}
function sumSimpleArray(array) {
let sum = 0;
for(let i = 0; i < array.length; i++) {
sum += array[i];
}
return sum;
}

Reduce Method

function sumSimpleArray(array) {
let sum = 0;
const reducer = (accumulator, currentValue) => {
return accumulator + currentValue
}
}
function sumSimpleArray(array) {
let sum = 0;
const reducer = (accumulator, currentValue) => {
return accumulator + currentValue
}

return array.reduce(reducer)
}
function sumSimpleArray(array) {
let sum = 0;
return array.reduce((accumulator, currentValue) => {
return accumulator + currentValue
)}
}

Full stack web developer. Frontend and design enthusiast. Avid rock climber and amateur photographer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store