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);
}
@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); }

@iuliaGrig
Copy link

Which way would be "better regarded" as a best answer, by using for loops or by using .forEach() and .map()? Or it really does not matter as long as the program executes what is expected of it? Cheers

@essenmitsosse
Copy link

@iuliaGrig The answer is: It depends.

In this example, it's so simple that it doesn't matter for most intents and purposes. For more complex examples a rule of thumb could be:

  • .forEach and .map are generally more readable (even though plenty of people already might disagree). .forEach should be used if there are side-effects to the function (like console.log), .map must be used if you want to create a new list (.forEach doesn't return anything)
  • for-loops are generally more performant, even though the difference only really matters for very large lists

@taosx
Copy link

taosx commented Oct 9, 2024

A v8 optimized version (I'd be quite surprised if it can be made faster by keeping the same signature):

function fizzBuzz(limit = 100) {
  var result = new Array(limit),
    f = "Fizz",
    b = "Buzz",
    fb = "FizzBuzz",
    i = 0;

  for (; i + 15 <= limit; i += 15) {
    result[i] = i + 1;
    result[i + 1] = i + 2;
    result[i + 2] = f;
    result[i + 3] = i + 4;
    result[i + 4] = b;
    result[i + 5] = f;
    result[i + 6] = i + 7;
    result[i + 7] = i + 8;
    result[i + 8] = f;
    result[i + 9] = b;
    result[i + 10] = i + 11;
    result[i + 11] = f;
    result[i + 12] = i + 13;
    result[i + 13] = i + 14;
    result[i + 14] = fb;
  }

  var pattern = [0, 0, f, 0, b, f, 0, 0, f, b, 0, f, 0, 0, fb];
  for (; i < limit; i++) result[i] = pattern[i % 15] || i + 1;

  return result;
};

@devellopah
Copy link

devellopah commented Jan 12, 2025

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

    if(i % 3 === 0) {
        output += "fizz"
    }

    if(i % 5 === 0) {
        output += "buzz"
    }
    
    console.log(output || i)
}

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