Recursive palindrome check with JavaScript

喜你入骨 提交于 2020-08-27 21:33:25

问题


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.


回答1:


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;
};



回答2:


    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'));



回答3:


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



回答4:


Here is another way to recursively check for a palindrome in JS:

function isPalindrome(str){ if (str[0] === str[str.length - 1] && str.length > 1) { isPalindrome(str.substring(1, str.length -1)) return true }else{ return false } }




回答5:


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
  )


来源:https://stackoverflow.com/questions/51567811/recursive-palindrome-check-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!