Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursive palindrome check with JavaScript

I am trying to find out whether a string is a palindrome by recursion using javascript. But I can't figure out what I am missing in the code.

var firstCharacter = function(str) {
    return str.slice(0, 1);
};

var lastCharacter = function(str) {
    return str.slice(-1);
};

var middleCharacters = function(str) {
    return str.slice(1, -1);
};

var isPalindrome = function(str) {
    if(str.length < 2) {
        return true;
    } else {
        if(firstCharacter(str) == lastCharacter(str)) {
            isPalindrome(middleCharacters(str));
        } else return false;
    }
};

var checkPalindrome = function(str) {
    console.log("Is this word a palindrome? " + str);
    console.log(isPalindrome(str));
};


checkPalindrome("a");
//Program.assertEqual(isPalindrome("a"), true);
checkPalindrome("matom");
//Program.assertEqual(isPalindrome("motor"), false);
checkPalindrome("rotor");
//Program.assertEqual(isPalindrome("rotor"), true);

For sure something is wrong with the recursive call. I would love to have your help. Thanks. I am attaching the output of my code.

enter image description here

like image 726
Niaz Ahsan Avatar asked Jul 28 '18 03:07

Niaz Ahsan


4 Answers

Here is another recursive palindrome.

function checkPalindrome(str){
    if(str.length === 1) return true;
    if(str.length === 2) return str[0] === str[1];
    if(str[0] === str.slice(-1)) return checkPalindrome(str.slice(1,-1))
    return false;
}

console.log(checkPalindrome('a')) // true
console.log(checkPalindrome('matom')) // false
console.log(checkPalindrome('rotor')) // true
like image 60
Avadhut Thorat Avatar answered Nov 15 '22 00:11

Avadhut Thorat


You defined isPalindrome() to return a value, so if you call it yourself, recursively or otherwise, you need to deal with that return value. Also, your if ... else logic is too complicated, simplify:

var isPalindrome = function(str) {
    if (str.length < 2) {
        return true;
    }

    if (firstCharacter(str) == lastCharacter(str)) {
        return isPalindrome(middleCharacters(str));
    }

    return false;
};
like image 38
cdlane Avatar answered Nov 15 '22 00:11

cdlane


    const isPalindrome = str => {
    const strLen = str.length;
    if (strLen < 2) return true;

    if (str[0] === str[strLen - 1]) {
        return isPalindrome( str.slice(1, strLen - 1) );
    }

    return false;
};

console.log(isPalindrome('madam'));
like image 24
Nitin . Avatar answered Nov 14 '22 23:11

Nitin .


Using slice creates an array - if you want to compare the first and last char, you will need to extract the value from the array before applying == -

var firstCharacter = function(str) {
    return str.slice(0, 1)[0] // <-- get the first element of the slice
}

var lastCharacter = function(str) {
    return str.slice(-1)[0] // <-- get the first element of the slice
}

Here's another recursive solution that uses parameters l (left) and r (right) to check the string using indexes (rather than creating intermediate values with slice) -

const palindrome = (s = "", l = 0, r = s.length - 1) =>
  r - l < 2
    ? true
    : s[l] === s[r] && palindrome (s, l + 1, r - 1)

console.log
  ( palindrome ("motor")   // false
  , palindrome ("rotor")   // true
  , palindrome ("racecar") // true
  , palindrome ("wow")     // true
  , palindrome ("i")       // true
  )

And here's a mutually recursive definition. It's wasteful but it has an elegant form nonetheless -

const pal = ([ s, ...more ]) =>
  more.length === 0 || pal2 (more.reverse(), s)

const pal2 = ([ s, ...more ], q) =>
  s === q && pal (more.reverse())

console.log
  ( pal ("motor")   // false
  , pal ("rotor")   // true
  , pal ("racecar") // true
  , pal ("wow")     // true
  , pal ("i")       // true
  )
like image 39
Mulan Avatar answered Nov 15 '22 00:11

Mulan