## DEV Community

Theo

Posted on • Updated on

# FizzBuzz challenge in as many languages as possible

I was recently reading this post inwhich the amazing Ady Ngom was disscussing different ways to solve the common FizzBizz challenge given in interviews and how to refactor and streamline your code.

This gave me an idea.

#### What if we ask the community here to write their own solution to the FizzBuzz challenge in their given language.

What to do:

``````--[[
Write a program that prints the numbers from 1 to 100.
But for multiples of three print Fizz instead of the number,
For the multiples of five print Buzz and for numbers
which are multiples of both three and five print FizzBuzz
--]]
``````

Rules:

1. Have fun!
2. Don't copy others code, keep it unique 👍.
3. Use this format:

Language: e.g Python
Code:
Other/info: e.g Hia folks, I like cheese.

Lets see how many different languages we can get here!
(You can make it as simple or as complex as you like.) Reinhart Previano K.

Language: JavaScript
Code: gist.github.com/reinhart1010/d2b81...

Other/Info: The above code execute `console.log` command for each number. And yes, this is compiled on JSFuck based on the original code:

``````var i;
for (i = 1; i <= 100; i++){
var fb = 0;
if (i % 3 == 0){
console.log("Fizz");
fb = 1;
}
if (i % 5 == 0){
console.log("Buzz");
fb = 1;
}
if (fb == 0) console.log(i);
}
``````

Trying to decode the first snippet on JSUnFuck will likely to cause browsers to hang.

Interestingly, this one can be exported to C with minimal modifications:

``````#include <stdio.h>

int main(){
int i;
for (i = 1; i <= 100; i++){
int fb = 0;
if (i % 3 == 0){
printf("Fizz");
fb = 1;
}
if (i % 5 == 0){
printf("Buzz");
fb = 1;
}
if (fb == 0) printf("%d", i);
printf("\n");
}
}
``````

JavaScript should be welcoming to my folks who are currently learning C thanks to similarity in syntaxes. (If you're reading this, good luck in facing your mid-term exams next week!) Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻

Code:

``````fizzBuzzer :: Int -> String
fizzBuzzer i | (i `mod` 15) == 0 = "FizzBuzz"
| (i `mod` 5) == 0 = "Buzz"
| (i `mod` 3) == 0 = "Fizz"
| otherwise = show i

fizzBuzz :: IO()
fizzBuzz = mapM_ print [ fizzBuzzer i | i <- [1..100]]
`````` Language: Javascript
Code:

``````const array = Array.from(Array(100).keys()).map(it => it + 1)
.map(it => it % 3 === 0 && it % 5 === 0 ? 'FizzBuzz' : it)
.map(it => typeof it === 'number' && it % 3 === 0 ? 'Fizz' : it)
.map(it => typeof it === 'number' && it % 5 === 0 ? 'Buzz' : it)

console.log(array)
`````` Brian Vermeer 🧑🏼‍🎓🧑🏼‍💻

Language: Java (8 and above)
Code:

``````    public void fizzBuzz() {
IntStream.rangeClosed(1,100)
.mapToObj(this::fizzBuzzer)
.forEach(System.out::println);
}

private String fizzBuzzer(int i){
if (i % 15 == 0) return "FizzBuzz";
if (i % 3 == 0) return "Fizz";
if (i % 5 == 0) return "Buzz";
return String.valueOf(i);
}
`````` Olivier “Ölbaum” Scherler

Language: Erlang
Code:

Short version, generate a list (of strings and integers), don’t bother printing:

``````lists:map(
fun( X ) ->
case { X rem 3, X rem 5 } of
{ 0, 0 } -> "FizzBuzz";
{ 0, _ } -> "Fizz";
{ _, 0 } -> "Buzz";
_ -> X
end
end,
lists:seq( 1, 100 )
).
``````

Full version, print one term per line. Here we have to convert integers to strings:

``````lists:foreach(
fun( X ) ->
io:format(
"~s~n",
[
case { X rem 3, X rem 5 } of
{ 0, 0 } -> "FizzBuzz";
{ 0, _ } -> "Fizz";
{ _, 0 } -> "Buzz";
_ -> integer_to_list(X)
end
]
)
end,
lists:seq( 1, 100 )
).
`````` Thomas Leathers

Language: SSTNPL
Code:

``````var remain3=+
var remain5=+

uiter fbnum,fbloop,@1,@100
stop

label fbloop
divmod fbnum,@3
set remain3
divmod fbnum,@5
set remain5

if remain3,@0 gsub fizz
if remain3,@0 return
if remain5,@0 gsub buzz
if remain5,@0 return
dumpd fbnum
newline
return

label fizz
if remain5,@0 goto fizzbuzz
prline Fizz
return
label fizzbuzz
prline FizzBuzz
return

label buzz
prline Buzz
return
``````

Other/Info: Figured I'd contribute something a bit more obscure. :) SSTNPL is an architecture-specialized programming language thats used with my SBTCVM ternary computer simulator. Yes, its a bit clunkier-looking than most of the examples here, but mainly down to it using labeled GOTOs... Theo

Very interesing, thank you for sharing. Never heard of SSTNPL and even a quick google search doesn't render many results. 👍 Thanks! Thomas Leathers

SSTNPL is somthing i put together for SBTCVM specifically. its kinda somehwhere between an assembler (as it compiles to SBTCVM's assembly language), and C, with a bit of a primitive syntax, and a fairly static structure. Its simple, but it does have a few neat features like 2-axis array-like tables, and a module system.

as far as SBTCVM itself, the blog's about page has a good overview of it: Language: Emacs Lisp
Code:

``````(cl-defun fizzbuzz(&optional (n 100))
(cl-loop for i from 1 to n do
(print
(cond ((= (mod i 15) 0) "FizzBuzz")
((= (mod i 3) 0) "Fizz")
((= (mod i 5) 0) "Buzz")
(t i)
))
))
(fizzbuzz)

`````` Theo

Language: Python
Code:

``````def FizzBuzz():
for i in range (1,101):
#print(i/3)
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)
FizzBuzz()
`````` Language: Smalltalk
Code:

``````1 to: 100 do: [ :i |
Transcript
show:
((i isDivisibleBy: 15)
ifTrue: [ 'FizzBuzz' ]
ifFalse: [ (i isDivisibleBy: 3)
ifTrue: [ 'Fizz' ]
ifFalse: [ (i isDivisibleBy: 5)
ifTrue: [ 'Buzz' ]
ifFalse: [ i ] ] ]);
cr ]
`````` Theo

👏👏👏, never used that language. Rickard Natt och Dag

Language: ReasonML
Sketch: sketch.sh/s/XABe2ghxBqncDWTTKpNK8n/

``````module FizzBuzz = {
let get =
fun
| (0, 0, _) => "FizzBuzz"
| (0, _, _) => "Fizz"
| (_, 0, _) => "Buzz"
| (_, _, value) => string_of_int(value);
};

for (index in 1 to 100) {
print_endline(FizzBuzz.get((index mod 3, index mod 5, index)));
};
`````` Avalander

I did write solutions in Javascript and Racket for a similar challenge a while ago.

Since nobody has done Javascript yet, here's a crazy implementation.

``````const worder = (predicate, patterner) => (prev, n) =>
predicate(prev, n)
? patterner(prev, n)
: prev

const isDivisible = d => (_, n) =>
n % d === 0

const isEmpty = s =>
s.length === 0

const append = x => (prev) => prev + x

const setNumber = (_, n) => n

const fizzer = worder(isDivisible(3), append('fizz'))

const buzzer = worder(isDivisible(5), append('buzz'))

const numberer = worder(isEmpty, setNumber)

const reducer = (...worders) => n =>
worders.reduce(
(prev, w) => w(prev, n),
''
)

const fizzbuzzer = reducer(
fizzer,
buzzer,
numberer
)

for (let i = 0; i <= 100; i++) {
console.log(fizzbuzzer(i))
}
``````

Consider how easy it is to extend to print `'fazz'` for multiples of 7.

``````const fazzer = worder(isDivisible(7), append('fazz'))

const fizzbuzzfazzer = reducer(
fizzer,
buzzer,
fazzer,
numberer
)
``````

Here's my implementation in Racket

``````(define (mult-15? n)
(and (mult-5? n)
(mult-3? n)))

(define (mult-5? n)
(= (modulo n 5) 0))

(define (mult-3? n)
(= (modulo n 3) 0))

(define (fizzbuzz n)
(cond
[(mult-15? n) "FizzBuzz"]
[(mult-5? n) "Buzz"]
[(mult-3? n) "Fizz"]
[else n]))

(define (print-list xs)
(map displayln xs))

(print-list (map fizzbuzz (range 1 101)))
``````

The `print-list` function is a bit redundant, since `(map fizzbuzz (range 1 101))` will already print the resulting list to the console. Theo

Language: Lua
Code:

``````function FizzBuzz()
for i = 1,100 do
--print(i)
if i%3 == 0 and i%5 == 0 then
print("FizzBuzz")
elseif i%3 == 0 then
print("Fizz")
elseif i%5 == 0 then
print("Buzz")
else
print(i)
end
end
end
FizzBuzz()
``````

Other/info: I guess I'll start the challenge off.
This is a pretty simple function in lua of the FizzBuzz challenge. Theo

I've learnt a lot since this old thread, here's how i'd attempt this question in an interview today:

``````
-- FizzBuzz

local function isDivisible(num: number, by: integer)
return num % by == 0
end

local function result(number)
local str = ""

if isDivisible(number, 3) then
str = str.."Fizz"
end

if isDivisible(number, 5) then
str = str.."Buzz"
end

return (str ~= "" and str) or number
end

for i = 1, 100 do
print(result(i))
end
`````` Language: OCaml
Code:

``````let fizzbuzz n =
let rec loop i max div3 div5 =
if i < max then
match i = div3, i = div5 with
| true, true   -> print_endline "FizzBuzz"; loop (i + 1) max (div3 + 3) (div5 + 5)
| true, false  -> print_endline "Fizz"; loop (i + 1) max (div3 + 3) div5
| false, true  -> print_endline "Buzz"; loop (i + 1) max div3 (div5 + 5)
| false, false -> print_endline (string_of_int i); loop (i + 1) max div3 div5
in
loop 1 n 3 5

let _ = fizzbuzz 100
`````` Theo

Interesting! What kind of work is OCaml used for? 😊 Theo

Interesting.
Welcome to dev.to btw (saw you joined today).

DEV Community

## Why You Need to Study Javascript Fundamentals The harsh reality for JS Developers: If you don't study the fundamentals, you'll be just another “Coder”. Top learnings on how to get to the mid/senior level faster as a JavaScript developer by Dragos Nedelcu.