Last active
April 20, 2016 19:29
-
-
Save lowmess/60bbac55ee9d7034561d to your computer and use it in GitHub Desktop.
FizzBuzz
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
// @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 | |
} |
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
// @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); | |
} | |
} |
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
// @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