Dark Mode
Enable Console
multiplesOf3or5(number)
/*
Returns the sum of all the multiples of 3 or 5 below the number passed in.
Additionally, if the number is negative, return 0 (for languages that do have them).
If the number is a multiple of both 3 and 5, it only counts it once.
*/
multiplesOf3or5(10);
// 23
multiplesOf3or5(88);
// 1845
Solution by - Need4Swede
function multiplesOf3or5(number){
// Create new Array
let numArr = [];
// Initalize Sum
let sum = 0;
// Count up to the passed-in number
for(let i = 1; i < number; i++){
if(i % 3 == 0 && i % 5 == 0){
// If the number is divisible by 3 and 5, add it to our new array
numArr.push(i);
}
else if(i % 3 == 0 || i % 5 == 0){
// If the number is divisible by 3 or 5, add it to our new array
numArr.push(i)
}}
// For every number in our array, add it to the sum
numArr.forEach(element => {
sum = element + sum;
})
// Return the sum total
return sum
}
Try it yourself - Codewars Source