Multiples of 3 and 5

Download Video

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. Find the sum of all the multiples of 3 or 5 below the provided parameter value number.

Solution

let multiplesOf3and5 = (number) => {

  let sum = 0;
  let i;

  // Look through each number from 0 to number taken in
  for (i = 0; i < number; i ++){

    // If either multiple of 3 or 5, add to sum
    if (i % 3 === 0 || i % 5 === 0){
      sum = sum += i;
    }
  }

  // Return Sum
  return sum

}

The program iterates through each number from 0 to the limit value. For each number, if it is a multiple of 3 or 5, its value is added to the sum. The sum is returned at the end.

Ganesh H