我需要写困难的回文

I need to write difficult palindrome

这就是我的例子。字符串给出。实现一个函数 - detectPalindrom,可以检测回文字符串。

我写了一个解决方案,但它不正确:

 const detectPalindrome = (str) => {
    const palindr = str.split('').reverse().join('')

    if(str === '') {
      return 'String is empty'
  } 
    if (str === palindr) {
       return 'This string is palindrome!'
   } 
    if (str !== palindr) {
       return 'This string is not a palindrome!'
   }
}

只需在创建 palindr 字符串之前进行检查即可。

const detectPalindrome = (str) => {
  if (typeof str !== "string") {
    return 'Passed argument is not a string'
  }
  
  const palindr = str.split('').reverse().join('');

  if (str === '') {
    return 'String is empty';
  }
  if (str === palindr) {
    return 'This string is palindrome!';
  }
  if (str !== palindr) {
    return 'This string is not a palindrome!';
  }
};

detectPalindrome("154");

您也可以检查输入是否为字符串,您的解决方案不适用于哪些示例?或者也许你可以使用类似的东西:

String(str)

将您的输入转换为字符串

您需要通过仅保留非白色 space 字符来转义您的输入。您也可以对其进行优化以去除特殊字符。

const
  reverse = str => str.split('').reverse().join(''),
  escape = str => str.toLowerCase().replace(/[^\S]|[,;\.?!'"]/g, '');

const detectPalindrome = input => {
  if (typeof input === 'string') {
    if (input === '') return 'String is empty'
    const
      str = escape(input),
      pal = reverse(str);
    return str === pal
      ? 'This string is a palindrome!'
      : 'This string is not a palindrome!';
  } else {
    return 'Passed argument is not a string';
  }
}

console.log(detectPalindrome(0));         // 'Passed argument is not a string'
console.log(detectPalindrome(''));        // 'String is empty'
console.log(detectPalindrome('Racecar')); // 'This string is a palindrome!'
console.log(detectPalindrome('Apple'));   // 'This string is not a palindrome!'

console.log(detectPalindrome("Madam I'm Adam")); // TRUE

const detectPalindrome = (str) => {

  if (typeof str !== 'string')
    return 'Passed argument is not a string'

  const palindr = str.split('').reverse().join('')

  if (str === '') {
    return 'String is empty'
  }
  if (str === palindr) {
    return 'This string is palindrome!'
  }
  if (str !== palindr) {
    return 'This string is not a palindrome!'
  }
}

console.log(detectPalindrome(123))
console.log(detectPalindrome(""))
console.log(detectPalindrome("abcd"))
console.log(detectPalindrome("lol"))

这应该接受字符串和数字,根据您的需要重构和即兴创作。这只是基本的工作方式。

        const detectPalindrome = input => {
        if (typeof input === 'string') {
            if(input === '') {
                return 'String is empty'
            }
            if (typeof input === 'string') {
                const palindr = input.split('').reverse().join('');
                if(input === palindr)
                {
                    console.log('This string is palindrome!');
                }
            }
    
        }
        else if(typeof input === 'number')
        {
            checkPalinForNumber(input)
        }
        else{
            console.log("no match");
        }
    
    }
    
function checkPalinForNumber(number)
{
    var temp = number;
    while(number>0)
    {
        var rem = number%10;
        number = parseInt(number/10);
        var final = final*10+rem;
    }
    if(final==temp)
    {
        console.log("number is Palindrome");
    }
    else
    {
        console.log("number is not palindrome");
    }

}
        detectPalindrome(123);
        detectPalindrome("aba")