Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
FizzBuzz JavaScript solution
for (var i=1; i <= 20; i++)
{
if (i % 15 == 0)
console.log("FizzBuzz");
else if (i % 3 == 0)
console.log("Fizz");
else if (i % 5 == 0)
console.log("Buzz");
else
console.log(i);
}
@Ankur-Singh01
Copy link

Ankur-Singh01 commented Mar 3, 2021

Using if-else & while loop.

`let i=1;
while (i<=50) {

function fizzBuzz() {
if(i%3===0 && i%5===0){
console.log("fizzBuzz");
}else if(i%3===0){
console.log("fizz");
}else if(i%5===0){
console.log("buzz");
}else{
console.log(i);
}

 i++;

}
fizzBuzz();

}`

@Ankur-Singh01
Copy link

Ankur-Singh01 commented Mar 3, 2021

using for loop

for (let i=1; i<=100; i++){
if(i%3===0 && i%5===0){
console.log("fizzBuzz");
}else if(i%3===0){
console.log("fizz")
}else if(i%5===0){
console.log("buzz")
}else{
console.log(i);
}
}

@Ankur-Singh01
Copy link

Ankur-Singh01 commented Mar 3, 2021

Advance level 🏆

let a3=1;
let a5=1;

for(let x=1; x<=100; x++){
a3++;
a5++;
let b = ''
if(a3 === 3){ b+= 'fizz'; a3 =0;}
if(a5 === 5) {b+= 'buzz'; a5 = 0;}
if(a3===3 && a5===5){b+=""; }
if(b === ''){
console.log(x)
}else{
console.log(b);
}
}

@bjangid20
Copy link

bjangid20 commented Mar 19, 2021

function main(){
debugger;
var c3 = 0;
var c5 = 0;
for(var i=1 ; i<= 50; i++){
c3++;
c5++;
var d= "";
if(c3 == 3) {d+= "fizz"; c3 = 0}
if(c5 == 5) {d+= "buzz"; c5 = 0}
if(d == ""){
console.log(i);
}else{
console.log(d)
}
}
}

@vladbarcelo
Copy link

vladbarcelo commented Mar 22, 2021

Using math!

for(let i=0;i<100;i++)console.log({1: i, 6: 'fizz', 10: 'buzz', 0: 'fizzbuzz'}[i**4%15])

@FeelHippo
Copy link

FeelHippo commented Apr 4, 2021

I'm not a big fan of one liners, future me always appreciates my past effort to make my code readable:

const fizzBuzzHelper = n => (!(n % 3) && !(n % 5) && 'FizzBuzz') || (!(n % 3) && 'Fizz') || (!(n % 5) && 'Buzz') || n
console.log([...Array(100)]
            .map(
              (element, index) => fizzBuzzHelper(index + 1)
            ))

I've seen some pretty interesting interpretations of fizzBuzz in this commentary, though
for example this and this other one

@Tioz1988
Copy link

Tioz1988 commented Apr 16, 2021

My solution as a beginner:

for (let i =1; i<101;i++) {
i % 15 === 0 ? console.log('FizzBuzz') :
i % 5 === 0 ? console.log('Buzz') :
i % 3 === 0 ? console.log('Fizz') :
console.log(i)
}

@night-lake
Copy link

night-lake commented Apr 24, 2021

def fizzbuzz(num):
    if num % 3 == 0 and num % 5 != 0:
        return print("Fizz")
    elif num % 5 == 0 and num % 3 != 0:
        return print("Buzz")
    elif num % 5 == 0 and num % 3 == 0:
        return print("FizzBuzz")
    else:
        return print(num)

for x in range(100):
    fizzbuzz(x)

@chisomloius
Copy link

chisomloius commented Apr 27, 2021

let i;
(for i=1; i <=100; i++){
   if(i % 15 == 0){
    console.log('FizzBuzz')
  }else if(i % 3 == 0){
    console.log('Fizz')
  }else if(i % 5 == 0){
    console.log('Buzz')
  }else{
   console.log(i)
  }
}

@r17x
Copy link

r17x commented May 19, 2021

Pattern-Matching-Style

const FizzBuzz = n => ({
  true: n, 
  [ n % 5 === 0]: "Buzz",
  [ n % 3 === 0]: "Fizz",
  [ n % 15 === 0]: "FizzBuzz",
  [ n === 0 ]: 0, 
}).true

const ThisIsAFizzBuzz = n => [...Array(n).keys()].forEach(n => console.log("Result: ", FizzBuzz(n))) 

@maciejwerner
Copy link

maciejwerner commented May 29, 2021

My solution:

for (let i = 1; i <= 100; i++) {
let string = i;

string = (i % 3 === 0) ? "fizz" : string;
string = (i % 5 === 0) ? "BUZZ" : string;
string = (i % 15 === 0) ? "fizzBUZZ" : string;

console.log(string)
}

@axeleli
Copy link

axeleli commented Jun 9, 2021

my shortest:
console.log(i%15?i%3?i%5?i:'Buzz':'Fizz':'FizzBuzz')

@KiselevAlexander
Copy link

KiselevAlexander commented Jun 10, 2021

const FizzBuzz = (x) => ((x % 3 === 0 ? 'Fizz' : '') + (x % 5 === 0 ? 'Buzz' : '')) || x;
console.log([...new Array(40)].map((i, k) => [k + 1, FizzBuzz(k + 1)]));

@Muhammadaziz107
Copy link

Muhammadaziz107 commented Jul 11, 2021

function fizzBuzz(number) {
  if (number % 3 === 0 && number % 5 === 0) {
    return "Fizz Buzz";
  } else if (number % 3 === 0) {
    return "Fizz";
  } else if (number % 5 === 0) {
    return "Buzz";
  } else {
    return number;
  }
}

@hari2anand
Copy link

hari2anand commented Jul 12, 2021

My solution, doesn't loop for 100 times, instead gets the results in 27 iterations, try the following,

let i = 0
let j = 1
let limit = 100
//prints zero
console.log(i);
while (true) {
    if (i > limit - 3) break; //can also kept in loop; but I always enjoy infinte loops breaking ;-) and have to subtract with 4 if needed till 99
    i = i + 3;

    //prints for number divisible by 5 that falls before the multiples of 3 lags one more step to reach like 10 needs 9->10
    (i - 2) % 5 === 0 ? console.log(i - 2, " - Buzz") : console.log(i - 2);

    //prints for number divisible by 5 that falls before the multiples of 3 needs lags one step to behind like 5 needs 5<-6
    (i % 5 === 1) ? console.log(i - 1, " - Buzz"): console.log(i - 1);

    //prints if the number is divisble by both 3 and 5
    if (i % 5 === 0) {
        console.log(i, " - FizzBuzz");
        continue
    }

    //prints default for 3 multiples
    console.log(i, " - Fizz")

    j++ //Loop Counter

}

console.log('Total iterations ran: ', j) 

I know the following is not the shortest written code, but it implies arithmetic logic to ensure the code sequence completes in shorter iterations for a given limit/interval :)

let i = 0
let j = 1
let limit = 150
//prints zero
console.log(`${i}`);
while (limit < 3) {
    i++;
    if (i > limit) break
    console.log(`${i}`)
}
while (true) {
    if (i > limit - 3) break; //can also kept in loop; but I always enjoy infinite loops breaking ;-) and have to subtract with 4 if needed till 99
    i = i + 6;

    //prints for number divisible by 5 that falls before the multiples of 3 lags one more step to reach like 10 needs 9->10
    (i - 5 <= limit) ? (i - 5) % 5 === 0 ? console.log(`${i - 5} - Buzz`) : console.log(`${i - 5}`) : console.log();

    //prints for number divisible by 5 that falls before the multiples of 3 needs lags one step to behind like 5 needs 5<-6
    (i - 3 <= limit) ? ((i - 3) % 5 === 1) ? console.log(`${i - 4} - Buzz`) : console.log(`${i - 4}`) : console.log();

    (i - 8 <= limit) ? ((i - 8) % 5 === 0) ? console.log(`${i - 3} - FizzBuzz `) : console.log(`${i - 3} - Fizz `) : console.log();

    (i - 2 <= limit) ? (i - 2) % 5 === 0 ? console.log(`${i - 2} - Buzz`) : console.log(`${i - 2}`) : console.log();

    //prints for number divisible by 5 that falls before the multiples of 3 needs lags one step to behind like 5 needs 5<-6
    ((i-1) <= limit) ? (i % 5 === 1) ? console.log(`${i - 1} - Buzz`) : console.log(`${i - 1}`) : console.log();


    //prints if the number is divisble by both 3 and 5
    if ((i <= limit) && (i % 5 === 0)) {
        console.log(`${i} - FizzBuzz`);
        continue
    }

    //prints default for 3 multiples

    (i <= limit) ? console.log(`${i} - Fizz`) : console.log()

    j++ //Loop Counter

}


console.log(
    `Total iterations ran: ${j}`
);

It takes only 15cycles to print fizz/buzz/fizzbuzz for a 100 iteration :)

@YasirHasn9
Copy link

YasirHasn9 commented Aug 1, 2021

short modern js solution

function fizzBuzz(n){
     for(let i = 0 ; i < n ; ){
            console.log( 
                   (++i % 3 ? "" : "fizz") + (i % 5 ? "" : "buzz") || i
                      )
       }
}

@Mahmud-cse
Copy link

Mahmud-cse commented Aug 6, 2021

My solution:

function fizzbuzz(num){
    if(num%3==0 && num%5==0){
        console.log('fizzbuzz');
        return;
    }else if(num%3==0){
        console.log('fizz');
    }else if(num%5==0){
        console.log('buzz');
    }
}

for(let i=1;i<=100;i++){
    fizzbuzz(i);
}

@CharlieMaskell
Copy link

CharlieMaskell commented Aug 10, 2021

Thought I'd take a crack at it:

var multipliers = [
  [3, "Fizz"],
  [5, "Buzz"]
]

for (let i = 1; i <= 100; i++) {
  let output = "";

  multipliers.forEach(function(item) {
     if (i % item[0] === 0) {
       output += item[1];
     }
  }); 

  console.log(output || i);
}

@GoroJR
Copy link

GoroJR commented Aug 15, 2021

/FizzBuzz
Write a program that uses console.log to print all the numbers from 1 to 100,
with two exceptions. For numbers divisible by 3, print "Fizz" instead of the
number, and for numbers divisible by 5 (and not 3), print "Buzz" instead.
When you have that working, modify your program to print "FizzBuzz" for
numbers that are divisible by both 3 and 5 (and still print "Fizz" or "Buzz"
for numbers divisible by only one of those).
(This is actually an interview question that has been claimed to weed out
a significant percentage of programmer candidates. So if you solved it, your
labor market value just went up.)
/
for(let i=1;i<=100;i++, msg="") { if (i%3==0) console.log('Fizz' + i); else if (i%5==0) console.log('Buzz'+ i); if (i%15==0) console.log('FizzBuzz'+ i); else console.log(i)

@LittleDeveloper-CSharp
Copy link

LittleDeveloper-CSharp commented Sep 30, 2021

function x(y){ if(y % 3 == 0 && y % 5 == 0) return 'FizzBuzz'; if(y % 3 == 0) return 'Fizz'; if(y % 5 == 0) return 'Buzz'; return y; } for(let i = 1; i <= 100; i++) console.log(x(i));

@AndresCardonaDev
Copy link

AndresCardonaDev commented Nov 7, 2021

Hahaha this is funny, i use Fizzbuzz in my interviews, initially to get a quick grasp of basic algorithmic basis, then i elaborate on skills by making a more complex version of the problem creating a counting game generator; anyway here are my short fizzbuzz solutions that i have written so far, seem that the shortests one, is one already accepted as the shortest but... here some guys have achieved two characters shorter https://code.golf/fizz-buzz#javascript 🤯 yet i don't know how 😆 please someone enlighten me

// A short one, an atrocity in terms of readability :D
for(i=0,b='Buzz',f='Fizz';++i<101;)console.log(i%15?i%3?i%5?i:b:f:f+b)
// The shortest i have written, yet i known there are one two characters shorter, no idea how
for(i=0;++i<101;)console.log((i%3?'':'Fizz')+(i%5?'':'Buzz')||i)
// A favorite one because it is made with recursion
f=(i=1)=>i-101?`${(i%3?'':'Fizz')+(i%5?'':'Buzz')||i}\n${f(i+1)}`:'';
console.log(f());
// And well one using array methods to iterate
console.log(Array.from({length:100}, (_,i)=>++i%15 ? i%3 ? i%5 ? i : 'Buzz' : 'Fizz' : 'FizzBuzz').join('\n'));

@Mukunda-Bam
Copy link

Mukunda-Bam commented Nov 9, 2021

//for printing in webpage
const n= window.prompt("Enter a number: ");
for(i=1; i<=n; i++)
if(i%3==0 && i%5==0){
console.log("FizzBuzz\n");
}
else if(i%3==0 && i%5!=0){
console.log("Fizz\n");
}
else if(i%3!=0 && i%5==0){
console.log("Buzz\n");
}
else{
console.log(i\n);
}

@MaryamFarshbafi
Copy link

MaryamFarshbafi commented Mar 10, 2022

//Fizz and Buzz
for(i=0; i<=100;i++){
if(i/3==0){
console.log("Fizz");
}
else if( i%5==0 && i/3!==0){
console.log("Buzz");
}

}

@Taimoor0217
Copy link

Taimoor0217 commented Mar 29, 2022

for (let i = 0 ; i < 100 ; i ++){
    let val = "";
    if( i % 3 === 0)
        val += "Fizz"
    if(i%5 ===0)
       val += "Buzz"
    console.log(val)
}

@DaCuteRaccoon
Copy link

DaCuteRaccoon commented Mar 31, 2022

Finally! I got it in 64 bytes!

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

@Guav0
Copy link

Guav0 commented Apr 22, 2022

This is my preferred way, fast and easy to read. Normal loop including the higher bound for the FizzBuzz. The ? and : are ternary operators, in (condition) ? truthy : falsy

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

@Port5k
Copy link

Port5k commented May 1, 2022

`for (let x = 1; x <= 100; x++) {

//Filter out the numbers indivisible by either 3 and 5
if (!(x % 3 == 0) && !(x % 5 == 0)) console.log(x);
//Filter out numbers divisible by 3 and 5
else if ((x % 3 == 0) && (x % 5 == 0)) console.log("FizzBuzz");
//Filter out numbers divisible by 3
else if (x % 3 == 0) console.log("Fizz");
//Filter out numbers divisible by 5
else if (x % 5 == 0) console.log("Buzz");

}`

@cynthia-nwannah
Copy link

cynthia-nwannah commented Jul 12, 2022

You guys are so amazing. I just started javascript, hope i would reach your level

@Mike-UU07
Copy link

Mike-UU07 commented Jul 31, 2022

const FizzBuzz = (range) => {
for (let i = 0; i <= range; i++) {
if (i === 0) {
console.log(i)
} else if (i % 15 === 0) {
console.log("Fizz Buzz")
} else if (i % 3 === 0) {
console.log("Buzz")
} else if (i % 5 === 0) {
console.log('Fizz')
} else {
console.log(i)
}
}
}

a quick fizzbuzz function where you can choose how many # you want to console.log
you can invoke the function by FizzBuzz() then inside the brackets how many numbers you want

@8bou3
Copy link

8bou3 commented Aug 13, 2022

Short and flexible

for(i=0;++i<101;)console.log([3,5].reduce((t,v,j)=>i%v?t+'':t+['Fizz','Buzz'][j],'')||i) // 88 characters
for(i=0;++i<101;)console.log([3,5,7,11].reduce((t,v,j)=>i%v?t+'':t+['Fizz','Buzz','Fuzz','Bizz'][j],'')||i)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment