Skip to content

Instantly share code, notes, and snippets.

@razashoaib
Created December 16, 2017 10:13
Show Gist options
  • Save razashoaib/3f52661d32f46ff812b26b2a2a8e5bdb to your computer and use it in GitHub Desktop.
Save razashoaib/3f52661d32f46ff812b26b2a2a8e5bdb to your computer and use it in GitHub Desktop.
Look and say sequence in javascript.
function lookAndSay(number) {
var regex = /(.)\1*/g;
return number.toString().replace(regex, function(regexMatch) {
return regexMatch.length.toString() + regexMatch.substring(0, 1);
});
}
// Usage
function init() {
var number = 1;
for (var i = 0; i < 6; i++) {
number = lookAndSay(number);
console.log(number);
}
}
/* Output for init()
11
21
1211
111221
312211
13112221
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment