I just passed my Palindrome test! Yay!

So I am like, how to do this? and I did it!

function cleanString(charsString) {
	const regex = /[\s!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~]/gi;
	return charsString.toLowerCase().replace(regex, '');
}
function reverseString(charsString) {
	let reversedString = "";
	for (let i = charsString.length - 1; i >= 0; i--) {
	reversedString += charsString[i];
	}
return reversedString;
}

function palindrome(str) {
	let cleanReversed = reverseString(cleanString(str));
	let cleanStr = cleanString(str);
	return (cleanReversed === cleanStr);
}

console.log(palindrome("eye"));
console.log(palindrome("_eye"));
console.log(palindrome("race car"));
console.log(palindrome("not a palindrome"));
console.log(palindrome("A man, a plan, a canal. Panama"));
console.log(palindrome("never odd or even"));
console.log(palindrome("nope"));
console.log(palindrome("almostomla"));
console.log(palindrome("My age is 0, 0 si ega ym."));
console.log(palindrome("1 eye for of 1 eye."));
console.log(palindrome("0_0 (: /-\ :) 0-0") );
console.log(palindrome("five|\_/|four"));

Any, any feedback will be greatly appreciated.

Thanks!

-D-

One thing I might improve is the regex. You can make it much simpler. Instead of concentrating on all the characters you don’t want to include, instead concentrate on the characters you want to keep and then get rid of the rest.

1 Like

Hi @bbsmooth , thanks for your feedback, much appreciated. I worked on what you advised me to do and here is the end result:

function cleanString(charsString) {
	return charsString.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
}
function reverseString(charsString) {
	let reversedString = "";
	for (let i = charsString.length - 1; i >= 0; i--) {
	reversedString += charsString[i];
	}
return reversedString;
}

function palindrome(str) {
	let cleanReversed = reverseString(cleanString(str));
	let cleanStr = cleanString(str);
	return (cleanReversed === cleanStr);
}

console.log(palindrome("eye"));
console.log(palindrome("_eye"));
console.log(palindrome("race car"));
console.log(palindrome("not a palindrome"));
console.log(palindrome("A man, a plan, a canal. Panama"));
console.log(palindrome("never odd or even"));
console.log(palindrome("nope"));
console.log(palindrome("almostomla"));
console.log(palindrome("My age is 0, 0 si ega ym."));
console.log(palindrome("1 eye for of 1 eye."));
console.log(palindrome("0_0 (: /-\ :) 0-0") );
console.log(palindrome("five|\_/|four"));

I hope this version is cleaner.

Oh ya, I think that regex is much simpler and easier to read, don’t you?

Now there is nothing wrong with breaking this into several functions like you did, especially if you think you might reuse those functions. But I would challenge you to use functional programming to condense this into just a few lines of code. In other words, get rid of the two helper functions completely and do everything they are doing in the palindrome function. The cleanString function is easy to get rid of because it’s just a one liner anyway. To get rid of the reverseString function I would recommend you look into the Array methods that JS offers. With the right combination of methods you can reverse that string without using a for loop.

Two lines in palindrome, that’s all you get :slight_smile:

1 Like

Many thanks, I will continue messing with this. I haven’t taken the functional programming course but perhaps I can cook something. Let’s:

function palindrome(str) {
	let cleanReversed = [...str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()].reverse().join("");
	let cleanStr = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
	return (cleanReversed === cleanStr);
}

console.log(palindrome("eye"));
console.log(palindrome("_eye"));
console.log(palindrome("race car"));
console.log(palindrome("not a palindrome"));
console.log(palindrome("A man, a plan, a canal. Panama"));
console.log(palindrome("never odd or even"));
console.log(palindrome("nope"));
console.log(palindrome("almostomla"));
console.log(palindrome("My age is 0, 0 si ega ym."));
console.log(palindrome("1 eye for of 1 eye."));
console.log(palindrome("0_0 (: /-\ :) 0-0") );
console.log(palindrome("five|\_/|four"));

That’s pretty good, but I said you only get two lines :slight_smile: Do you need to call some of those methods twice?

1 Like

There you go boss:

function palindrome(str) {
	return [...str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase()].reverse().join("")=== str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
}

console.log(palindrome("eye"));
console.log(palindrome("_eye"));
console.log(palindrome("race car"));
console.log(palindrome("not a palindrome"));
console.log(palindrome("A man, a plan, a canal. Panama"));
console.log(palindrome("never odd or even"));
console.log(palindrome("nope"));
console.log(palindrome("almostomla"));
console.log(palindrome("My age is 0, 0 si ega ym."));
console.log(palindrome("1 eye for of 1 eye."));
console.log(palindrome("0_0 (: /-\ :) 0-0") );
console.log(palindrome("five|\_/|four"));

Nice. Yes, you can definitely do this in one line but now you are calling replace and toLowerCase on str twice. Personally, I prefer to create a temporary variable that will hold the “clean” string, which will mean you only need to call replace and toLowerCase once. This will make the code IMO a little simpler and cleaner. The way it is now, I have to check the regexes on both sides of the equals sign to make sure they are doing the same thing. With a temp variable I wouldn’t have to do that. But that’s just my opinion. Also, this is a pretty simple regex, so it is easy to read and compare them. For a more complex regex I would definitely recommend the temp variable. Also, maintenance wise, if you needed to adjust the regex, you would have to do it in two places instead of just one with the temp variable, which increases your chance of mistake.

Good job!

1 Like

That’s cool, let’s see if I do it better now:

function palindrome(str) {
	let cleanString = str.replace(/[^a-zA-Z0-9]/g, '').toLowerCase();
	let reversedCleanString = [...cleanString].reverse().join("");
	return cleanString === reversedCleanString;
}