Hire QA – Specialized in QA Recruitment, Technical Interviews and Testing Solutions

Write a function to find out longest palindrome in a given string?

Category: Java
// program to check if the string is palindrome or not

function checkPalindrome(string) {

    // find the length of a string
    const len = string.length;

    // loop through half of the string
    for (let i = 0; i < len / 2; i++) {

        // check if first and last string are same
        if (string[i] !== string[len - 1 - i]) {
            return 'It is not a palindrome';
        }
    }
    return 'It is a palindrome';
}

// take input
const string = prompt('Enter a string: ');

// call the function
const value = checkPalindrome(string);

console.log(value);
anana
radar

Output

Leave a Reply

Your email address will not be published. Required fields are marked *