0

That's my example. String are given. Implement a function - detectPalindrom, that can detect palindrome string.

  • Given argument not an string - return 'Passed argument is not a string'.
  • Given string is empty - return 'String is empty'.
  • Given string palindrome - return 'This string is palindrome!'.
  • Given string is not a palindrome - return 'This string is not a palindrome!'

I wrote a solution, but it works incorrectly:

 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!'
   }
}
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
  • Examples: detectPalindrom(true) // "Passed argument is not a string" - detectPalindrom('') // "String is empty" - detectPalindrom("TARARAT") // 'This string is palindrome!' - detectPalindrom("I can fly ylf nac i") // 'This string is palindrome!' - detectPalindrom("testtest") // 'This string is not a palindrome!' – Misha Yanenko Apr 08 '21 at 14:35
  • 2
    Why do you say "it works incorrectly"? Can you make an example? I tried it out and it seems to work correctly – andref94 Apr 08 '21 at 14:39
  • Just write console.log(detectPalindrome(154)), and you see that JS says, TypeError – Misha Yanenko Apr 08 '21 at 14:41
  • 1
    Your code does not check if the argument is not a string, which is one of the things it should do according to what you write above. – Jesper Apr 08 '21 at 14:42
  • I don`t understand how to do it – Misha Yanenko Apr 08 '21 at 14:43
  • See [Check if variable is a string in JavaScript](https://stackoverflow.com/questions/4059147/check-if-a-variable-is-a-string-in-javascript) – Jesper Apr 08 '21 at 14:44

5 Answers5

1

Just can just put a check before creating the palindr string.

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");
Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28
0

You need to escape your inputs by keeping only non-white-space characters. You could refine this to strip-out special characters as well.

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
Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
0

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"))
Som Shekhar Mukherjee
  • 4,701
  • 1
  • 12
  • 28
andref94
  • 96
  • 6
0

This should accept with string and number both, Refactor and improvise as per your need. This is just basic to work with.

        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")
Sohan
  • 6,252
  • 5
  • 35
  • 56
-1

You can check if the input is string or not as well, what examples that your solution does not work for? or maybe you can just use something like:

String(str)

to convert your input to string

a0m0rajab
  • 115
  • 7