Skip to content

Instantly share code, notes, and snippets.

@kerryboyko
Created January 24, 2018 19:18
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kerryboyko/9e9f7039059117221d76fa32a47df3d3 to your computer and use it in GitHub Desktop.
Save kerryboyko/9e9f7039059117221d76fa32a47df3d3 to your computer and use it in GitHub Desktop.
Would you like to build a function?
// Hey Anna, wanna write a function that returns the output for a look and say sequence: https://en.wikipedia.org/wiki/Look-and-say_sequence
function lookAndSaySequence(n) {
// would you like to build a function?
// a function that returns a striiiing.
var lookSay = function(strNum) {
var sayArray = [];
var repeats = 1; // we check to see how many nums
var digit = 0; // and say the num
for (var i = 0; i < strNum.length; i++) {
if (!!strNum.charAt(i + 1) && strNum.charAt(i) === strNum.charAt(i +
1)) { // and if they are the saaaaaame?
repeats++; // if they are we take this, and increment
} else {
digit = strNum.charAt(i); // else digit is the char at IIIIII!
sayArray.push(repeats);
sayArray.push(digit);
repeats = 1;
}
}
return sayArray.join(""); // would you like to build a function.
// a for-loop non-recursive function...
};
// okay, bye.
var output = ''; // Would you like to have an output!
// have an output as a striiiing.
for (var i = 1; i <= n; i++) { // and now we're going to iterate
// say, ain't it great, we can count up to the thiiiing!
if (i === 1) {
output = '1'; // here we have our edge case.
// when i is 1
// and build from there to the sky!!!!
} else {
output = lookSay(output); // then we iterate the output...
}
}
// a stringy super-awesome output!
return output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment