Skip to content

Instantly share code, notes, and snippets.

@itotallyrock
Last active July 31, 2017 20:34
Show Gist options
  • Save itotallyrock/2587b366dd8623b64b7d2efa074c2e84 to your computer and use it in GitHub Desktop.
Save itotallyrock/2587b366dd8623b64b7d2efa074c2e84 to your computer and use it in GitHub Desktop.
function getNum(input) {
let conditions = [{num: 3, word: 'Fizz'}, {num: 5, word: 'Buzz'}]
return conditions.map(({num, word}) => {
return input % num === 0 ? word : '#'
}).join('').replace(/#+/, input).replace(/(\d+)?([A-z]+)(\d+)?/g, '$2')
}
function getNum(num) {
return num % 3 === 0 ? (num % 5 === 0 ? 'FizzBuzz' : 'Buzz') : (num % 5 === 0 ? 'Fizz' : num.toString())
}
@itotallyrock
Copy link
Author

Adding more condtions is easy, just add another object to the conditions variable.
For example:

let conditions = [
  { num: 3, word: 'Fizz' },
  { num: 5, word: 'Buzz' },
  { num: 7, word: 'Boop' }
];

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment