FizzBuzz Theory

Falade Timilehin - Jan 29 '23 - - Dev Community

In this article, we look at FizzBuzz, a very common programming task in software development interviews. Since the solution utilizes a few major concepts, this task has become a go-to for interviewers.

If you are new to the concept, I’d recommend you follow step by step to understand the concept as a whole. However, If you are here to refresh your knowledge, you can jump straight to the solutions.

What is FizzBuzz?

Firstly, let’s get this out of the way, FizzBuzz is a task where the programmer is asked to print numbers from 1 to 100, but here’s the catch, multiple of three should print “Fizz” and similarly print “Buzz” for multiples of 5 and lastly print “FizzBuzz” for multiples of three and five.

Although the last may seem straightforward, even seasoned programmers tend to get the logic wrong at times.

Given an integer n, return a string array answer (1-indexed) where:

answer[i] == "FizzBuzz" if i is divisible by 3 and 5.
answer[i] == "Fizz" if i is divisible by 3.
answer[i] == "Buzz" if i is divisible by 5.
answer[i] == i (as a string) if none of the above conditions are true.

**
 * @param {number} n
 * @return {string[]}
 */
var fizzBuzz = function(n) {
let answer = [];
    for(let i = 1; i <= n ; i++){
        if(i % 15 === 0){
        answer[i] = "FizzBuzz"
        continue
        }
        else if(i % 3 ===0 ){
         answer[i] = "Fizz"
         continue
        }
        else if(i % 5 === 0){
            answer[i] = "Buzz"
            continue
        }
        else{
         answer[i] = `${i}`
         continue
        }
    }
answer.shift();
    return answer;
};


Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . .
Terabox Video Player