Write a program that prints the numbers from 1 to 100.

But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”.

For numbers which are multiples of both three and five print “FizzBuzz”.

I will post my own code snippets/solution towards the end, but for now, I want you to try and tackle these steps on your own as I explain.

Where to even start?

Let’s forget about languages for a second. What is required of us to solve this problem?

To begin with, we simply need to print the numbers 1 to 100. Don’t worry about the next part of the problem. Just get the numbers 1 through 100 printed(in JavaScript you can just console.log your code).

Hint: In order to log every number 1 to 100, you will need a loop, console logging each number.

After you have gotten the numbers 1 to 100 logged to your console, you are ready for the next step.

N

ext, we are asked to print “Fizz” for multiples of 3. What does this even mean? “A multiple of 3” is a number that is divisible by 3 with no remainder left over.

So we need to check whether or not each number is divisible by 3. If the number is divisible by 3, you need to log “Fizz” instead of the actual number. You are already looping through 1–100, now we need to check for conditionals. Let’s use an if statement! One of the simplest ways to check for conditions in JavaScript.

If the number is divisible by 3, print “Fizz”, else continue logging all other numbers. Once you have this implemented, let’s move on.

N

ext, we are asked to print “Buzz” for multiples of 5. If you were able to complete the previous step, this step should be a breeze.

If the number is divisible by 5, print “Buzz”, else continue logging all other numbers.

F

inally, we are asked to print “FizzBuzz” for all numbers that are multiples of both 3 and 5. You’ve made it this far, how can you check if a number is divisible by multiple numbers?

Yes, an if statement, combined with the logical and operator.

If the number is divisible by both 3 and 5, print “FizzBuzz”, else continue logging all other numbers.

By this point, you have solved the coding challenge. You are printing “Fizz” for all multiples of 3, you are printing “Buzz” for all multiples of 5, and lastly, you are printing “FizzBuzz” for all multiples of both 3 and 5.

Well done! Now let’s look at my own personal solution to this problem.

Remember: My code is written in JavaScript. Is it the most optimized solution? No. However, it’s readable and easy to understand. Other developers are going to be coming behind you weeks, months, maybe years down the line. Make your code readable!

Logging 1 through 100 to your console

Image for post

Screenshot by the author

Here we are using JavaScript’s for loop, looping through the numbers 1 to 100, console logging each number. Notice, we are assigning a value to (i)_, _andfor each iteration of the loop as long as (i) is less than or equal to 100, we add 1 to (i). The conditions inside of the for loop curly braces {} run for every iteration of the loop, this is how we are able to log our numbers and words.

#code #learning-to-code #fizzbuzz #algorithms #javascript

How to Solve ‘FizzBuzz’ in JavaScript
8.75 GEEK