Created
December 16, 2017 10:13
-
-
Save razashoaib/3f52661d32f46ff812b26b2a2a8e5bdb to your computer and use it in GitHub Desktop.
Look and say sequence in javascript.
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(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