What is FizzBuzz Challenge?

0
(0)

The FizzBuzz Challenge is a classic coding task often used in job interviews to verify that a candidate understands basic programming fundamentals like loops, conditional logic, and basic arithmetic.

The Rules

Write a program that prints the numbers from 1 to 100, following these conditions:

  • For multiples of 3, print "Fizz" instead of the number.

  • For multiples of 5, print "Buzz" instead of the number.

  • For numbers that are multiples of both 3 and 5 (like 15), print "FizzBuzz".

  • For all other numbers, just print the number itself.

Example Output

Plaintext

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...

Basic Solution (Python)

The key trick to solving FizzBuzz is checking for the both (15) condition first, or handling string concatenation, so that 15 outputs "FizzBuzz" instead of just "Fizz".

Python

for i in range(1, 101):
    if i % 3 == 0 and i % 5 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        print(i)

(The % operator is the modulo operator, which returns the remainder of a division. Ifi % 3 == 0, the number is evenly divisible by 3.)

Different ways to solve the FizzBuzz challenge in JavaScript or Python, including short one-liner approaches.

Python Solutions

1. String Concatenation (Extensible Approach): This approach avoids checking i % 15 == 0 explicitly and easily scales if you need to add more word rules (e.g., “Bazz” for 7).

Python

for i in range(1, 101):
    output = ""
    if i % 3 == 0: output += "Fizz"
    if i % 5 == 0: output += "Buzz"
    print(output or i)

2. List Comprehension / Ternary One-Liner Using nested ternary operators inside a single list comprehension:

Python

print('\n'.join(["Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or str(i) for i in range(1, 101)]))

How it works: Python allows string multiplication by booleans ("Fizz" * True becomes "Fizz", "Fizz" * False becomes ""). If both evaluate to false, "" or str(i) falls back to str(i).

JavaScript Solutions

1. Clean Array Map / Higher-Order Functions: A functional approach using array generation and modern JS syntax:

JavaScript

Array.from({ length: 100 }, (_, i) => i + 1).forEach(i => {
  const fizz = i % 3 === 0 ? "Fizz" : "";
  const buzz = i % 5 === 0 ? "Buzz" : "";
  console.log(fizz + buzz || i);
});

2. Modern JavaScript One-Liner: A concise one-liner leveraging logical OR short-circuiting:

JavaScript

for (let i = 1; i <= 100; i++) console.log((i % 3 ? '' : 'Fizz') + (i % 5 ? '' : 'Buzz') || i);

How it works: i % 3 returns 0 (falsy) when divisible, so i % 3 ? '' : 'Fizz' resolves to 'Fizz'.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email

Leave a Reply

Your email address will not be published. Required fields are marked *

students could gain from adopting the “three part day” concept : one block can be dedicated each to academic tasks, paid work and exploring nearby sites.