Skip to content

Instantly share code, notes, and snippets.

@jaysonrowe
Created January 11, 2012 01:39
Show Gist options
  • Save jaysonrowe/1592432 to your computer and use it in GitHub Desktop.
Save jaysonrowe/1592432 to your computer and use it in GitHub Desktop.
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);
}
@b0yblake
Copy link

let i = 1;
while(i <= 100) {
console.log((${i%3===0?'Fizz':''}${i%5===0?'Buzz':''}) || i)
i++;
}``

@b0yblake
Copy link

b0yblake commented May 31, 2023

Using currying is highly technical but for some reason, I think is not more reliable than one-line code (It divides so many fn to solved). I just think we can use HOC or using closures to limit the context.

@essenmitsosse
Copy link

essenmitsosse commented May 31, 2023

If you want fun and games:

new Array(100)
    .fill()
    .map((_,i) => `${i%3===0?'Fizz':''}${i%5===0?'Buzz':''}`)
    .forEach(console.log)

or a little more testable

const fizzbuzz = (_,i) => `${i%3===0?'Fizz':''}${i%5===0?'Buzz':''}`

new Array(10).fill().map(fizzbuzz).forEach(console.log)

@Megafry
Copy link

Megafry commented Jun 1, 2023

@essenmitsosse

The use of forEach is not necessary here since map already traverses the array.
But I like how you used fill

new Array(100).fill().forEach((_,i) => {
    i++
    console.log((`${i%3===0?'Fizz':''}${i%5===0?'Buzz':''}`) || i)
})

@essenmitsosse
Copy link

@Megafry If you want fizzbuzz to be side-effect free, it still makes sense to separate the two. Also, I usually wouldn't expect map to have a side effect — that's what forEach is for. It is an additional traversal, but in most cases, the gain in clarity should outweigh the performance hit.

@mortocks
Copy link

Not the smallest / most elegant solution but when that pesky product manager wants to add "boop" on 7 it won't require a refactor

const fizzBuzz = (duration = 100) => Array(duration).fill().map((_, i) => {
  i++
  const subs = new Map([[15, "Fizzbuzz"], [3, "Fizz"], [5, "Buzz"] ])
  const match = Array.from(subs.keys()).find(k => i % k === 0)
  return match ? `${subs.get(match)}` : `${i}`;
})

console.log(fizzBuzz())

@nehalkadyan
Copy link

for (var i = 1; i <= 100; i++) {
switch (true) {
case i % 5 == 0 && i % 3 == 0:
console.log("FizzBuzz");
break;
case i % 5 == 0 && i % 3 != 0:
console.log("Buzz");
break;
case i % 3 == 0 && i % 5 != 0:
console.log("Fizz");
break;
default:
console.log(i);
break;
}
}

Thanks man, was looking for switch case implementation on this one.

@persianturtle
Copy link

for (let i = 1; i <= 100; i++) {
  const fizz = i % 3 === 0 ? "Fizz" : ""
  const buzz = i % 5 === 0 ? "Buzz" : ""
  console.log(`${fizz}${buzz}` || i)
}

@Wlad2000
Copy link

Wlad2000 commented Feb 1, 2024

const fizzBuzz = (num, rules) => {
  const result = [];
  for (let i = 1; i <= num; i++) {
    result.push(applyRules(i, rules));
  }
  return result;
};

const applyRules = (num, rules) => {
  const output = rules.reduce((result, rule) => {
    if (rule.condition(num)) {
      result += rule.output;
    }
    return result;
  }, '');

  return output || num;
};
 // Example usage:
  const isMultipleOf = (n) => (num) => num % n === 0;
  const rules = [
    { condition: isMultipleOf(3), output: 'Fizz' },
    { condition: isMultipleOf(5), output: 'Buzz' },
  ];
    const resultArray = fizzBuzz(15, rules);
    console.log(resultArray);

@GTRshaoran
Copy link

//para imprimir en la página web const n= window.prompt("Ingrese un número: "); 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); }

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