Created
February 3, 2019 15:21
-
-
Save jorenbroekema/db3d17f7ce9d6e9450d572a2260c0605 to your computer and use it in GitHub Desktop.
Quick fizzbuzz examples different levels
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// FIZZBUZZ Simple | |
for (var i = 1; i <= 100; i++) { | |
var fizz = 3; | |
var buzz = 5; | |
if (i % 3 === 0 && i % 5 === 0){ | |
console.log('FizzBuzz'); | |
} else if (i % 3 === 0) { | |
console.log('Fizz'); | |
} else if (i % 5 === 0) { | |
console.log('Buzz'); | |
} else { | |
console.log(i); | |
} | |
} | |
// FIZZBUZZ Intermediate | |
for (var i = 1; i <= 100; i++) { | |
var fizz = 3; | |
var buzz = 5; | |
var input = ''; | |
if (i % 3 === 0) { | |
input += 'Fizz'; | |
} | |
if (i % 5 === 0) { | |
input += 'Buzz'; | |
} | |
if (input === ''){ | |
input = i; | |
} | |
console.log(input); | |
} | |
// FIZZBUZZ Advanced | |
for (var i = 1; i <= 100; i++) { | |
var words = [ | |
{'Fizz': 3}, | |
{'Buzz': 5}, | |
{'Bar': 6}, | |
]; | |
var input = ''; | |
words.forEach(function(word) { | |
var objs = Object.entries(word)[0]; | |
if (i % objs[1] === 0) { | |
input += objs[0]; | |
} | |
}); | |
if (input === ''){ | |
input = i; | |
} | |
console.log(input); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment