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