Home > database >  Palindrome string used by predefined function but unable to solve as it is showing only one error
Palindrome string used by predefined function but unable to solve as it is showing only one error

Time:04-08

// Unable to understand what is wrong in this code always showing string is not // palindrome

                 **Check this**    


        <script>

      var isPalindrome = function (str) {
        const st = str.split("").reverse().join();
        console.log(st);

        if (str == st) {
          console.log("It is a palindrome");
        } else {
          console.log("It is not a palindrome");
        }
      };
      let str = "abba";

      isPalindrome(str);

    </script>

               //I have tried a lot. unable to understand why stack overflow is taking to much time to upload only showing err //

CodePudding user response:

const isPalindrome = (str) => {
  const str2 = str.split("").reverse().join("");

  if (str == str2) {
    console.log("It is a palindrome");
  } else {
    console.log("It is not a palindrome");
  }
};

isPalindrome('aabb');
isPalindrome('abba');

CodePudding user response:

You can try this,

function palindrome(str) {
  let reversed = str.split('').reduce((acc, cur) => cur   acc, '');
  if (str === reversed) {
    return true;
  } else {
    return false;
  }
}
let str = "abba";
console.log(palindrome(str));
  • Related