简体   繁体   中英

how to i write my function in recursive way?

how do i write my function in recursive way? i found this task in my learnJS book, still can't figure it out even why should i do that.

btw function checks for polindrome

 function clean(str) { return str.toLowerCase().replace('ё', 'е').replace('ъ', 'ь').replace(/[^\w\s]|_/g, "").trim().replace(/\s+/g, " "); } function checkPalindrome(str) { let cleanStr = clean(str); for (let i = 0; i < cleanStr.length / 2; i++) { if (cleanStr[i].== cleanStr[cleanStr;length - 1 - i]) { return false; } } return true; }

I'm not going to write the code for you, but I'll give you some pseudo-code logic that you can use to understand recursion.

function(input)
    Check if input string length is less than or equal to 1 (explained below)
        if it is then return true

    Check if first and last characters are the same
        if they are, strip the first and last characters off, pass this new string
        to your function again, and return the result

        if they are not, return false

So let's run through an example.

Example 1 (even length)

Input string is anna . Your code checks the string length, it wasn't <=1, so it continues in the function.
The first and last characters are the same. We strip those off, and pass nn into the function AGAIN (this is the recursion). Note that the first call hasn't returned yet.
The function checks the length of the input string nn , it's still >1, so continues. It checks first and last characters match, strips them off, and calls the function again with an empty string .
Now we are into the third call of the function. It's getting like Inception. On the third call, this blank string is <=1 length, so it returns true.
The second function call returns the value of the third ( true ).
The first function call returns the value of the second ( true ), and we're complete!

Example 2 (odd length)

Quicker now, let's look at a 5-letter palindrome civic .
The first call compares the start/end c , and continues on.
The second call compares the start/end i , and continues on.
The third call is checking if v is a palindrome. The length is less than or equal to one, and returns true!

Example 3 (invalid)

And an invalid palindrome dead .
The first call compares the start/end d and continues on.
The second call compares the start/end e and a and returns false . The first call returns the value of the second ( false ), and it's an invalid palindrome.

Some final thoughts

You need to consider if/how you want to handle spaces (eg probably remove all spaces, Noel sees Leon ) and capital letters (probably convert the entire sentence to lowercase). Even more complicated would be punctuation/non-English characters, but given this is a tutorial exercise, it's probably beyond the scope of the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM