JavaScript Coding Challenge #1

There is something I’ve been wanting to do for a long time, and that is to start writing blog posts with random coding challenges along with my solution for them in JavaScript.

I’m using JavaScript because is one of the most used programming language today… that’s the 1st reason. The 2nd reason is that I find it really fun to work with :D and it’s the one I know the most, so… But don’t worry if you haven’t used it as I’m going to explain step by step the entire process and the thinking behind solving the challenge.

Kindly note that I don’t claim to have the best possible solution. I’m more than happy to see other solutions for the challenge.

Ok, enough with the chit-chat … let’s get our hands dirty. We’re going to start with a simpler problem and advance as we go.

The problem is from: https://projecteuler.net/. For those who are interested:

Project Euler is a series of challenging mathematical/computer programming problems that will require more than just mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems.

Multiples of 3 and 5

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 1000.

Great… sounds fun! Let’s get into it.

Firstly we’ll need a variable to store the sum. We’ll initialize it with 0.

let sum = 0;

Now… We’ll need a way to loop through all the numbers from 1 to 999 as we need all numbers below 1000. A for loop will do the trick:

let sum = 0;
for(let i=1; i<1000; i++) {
}

To only get the numbers which are multiples of 3 or 5 we’ll use an if statement:

let sum = 0;
for(let i=1; i<1000; i++) {
if(i % 3 === 0 || i % 5 === 0){
    }
}

The % modulo operator will give us the remainder of the division. For example if we divide 4 by 3 the remainder is 1, and for 6 / 3 the remainder is 0, which is exactly what we need… All the numbers that are evenly divisible by 3 or 5, thus their remainder is 0.

If this is the case, we simply add i to the sum:

let sum = 0;
for(let i=1; i<1000; i++) {
if(i % 3 === 0 || i % 5 === 0){
sum += i;
}
}

For those who don't know, sum += i is the shorthand for sum = sum + i .

That’s all! After the for loop our sum variable will hold the answer for the problem, which is ………… I’ll let you run the code and tell the answer in the comments!

One step further…

Cool! We now have a bit of code that will give us the sum for all the numbers from 1 to 999 that are evenly divisible by 3 or 5.

But let’s take one step further and create a function that will get a number as a parameter and it will return the sum of all the numbers below it which are evenly divisible by 3 or 5. Interesting? :D Let’s go!

We’ll call the function sumOfMultiples and we’ll paste in the code we have above. Also we’ll add a parameter:

function sumOfMultiples(number) {
let sum = 0;
    for(let i=1; i<1000; i++) {
if(i % 3 === 0 || i % 5 === 0){
sum += i;
}
}
}

Awesome! But there’s an issue… We need to loop from 1 to the number minus one (as we need to get all the numbers below the our number) , but we currently loop from 1 to 999… That’s wrong… Also, we want to return the sum at the end of the function. Let’s do that:

function sumOfMultiples(number) {
let sum = 0;
    for(let i=1; i<number; i++) {
if(i % 3 === 0 || i % 5 === 0){
sum += i;
}
}
    return sum;
}

Now we can call the function and pass in any number. It’ll return the correct answer:

sumOfMultiples(10); // returns 23 as 3 + 5 + 6 + 9 = 23
sumOfMultiples(15); // returns 45 as 3 + 5 + 6 + 9 + 10 + 12 = 45

Hurray! We did it!

Conclusion

I know that the problem might seem trivial for some of you, but if it helped you in any way, I’m very happy about it!

Also… for those interested to take a step further:

What if you could also pass 2 more parameters to the function which will replace the 3 and the 5 in the if statement? This way you could give it any other 2 numbers.

A more interesting challenge would be to be able to pass an array of numbers instead of just 2, and calculate the sum of all the numbers that are evenly divisible by the numbers from the array. For this you’ll need another for loop. Would be very nice to see your solution for this problem!

Thank you very much for the patience. I’d love to hear your feedback about the post.

Also, here you can find the follow up article of this post in which we solve the algorithm in a different way.

Happy coding! ^_^

If you liked my post, I would sincerely appreciate a click on the Recommend button. 💚