Computer >> Computer tutorials >  >> Programming >> Javascript

Function to find out palindrome strings JavaScript


We are required to write a JavaScript function that takes in a string. The function should return true if the string is a palindrome string, false otherwise.

Palindrome strings are those strings that reads the same from back and front.

For example − 'madam', 'dad', 'abcdcba'

Our only condition is that we cannot use any inbuilt string method or convert the string to array.

Example

const str = 'madam';
const isPalindrome = (str = '') => {
   const { length } = str;
   let start = 0, end = length - 1; while(start < end){
      const leftChar = str[start];
      const rightChar = str[end];
      if(leftChar !== rightChar){
         return false;
      };
      start++;
      end--;
   };
   return true;
};
console.log(isPalindrome(str));
console.log(isPalindrome('avsssvsa'));

Output

And the output in the console will be −

true
false