Skip to content

Instantly share code, notes, and snippets.

@CodeLeom
Created February 3, 2023 11:01
Show Gist options
  • Save CodeLeom/e01c89591c467542b1d786f9feb40b7f to your computer and use it in GitHub Desktop.
Save CodeLeom/e01c89591c467542b1d786f9feb40b7f to your computer and use it in GitHub Desktop.
This gist is an answer to, write the fizzbuzz javascript algorithm which returns an array of strings from 1 to N but for multiples of 3, print Fizz, multiples of 5, print Buzz, multiples of 3 and 5, print fizz buzz
function fizzbuzz(n) {
let result = [];
for (let i = 1; i <= n; i++) {
let output = "";
if (i % 3 === 0) {
output += "Fizz";
}
if (i % 5 === 0) {
output += "Buzz";
}
if (output === "") {
output += i;
}
result.push(output);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment