Skip to content

Instantly share code, notes, and snippets.

@ekarudianto
Created June 4, 2017 03:47
Show Gist options
  • Save ekarudianto/51d94f4636f71b51a6adf8939746dbf0 to your computer and use it in GitHub Desktop.
Save ekarudianto/51d94f4636f71b51a6adf8939746dbf0 to your computer and use it in GitHub Desktop.
/**
* @message {String}
*/
function myFunction(message) {
// Need the index counter which I'll mention
// the reason on below comment
var printString = function(str, i) {
if(i === 0) return;
i = i || 1;
/**
* Checkpoint where function is printing all the words.
* In this case, if user pass in 'abc' then this checkpoint
* is checking if function already printing whole 'abc'
*
* If so then return by calling self function
* with new negative index so the printing will be in reverse
*/
if(i > str.length) return printString(str, -1 * str.length + 1);
// Math.abs is required to convert negative number to positive
console.log(str.substr(0, Math.abs(i)));
return printString(str, i+1) ;
}
if (!message || message && typeof message != 'string') return;
return printString(message);
}
/**
* It will print :
*
* a
* ab
* abc
* ab
* a
*/
myFunction('abc');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment