xxxxxxxxxx
// Write a program that console logs the numbers
// from 1 to n. But for multiples of three print
// “fizz” instead of the number and for the multiples
// of five print “buzz”. For numbers which are multiples
// of both three and five print “fizzbuzz”.
// --- Example
// fizzBuzz(5);
// 1
// 2
// fizz
// 4
// buzz
function fizzBuzz(n) {
for (let i = 1; i <= n; i++) {
let str = "";
if (i % 3 === 0) {
str += "fizz";
}
if (i % 5 === 0) {
str += "buzz";
}
if ((i % 5 != 0 && i % 3 != 0) || str === "") {
str = i;
}
console.log(str);
}
}
xxxxxxxxxx
for (let i = 1, msg; i <= 100; i++, msg = '') {
if (!(i % 3)) msg += 'Fizz';
if (!(i % 5)) msg += 'Buzz';
console.log(msg || i);
}
xxxxxxxxxx
for i in range(1, 101):
if i % 3 == 0 and i % 5 == 0:
i = "FizzBuzz"
elif i % 3 == 0:
i = "Fizz"
elif i % 5 == 0:
i = "Buzz"
print (i)
xxxxxxxxxx
// Write a program that console logs the numbers
// from 1 to n. But for multiples of three print
// “fizz” instead of the number and for the multiples
// of five print “buzz”. For numbers which are multiples
// of both three and five print “fizzbuzz”.
// --- Example
// fizzBuzz(5);
// 1
// 2
// fizz
// 4
// buzz
function fizzBuzz(n) {
for (let index = 1; index <=n; index++) {
if(index%5===0 && index%3===0){
console.log("fizzbuzz");
}else if(index%5===0){
console.log("buzz");
}
else if(index%3===0){
console.log("fizz");
}else {
console.log(index);
}
}
xxxxxxxxxx
for (let i = 1; i <= 100; i++) {
let f = i % 3 == 0,
b = i % 5 == 0;
console.log(f ? (b ? 'FizzBuzz' : 'Fizz') : b ? 'Buzz' : i);
}
xxxxxxxxxx
import java.util.Scanner;
public class FizzBuzz {
public static void main (String[] args){
Scanner userInput = new Scanner(System.in);
int n = userInput.nextInt();
if (n%3==0&& n%5==0) {
System.out.println("FizzBuzz");
} else if(n%5==0) {
System.out.println("Buzz");
} else if(n%3==0 ) {
System.out.println("Fizz");
}
}
}
FizzBuzz with flexibility, docstrings, try...except, alternatives,
xxxxxxxxxx
def to_fizz_buzz_form(fizz:str="Fizz", buzz:str="Buzz", rg:int=100)-> None:
""" A working solution for FizzBuzz problem. """
try:
fizz, buzz, rg = str(fizz).title(), str(buzz).title(), int(rg)
except Exception as err:
print(f"Error: {err}. Terminated prematurely...")
fizz_buzz = fizz + buzz
for i in range(1,rg):
a = i % 3 == 0 # True or False
b = i % 5 == 0 # True or False
print( fizz_buzz if a and b else fizz if a else buzz if b else i)
"""Inactive Tests:"""
# to_fizz_buzz_form() # uses default values: "Fizz", "Buzz", 100
# to_fizz_buzz_form(list(), dict(), "20") # "[]", "{}", "16", <- Weird Edge Case, I was bored obviously!
# to_fizz_buzz_form("super", "mario", 31) # Super, Mario, SuperMario, i <- Reg Test
xxxxxxxxxx
for (var i = 1; i < 101; i++) {
if (i % 6 == 0) console.log("FizzBuzz");
else if (i % 2== 0) console.log("Fizz");
else if (i % 3 == 0) console.log("Buzz");
else console.log(i);
}
xxxxxxxxxx
package main
import "fmt"
func main() {
for i := 0; i < 100; i++ {
fmt.Println(i)
if i % 3 == 0 {
fmt.Println(i, "Fizz")
}else if i % 5 == 0{
fmt.Println(i, "Buzz")
}
}
}