Created
August 11, 2014 11:39
-
-
Save IQAndreas/817a1eac7740c2a020fa to your computer and use it in GitHub Desktop.
The "Look and Say Sequence" in JavaScript. See: https://www.youtube.com/watch?v=ea7lJkEhytA
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
function lookAndSay(num) { | |
var look = /(1+|2+|3+)/g; | |
function say(match, position) { | |
return match.length.toString() + match.substring(0, 1); | |
} | |
return num.toString().replace(look, say); | |
} | |
// Usage | |
var num = 1; | |
for (var i:int = 0; i < 20; i++) { | |
num = lookAndSay(num); | |
console.log(num); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The script assumes the following:
but can easily be expanded to allow for "non-standard" seeds.