Skip to content

Instantly share code, notes, and snippets.

@lowmess
Last active April 20, 2016 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lowmess/60bbac55ee9d7034561d to your computer and use it in GitHub Desktop.
Save lowmess/60bbac55ee9d7034561d to your computer and use it in GitHub Desktop.
FizzBuzz
// @link http://codepen.io/lowmess/pen/JXvNVp?editors=0011
// this is a bad idea.
Array.prototype.fizzbuzz = function () {
for (var i = 1; i <= this.length; i++) {
var fizzBuzzString = ''
if (i % 3 === 0) fizzBuzzString = 'Fizz'
if (i % 5 === 0) fizzBuzzString += 'Buzz'
if (fizzBuzzString !== '') this[i - 1] = fizzBuzzString
}
return this
}
// @link http://codepen.io/lowmess/pen/zqEJLp
ol, ul {
list-style-type: none;
counter-reset: fizzbuzz;
}
li {
counter-increment: fizzbuzz;
&:nth-child(3n):before {
content: 'Fizz';
}
&:nth-child(5n):after {
content: 'Buzz';
}
&:not(:nth-child(3n)):not(:nth-child(5n)):before {
content: counter(fizzbuzz);
}
}
// @link http://codepen.io/lowmess/pen/wGBVXP?editors=0011
function fizzBuzz (num, isConsole) {
var ul, li
num = Math.round(parseInt(num, 10))
if (isConsole === null || typeof isConsole === 'undefined') isConsole = false
if (isNaN(num)) {
console.log('Function expects number.')
return
}
if (num < 1) {
console.log('Please enter a number higher than 0.')
return
}
if (!isConsole) {
ul = document.createElement('ul')
ul.classList.add('fizzbust__list')
}
for (var i = 1; i <= num; i++) {
var fizzBuzzString = ''
if (i % 3 === 0) fizzBuzzString = 'Fizz'
if (i % 5 === 0) fizzBuzzString += 'Buzz'
if (fizzBuzzString === '') fizzBuzzString = i
if (!isConsole) {
li = document.createElement('li')
li.innerHTML = fizzBuzzString
ul.appendChild(li)
} else {
console.log(fizzBuzzString)
}
}
if (!isConsole) document.body.appendChild(ul)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment