Skip to content

Instantly share code, notes, and snippets.

@fitsum
Last active April 26, 2024 05:20
Show Gist options
  • Save fitsum/5b7599c8ab49a95f3881218265c461b3 to your computer and use it in GitHub Desktop.
Save fitsum/5b7599c8ab49a95f3881218265c461b3 to your computer and use it in GitHub Desktop.
Super accurate output
// getting the output to look exactly like what's here
// https://node-girls.gitbook.io/beginners-javascript/challenges/challenge-4-fizzbuzz
const fizzBuzz = ( start, end ) => {
const format = ( idx, output ) => {
let out = output || '';
outColors = {
true: 'color: #71a7ff',
false: 'color: orange'
}
let outColor = outColors[isNaN(parseInt(out))];
console.log(`%c${idx} %c${out}`, 'color: grey', `${outColor}; padding-left: 3px`);
}
for( let i = start; i <= end; i++ ){
let out;
switch(true){
case ( i%3===0 && i%5===0 ): format(i, "'FizzBuzz'"); break;
case i%3===0: format(i, "'Fizz'"); break;
case i%5===0: format(i, "'Buzz'"); break;
default: format(i, i); break;
}
}
}
// fizzBuzz(1,15)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment