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)