I've attempted a recursive string reversal below:
function reverse(str){
var results =[];
var j =0;
if(str.length === 0){
console.log('this is zero, yo');
return results.join('');
}
results[j] = str[str.length -1];
console.log('results: ' + results);
j++;
var next = str.substring(0,str.length -1);
console.log(next);
return reverse(next);
}
try{
console.log('***');
console.log(reverse('testing'));
}
catch(e){
console.log('blew the stack');
}
unfortunately, results is being set to an empty string the last time the function runs. Should I create an inner function that returns results
, so it's not set to an empty string? Is this code close?
edit: this is for curiosity's sake, i'm trying not to use the functions that make it really easy (reverse())