DEV Community

Cover image for JavaScript Challenge 7: Multiples of 3 or 5
AlbertoM
AlbertoM

Posted on • Originally published at inspiredwebdev.com

JavaScript Challenge 7: Multiples of 3 or 5

This article was originally posted on my blog. Head over to inspiredwebdev.com for more articles and tutorials. Check out my JavaScript course on Educative to learn everything from ES6 to ES2020.

 

In this article we will solve together the Multiples of 3 or 5 challenge from CodeWars, you can find it at this link. The difficulty of this challenge is easy.

Let's read the task together:

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. Also, if a number is negative, return 0(for languages that do have them)

This challenge is very simple and we can achieve the expected result using the remainder operator (%).

 

The remainder operator

What this oparator does is is return the remainder left over when one operand is divided by a second operand.

Let's look at some examples:

6%3;
// 0
6%2;
// 0
6%4;
// 2
6%5;
// 1
6%7;
// 6
Enter fullscreen mode Exit fullscreen mode

Let's go over each example:

1) 6%3 = 0 because 3 * 2 = 6 with no remainder;
2) 6%2 = 0 because 2 * 3 = 6 with no remainder;
3) 6%4 = 2 because 4 * 1 = 4 with 2 remainder;
4) 6%5 = 1 because 5 * 1 = 5 with 1 remainder;
5) 6%7 = 6 because 6 * 0 = 0 with 6 remainder;

Knowing this, we can easily determine if a number is a multiple of 3 or 5 and then perform the sum we need;

 

Working on the solution

function solution(number){
  let sum = 0;
  for (var i = 0; i < number; i++) {
    if (i % 3 === 0 || i % 5 === 0) {
      sum += i;
    }
  }
  return sum;
}
Enter fullscreen mode Exit fullscreen mode

1) first we initialize our sum variable that will hold the total sum of numbers
2) then we iterate over all the numbers, getting only the one perfectly divisible by 3 or 5, using the % (remainder) operator that we saw above
3) lastly we return the sum of all the numbers that match our condition

There are many other ways of solving this problem, let me know yours in the comment.

If you liked this type of content, please let me know in the comments and I'll create more of these.


If you want to learn everything about JavaScript from ES6 all the way to ES2020, please check out my book available to read for free on Github. A course is also on Educative

Alt Text

 

Top comments (6)

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️ • Edited

You missed a very obvious optimization there:

(code in Lua because it's what's easiest for me :D)

local function answer(n)
   local acc = 0
   for i=3, n-1, 3 do
      if i % 5 > 0 then
         acc = acc + i
      end
   end
   for i=5, n-1, 5 do
      acc = acc + i
   end
   return acc
end
Enter fullscreen mode Exit fullscreen mode

This will only iterate over numbers you actually need to add and skip the doubles :D

However, there's a more complex approach you can take:

local function sum_from_1_to_n(n)
    n = math.floor(n)
    return n * n / 2 + n/2
end

local function answer(n)
    n = n - 1
    local multiples_of_3 = 3 * sum_from_1_to_n(n / 3)
    local multiples_of_5 = 5 * sum_from_1_to_n(n / 5)
    local common_multiples = 15 * sum_from_1_to_n(n / 15)
    return multiples_of_3 + multiples_of_5 - common_multiples
end
Enter fullscreen mode Exit fullscreen mode

If necessary I can write an article explaining how that one works ;)

Collapse
 
newbie322 profile image
Friday

Is Lua some programming language or something?

Collapse
 
darkwiiplayer profile image
𒎏Wii 🏳️‍⚧️
Collapse
 
albertomontalesi profile image
AlbertoM

Awesome, yeah since we are calculating numbers in succession from 1 to x we can totally do it this way rather than looping.

Collapse
 
zerodragon profile image
Zero Dragon • Edited

let's over complicate this :P

const solution = number => // implicit return, no need for curly braces
  [...new Array(number)] //start new array of length
    .map((e,i) => i) // add values from 0 to number - 1
    .filter(item => item % 3 === 0 || item % 5 === 0) // pass only elements that matches modal
    .reduce((acum, current) => acum + current) // sum all passed elements
Enter fullscreen mode Exit fullscreen mode
Collapse
 
newbie322 profile image
Friday

Sure, I like it....I think it's exactly what a beginner like me need to grow....