reverse string recursively in javascript

preview_player
Показать описание
Write a recursive function called reverse which accepts
a string and returns a new string in reverse
Рекомендации по теме
Комментарии
Автор

Absolutely excellent, I was struggling to articulate this but you've laid it out so clearly!

pmcd-stvi
Автор

Just to add one point, this is a call stack, so the function that comes last is called first, which is the last reverse function with empty input. Then it goes from the bottom to the top.

ericzheng
Автор

I have a better understanding of what's going on than I did before I came to this video, but I am still a bit confused.

I don't understand where the actual reversal is taking place, and getting stored until the final product is returned.

buddhahead
Автор

"The reverse of 'ppy' is 'why pee pee?' lmao

nanonkay
Автор

God bless you ma'am..
Really needed this

martins
Автор

Nice explanation u can also show by using debugger

saranbhoopathy
Автор

Thank you for your detailed explanation!

nauilnil
Автор

Yeah, I make small steps to understand recursive function. Your explication it's very close related on how stack events are working in js, last in first out. Thank you!

valikonen
Автор

Thanks, I have better understanding now on how recursive function and call stack are working

hasanzahran
Автор

Don't know why but i found this solution with slice a bit confusing. After spending some time i came up with the following kinda more verbose (although it requires an optional argument):

function reverseString(string, n = string.length - 1) {
let newString = '';
if (n == 0) return newString + string[0];
else {
newString += string[n]
return newString + reverseString(string, n - 1)
}
}


stefkoum
welcome to shbcf.ru