Created
February 5, 2016 22:19
-
-
Save roneesh/7be487be02d58ff16399 to your computer and use it in GitHub Desktop.
Several crackle pop solutions
This file contains 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
// an uncommon implementation that creates a 100 character string, and replaces each character with either it's index or the crackle/pop/cracklepop | |
var s = (new Array(102).join('x')).replace(/x/g, function(match, index) { | |
return (index % 15 === 0 ? 'CracklePop\n' : (index % 5 === 0 ? 'Pop\n' : (index % 3 === 0 ? 'Crackle\n' : (index + '\n')))); | |
}); | |
console.log(s); | |
// an uncommon implementation that uses an array and it's shifting to traverse the array | |
var numbers = Object.keys(new String(new Array(102))); | |
numbers.shift(); | |
while (numbers.length) { | |
var i = numbers.shift(); | |
console.log(i % 15 === 0 ? 'CracklePop' : (i % 5 === 0 ? 'Pop' : (i % 3 === 0 ? 'Crackle' : i))); | |
} | |
// An implementation with recursion | |
(function recursiveCrackle(m) { | |
console.log(m % 15 === 0 ? 'CracklePop' : (m % 5 === 0 ? 'Pop' : (m % 3 === 0 ? 'Crackle' : m))); | |
if (m < 100) recursiveCrackle(m+1); | |
})(1) | |
// a quite common implementation with a while loop and a numerical counter | |
var number = 100; | |
while (number) { | |
console.log((number % 15 === 0 ? 'CracklePop' : (number % 5 === 0 ? 'Pop' : (number % 3 === 0 ? 'Crackle' : number)))); | |
n--; | |
} | |
//the most common implementation - a classic for loop | |
for (var i = 1; i <= 100; i++ ) { | |
console.log((i % 15 === 0 ? 'CracklePop' : (i % 5 === 0 ? 'Pop' : (i % 3 === 0 ? 'Crackle' : i)))); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment