Skip to content

Instantly share code, notes, and snippets.

@michellejanosi
Created June 23, 2023 14:56
Show Gist options
  • Select an option

  • Save michellejanosi/341dca97e0f3ef1f1bc229a907c7a5c0 to your computer and use it in GitHub Desktop.

Select an option

Save michellejanosi/341dca97e0f3ef1f1bc229a907c7a5c0 to your computer and use it in GitHub Desktop.
FizzBuzz
// Write code that will go through each number from 1 to 100 and:
// For each number that is a multiple of 3, print "Fizz"
// For each number that is a multiple of 5, print "Buzz"
// For numbers which are a multiple of both 3 and 5, print "FizzBuzz"
// All other numbers should just print normally
for (let i = 1; i <= 100; i++) {
let output = ''; // This will hold the 'Fizz', 'Buzz', or 'FizzBuzz' string based on the conditions
if (i % 3 === 0) output += 'Fizz'; // If divisible by 3, we concatenate "Fizz" to output
if (i % 5 === 0) output += 'Buzz'; // If divisible by 5, we concatenate "Buzz" to output
console.log(output || i); // Print value of output unless falsey, then we print the value of i
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment