Created
November 22, 2018 11:05
-
-
Save harrisonmalone/e734e07bf1683a3d73f3d18600f1e0fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. | |
// Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. | |
// Note: If the number is a multiple of both 3 and 5, only count it once. | |
function solution(number){ | |
const arr = [] | |
for(i = 1; i < number; i++) { | |
arr.push(i) | |
} | |
const sumArr = [] | |
arr.forEach(function(number) { | |
if (number % 3 === 0 || number % 5 === 0) { | |
sumArr.push(number) | |
} | |
}) | |
if (sumArr.indexOf(sumArr[0]) === -1) { | |
return 0 | |
} | |
const result = sumArr.reduce(function(acc, cv) { | |
return acc + cv | |
}) | |
return result | |
} | |
console.log(solution(11)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment