Codewars — Solving Multiples of 3 or 5 using JavaScript

The most important part of solving a problem is to gather information.

  • What is the problem? — In this case, the problem is that we are being passed a number into a function, but the function currently does not provide the desired output.
  • What do we know so far? — We know that we will receive any number either positive or negative. We also know that our function will be written in Javascript.
  • What are we trying to achieve? — We want our function to take a number and then provide the sum of all the natural numbers below it, which are multiples of either 3 or 5.

Now the fun part…

I’ve broken down the steps using comments to help guide me as I code.
  • Create a variable to hold the sums
This part is pretty simple as you can see.
  • Use a for loop to go through the numbers leading up
I created an empty for loop.

The for loop here starts with i = 0, continues looping until i = number, and i increments by +1 each loop. This lets us check each number leading up to the number that gets passed through the function.

  • Check each number if it is a multiple of 3 or 5
Here we have the checks to see if the number is divisible by 3 or 5.

Using an if statement, we can check if the current number, i, is divisible by 3 or 5 using the modulus operator. If the result of i % 3 or i % 5 is equal to 0 that would mean that there were no remainders and that it passes our check. Upon passing the check, we will add the number to our sum variable as shown by the commented area.

Notice that continue; was added after where we would add to the sum. We do this so that as soon as a number is added the first time that it would skip the other checks for that number. Otherwise, if a number was divisible by both 3 AND 5 it would have been added twice. The instructions stated that it should only be counted once in that case.

Always read the fine print! It can bite you in the ass.
  • Add the number to the sum if it passes the check
The sums are calculated by adding the current value of i.

The last part was really the hardest part of this problem. It’s smooth sailing from here. As you can see we added sum+=i, which is the same as sum = sum + i.

  • Return the sum
Return the sum. Not much else to say here.

Did it work?

Now we check if our solution passes the tests!

All green! We did it, folks.

--

--

--

I struggle, I make mistakes, and then I write about solutions like I was an expert all along.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

#freestockphoto (Wednesday 25th 08AM)

Optimize The Size Of Your Dockerized Go Application Down To 2MB

How to integrate Active Directory in PHP Application for SSO

How to Convert a Video to GIF in PHP

DevXCon SF 2017: Faces to Names

Unity2D DevLog 13— They Shoot Back!

You didn't declare the variable 'len' in this snippet

How to Use ‘break’, ‘pass’, and ‘continue’ Statements in Python

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Danny

Danny

I struggle, I make mistakes, and then I write about solutions like I was an expert all along.

More from Medium

Difference between Library and framework

laibray and frameworks

CS50P Problem Set 2 Coke Machine

Hacker Rank Counting Sort Node.js

USE GIT, GITHUB LIKE A PRO..