Skip to content
the poor coder

js fizzbuzz

Poor Man
Published
Reading time
2 min read

JS FizzBuzz#

If you are a beginner programmer or preparing for a coding interview, you might have heard of the FizzBuzz problem. In this problem, you have to print numbers from 1 to n. But, if the number is divisible by 3, print "Fizz" instead of the number. If the number is divisible by 5, print "Buzz". If the number is divisible by both 3 and 5, print "FizzBuzz".

Here is one way to solve this problem in JavaScript:

JavaScript

            function fizzBuzz(n) {
                for (let i = 1; i <= n; i++) {
                    if (i % 3 === 0 && i % 5 === 0) {
                        console.log('FizzBuzz');
                    } else if (i % 3 === 0) {
                        console.log('Fizz');
                    } else if (i % 5 === 0) {
                        console.log('Buzz');
                    } else {
                        console.log(i);
                    }
                }
            }
            fizzBuzz(15);
        

In this code snippet, we define a function called "fizzBuzz" that takes an argument "n" as the upper limit of the range of numbers. We then use a for loop to iterate through each number from 1 to n. Inside the loop, we use if-else statements to check if the number is divisible by 3 and/or 5 and print "Fizz", "Buzz", or "FizzBuzz" accordingly. If none of these conditions are true, we simply print the number itself.

Another way to solve this problem is by using a switch statement instead of if-else statements. Here is the code for that:

JavaScript

            function fizzBuzz(n) {
                for (let i = 1; i <= n; i++) {
                    switch (true) {
                        case i % 3 === 0 && i % 5 === 0:
                            console.log('FizzBuzz');
                            break;
                        case i % 3 === 0:
                            console.log('Fizz');
                            break;
                        case i % 5 === 0:
                            console.log('Buzz');
                            break;
                        default:
                            console.log(i);
                    }
                }
            }
            fizzBuzz(15);
        

In this code snippet, we use a switch statement to check the conditions for printing "Fizz", "Buzz", or "FizzBuzz". Note that the "true" argument in the switch statement is used to compare the value of each case with the boolean value true.

Both of these solutions work fine for the FizzBuzz problem. However, there are many more ways to solve this problem in JavaScript and other programming languages. The key is to understand the problem and come up with a logical and efficient solution.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC