l2t

String is Palindrome

Asked: a month ago

Problem: Given a string, write a function to check if it is palindrome or not.

Rules:

  • A string is said to be palindrome if reverse of the string is same as string.
  • For example, “abba” is palindrome, but “abbc” is not palindrome.
function isPalindrome(str) {
    
    let lowIndex = 0;
    let highIndex = str.length - 1;

    while(highIndex > lowIndex) {
        if (str[lowIndex++] !== str[highIndex--]) {
            return false;
        }
    }

    return true;
}

console.log(isPalindrome("abba"));     // true
console.log(isPalindrome("sucks"));    // false
console.log(isPalindrome("abbccbba")); // true
Viewed: 87 times
Liked: 1 times