Introduction

The “Fizz-Buzz programming test” is an interview technical question aiming to filter out the 99.5% of programming job candidates who can’t seem to program their way out of a wet paper bag. The text of the programming assignment is as follows:

Given a number n, for each integer i in the range from 1 to n inclusive, print one value per line as follows:

  • if i is a multiple of both 3 and 5 print FizzBuzz.
  • if i is a multiple of 3 (but not 5) print Fizz.
  • if i is a multiple of 5 (but not 3) print Buzz.
  • if i is NOT a multiple of both 3 or 5 print the value of i.

The FizzBuzz Challenge

Below you will find the code needed to complete the challenge in Javascript, Bash, Python, and PhP

FizzBuzz In JavaScript

var n = 100;
function FizzBuzz(n) {
var fizz = "Fizz";
var buzz = "Buzz";
var fizz_buzz = "FizzBuzz";

for (var i = 1; i <= n; i++) {
    if (i % 3 === 0 && i % 5 === 0) {
        console.log(fizz_buzz);
    }
    else if (i % 3 === 0) {
        console.log(fizz);
    }
    else  if (i % 5 === 0) {
        console.log(buzz);
    }
    else {
        if (i % 3 != 0 || i % 5 != 0)
            console.log(i)
        }
    }
}
FizzBuzz(n);

FizzBuzz In Bash

n=100
for (( i=1; i<=n; i++ )); #for i in {1..n};
do
    if [ $((i%3)) = 0 ] && [ $((i%5)) = 0 ]; then
        echo  "FizzBuzz";
        
    elif [ $((i%3)) = 0 ]; then
        echo "Fizz";
        
    elif [ $((i%5)) = 0 ]; then 
        echo "Buzz";
            
    else 
        if [ $((i%3)) != 0 ] ||  [ $((i%5)) != 0 ]; then
            echo $i;
        fi
    fi
done

FizzBuzz In Python

n=100
for i in range(1, n+1):
    if i % 15 == 0:
        print("FizzBuzz")
    elif i % 3 == 0:
        print("Fizz")
    elif i % 5 == 0:
        print("Buzz")
    else:
        if i % 15 != 0:
            print(i)

FizzBuzz In PHP

<?php 
n=100;
for ($i=1; $i<=n; $i++) {
	$output = '';
	if ($i % 3 == 0) {
		$output .= 'Fizz';
	}
	if ($i % 5 == 0) {
		$output .= 'Buzz';
	}
	if (!$output) {
		$output = $i;
	}
	echo $output . "\n";
}
?>
0 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

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