DEV Community

Cover image for FizzBuzz: the-problem 🧮
Matheus Leite
Matheus Leite

Posted on

FizzBuzz: the-problem 🧮

Hello! Welcome.
This is my first article, seriously first, and I'd like to talk a little about a "problem" I learned when I was entering the programming field (intern). It's very simple, fun and makes you think a lot about how you think, logically speaking, and if you can go beyond the basics. I hope you like this series of articles that I'll be creating, happy reading!

What?

Fizzbuzz is a word-game for children that teacher uses to teach them about math divisions. The game consists in replace any number divisible by three with the word Fizz, and any number divisible by five with the word Buzz and any number divisible by both (three and five) with the word FizzBuzz.

The game above can be used as a coding interview question in some case. The question consist in write a logic to output the first 100 (hundred) FizzBuzz numbers. Your value in interviews is to analyze fundamental coding habits, like, logic, design and language proficiency.

How it works in code

The code bellow shows how the "FizzBuzz" logic work.

// javascript
const x = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];

for (let [i, n] of x.entries()) {
    if (n % 15 === 0) {
        x[i] = 'FizzBuzz';
        continue;
    }
    if (n % 3 === 0) {
        x[i] = 'Fizz';
    }
    if (n % 5 === 0) {
        x[i] = 'Buzz';
    }
}
console.table(x);
// Output:
┌─────────┬────────────┐
 (index)    Values   
├─────────┼────────────┤
    0     'FizzBuzz' 
    1         1      
    2         2      
    3       'Fizz'   
    4         4      
    5       'Buzz'   
    6       'Fizz'   
    7         7      
    8         8      
    9       'Fizz'   
   10       'Buzz'   
   11         11     
   12       'Fizz'   
   13         13     
   14         14     
   15     'FizzBuzz' 
└─────────┴────────────┘
Enter fullscreen mode Exit fullscreen mode

That's FizzBuzz, I hope you understood and I'll see you in the next article! Let's talk a little about TDD and how we can apply it to this simple "application". I'll see you there!

Top comments (0)