-2

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;
}
gunniq
  • 21
  • 5
  • Does this answer your question? [Palindrome check in Javascript](https://stackoverflow.com/questions/14813369/palindrome-check-in-javascript) – Mahyar Mottaghi Zadeh Feb 07 '21 at 21:57
  • 3
    Can you demonstrate *any* effort at solving this yourself? – Scott Hunter Feb 07 '21 at 21:58
  • i can't figure out what is 'recursive function', so i thought ur help rewriting my function would help me understand :( – gunniq Feb 07 '21 at 22:00
  • `how do i write my function in recursive way` .... you dont need recursion for this problem. You *do need* some functionality that determines the reverse of the string for comparison – GetSet Feb 07 '21 at 22:01
  • but my book says find out how to write it in recursive way :( – gunniq Feb 07 '21 at 22:01
  • `but my book says find out how to write it in recursive way` .... I'd suggest better books – GetSet Feb 07 '21 at 22:02
  • Be that as it may, here is a logical error `cleanStr.length / 2` since by definition a palindrome is spelled same backwards and forwards. There is no mention of half of it. E.g. "aba" which is not a real word, is true on test case. But you can't assume even amount of letters. – GetSet Feb 07 '21 at 22:05
  • 1
    A recursive function is a function which will call itself in some way. There are tons of excamples for recursive functions. https://www.javascripttutorial.net/javascript-recursive-function/ – FelHa Feb 07 '21 at 22:05
  • i've read google and what it is, so when i come back to my function im like - omg, is this even possible to write it another way? – gunniq Feb 07 '21 at 22:07
  • Seems like a dumb (or too smart) question to ask for recursiveness @gunniq. It can be done recursively. It wouldn't be "pure" though, as you need an "if" condition on even/odd of length source – GetSet Feb 07 '21 at 22:13
  • 1
    @GetSet No, you don't need to handle strings of even/odd length any different from each other. You just need to adjust the base case accordingly. – Bergi Feb 07 '21 at 22:17
  • Kinda disagree on that logic @Bergi. Assuming Base 0. Only an odd length string can be split at the start point for recursion. If even length, then the two middle chars must be the same. – GetSet Feb 07 '21 at 22:22
  • 2
    @GetSet The trick is not to start in the middle… – Bergi Feb 07 '21 at 23:18
  • True see your point @Bergi. Start at the beginning and end. Yes from that perspective could be "pure". But i probably am still stuck in not solving this recursively. My comments were on OP attempted solution anyhow. – GetSet Feb 07 '21 at 23:36

1 Answers1

2

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.

Ian
  • 1,475
  • 2
  • 15
  • 31