# Sharing a code for your option... Palindrome challenge

I am trying to get your feedback on this topic. I have solved it of course, I just want to know if there’s a better a most effective way of solving it with less code…

``````    const evenOrOdd = (text) => text.length % 2 === 0 ? true : false;

function polindrome(text) {
const verifyFirstPalindrome = loopThrough(returnEvenText(removeSpaceInText(text)), splitIntoTwoText(removeSpaceInText(text)));
const verifySecondPalindrome = loopThrough(returnOddText(removeSpaceInText(text)), splitIntoTwoText(removeSpaceInText(text)));
if(verifyFirstPalindrome || verifySecondPalindrome) {
return text + " " + " is a palindrome";
} else {
return text + " " + " is not a palindrome"
}
}

function returnOddText(text) {
let half = text.length / 2;
return text.slice(0, half--);
}

function returnEvenText(text) {
let half = text.length / 2;
return text.slice(0, half);
}

const removeSpaceInText = (text) => {
let finalText = text.replace(/[\s . _  : ) ( ,]+/g, "");
finalText = finalText.replace(/-/g, "");
return finalText.toLowerCase().split("");
}

const splitIntoTwoText = (text) => {
let half = parseInt(Math.floor(text.length / 2));
let reminder = text.length % 2 + half;
if(evenOrOdd(text)) {
return text.slice(half, text.length).reverse();
} else {
return text.slice(reminder, text.length).reverse();
}
}

function loopThrough(textOne, textTwo) {
const x = textOne.length;
let count = 0;
for(let i = 0;  i < x; i++) {
if(textOne[i]=== textTwo[i]) {
count++;
}
}
if(count === x) {
return true;
} else {
return false;
}
}
}
``````
1 Like

There is a lot you could do, all your functions could use ES6. I dont see any callback functions but those could all be implicit `returns` eliminating the need for the keyword all together. any `if and else if statements` could use the switch approch that looks something more like a array with properties and values. Happy Coding!

For example, I don’t think even or odd is a distinction you need to make.

Also, why keep a count? The very first time a letter doesn’t match, you know you can’t have a palindrome.

``````function isPalindrome(text) {
const normalizedText = text.toLowerCase().replace(/[^a-z0-9]/g, '');
const reversed = normalizedText.split('').reverse().join('');
return normalizedText === reversed;
}

console.log(isPalindrome("Race car"));   // true
console.log(isPalindrome("Hello"));       // false
console.log(isPalindrome("A man, a plan, a canal: Panama")); // true

``````

Explanation:

1. Function Definition: The code defines a single function `isPalindrome` that takes the text as input.
2. Normalization:
• `text.toLowerCase()` : Converts the text to lowercase for case-insensitive comparison.
• `replace(/[^a-z0-9]/g, '')` : Removes non-alphanumeric characters using a regular expression. This ensures that punctuation, spaces, and other symbols don’t interfere with the palindrome check.
1. Reversal:
• `split('')` : Splits the normalized text into an array of characters.
• `reverse()` : Reverses the order of characters in the array.
• `join('')` : Joins the reversed array back into a string.
1. Comparison: The normalized text and its reversed version are compared using strict equality (`===` ).

Improvements:

• Conciseness: The code is more concise by combining multiple functions into a single function and utilizing built-in methods effectively.
• Readability: The variable names are descriptive, making the code easier to understand.
• Efficiency: The regular expression approach is efficient for handling a wide range of non-alphanumeric characters.
• Flexibility: This solution can be adapted to handle different delimiters or symbols by modifying the regular expression pattern within `replace` .