Skip to content

Instantly share code, notes, and snippets.

@IQAndreas
Created August 11, 2014 11:39
Show Gist options
  • Save IQAndreas/817a1eac7740c2a020fa to your computer and use it in GitHub Desktop.
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
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);
}
@IQAndreas
Copy link
Author

The script assumes the following:

No digits other than 1, 2, and 3 appear in the sequence, unless the seed number contains such a digit or a run of more than three of the same digit.
-- From Wikipedia: Look and Say Sequence

but can easily be expanded to allow for "non-standard" seeds.

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