Skip to content
the poor coder

palindrome txt

Poor Man
Published
Reading time
2 min read
JavaScript

//One way to create a palindrome text program is to use a recursive function that takes a string as its argument. The function will check if the string is a palindrome or not. If it is, it will return true, if not, it will return false.

//Recursive function to check if a string is a palindrome or not
function isPalindrome (txt) {
    //base case
    if (txt.length <= 1) return true;

    //check first and last character
    if (txt.charAt(0) !== txt.charAt(txt.length -1)) return false;

    //recursive call
    return isPalindrome( txt.substring(1, txt.length -1) );
}

//Test strings
console.log(isPalindrome("level"));
console.log(isPalindrome("abba"));
console.log(isPalindrome("abcd"));
    

This program uses a recursive function to check whether a given string is a palindrome or not. The function takes a string as its argument and checks if the string is a palindrome or not. If the string is a palindrome, the function returns true, and if not, the function returns false. The program checks the first and last characters of the string and, if they are the same, it makes a recursive call with a substring that excludes the first and last characters. The process is repeated until there is only one character left in the string. If all the characters from the beginning and end of the string are the same, the string is a palindrome. The program is tested with three strings, two of which are palindromes, and one which is not. The output is as expected, with "level" and "abba" returning true, and "abcd" returning false.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC