# Post your Palindrome Checker solutions here!

**URL:** <https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750>\
**Category:** JavaScript Bootcamp\
**Created:** [September 20, 2022, 8:35am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750 "2022-09-20T08:35:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![hola\_soy\_milk](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/hola_soy_milk/32/600_2.png) [@hola\_soy\_milk](https://group.classcentral.com/u/hola_soy_milk)\
**Post date:** [September 20, 2022, 8:35am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/1 "2022-09-20T08:35:17Z")

</div>

Heya folks!

Congrats on solving the palindrome checker project!! 🎉

Feel free to drop your solutions here.

Here’s mine:

```auto
function cleanUpNonAlpha(str) {
  return str.replace(/[\W_]/g, "");
}

function makeUppercase(str) {
  return str.toUpperCase();
}

function flipStr(str) {
  // Take an empty string
  let result = "";

  // Start a loop at the end of the input string
  // Put the letter at the current stage at the end of the result string
  for(let i=str.length-1; i>=0; i--) {
    result += str[i];
  }
  
  // return result string
  return result;
}

function palindrome(str) {
  // Clean up string by removing non-alphanumeric characters
  const cleanedUpStr = cleanUpNonAlpha(str);

  // Make cleaned up string uppercase
  const uppercaseStr = makeUppercase(cleanedUpStr);

  // Reverse uppercase cleaned up string
  const reversedStr = flipStr(uppercaseStr);

  // Compare reversed and uppercase string
  // If they're the same, it's a palindrome (return true)
  console.log(`Is ${uppercaseStr} the same as ${reversedStr}?`)
  return reversedStr === uppercaseStr;
}

console.log(palindrome("Tacocat!"));

```

---

<div class="post-metadata">

**Author:** ![yashbhatt2913622](https://avatars.discourse-cdn.com/v4/letter/y/73ab20/32.png) [@yashbhatt2913622](https://group.classcentral.com/u/yashbhatt2913622)\
**Post date:** [September 20, 2022, 8:40am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/2 "2022-09-20T08:40:21Z")

</div>

Here’s mine:

> ​ **function** palindrome(str) {  
> //remove alpha numeric characters  
> **const cleanStr** = str.replace(/[\W\_]/g , “”);  
> console.log(cleanStr);  
> //make the string uppercase  
> **const newStr** = cleanStr.toUpperCase();  
> // check  
> **for** (let i = 0; i \< newStr.length;i++ ){  
> **if** (newStr[i] === newStr[(newStr.length - 1)-i]){  
> **continue** ;  
> } **else** {  
> **return false** ;  
> };  
> }  
> \*\* return true\*\*;  
> }  
> console.log(palindrome(“\_eye”));

---

<div class="post-metadata">

**Author:** ![carlost2672543](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/carlost2672543/32/4162_2.png) [@carlost2672543](https://group.classcentral.com/u/carlost2672543)\
**Post date:** [September 20, 2022, 9:08am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/3 "2022-09-20T09:08:58Z")

</div>

Hi guys and gals!!

This is my solution. I have commented out trying to explain it. I hope my english didn’t get too tricky. 🤪

Even though my solution seems more fancy, for this challenge an old fashion loop is probably a better aproach. The code is longer but **more readeable** , readability is really important.

Here it goes:

```auto
function palindrome(str) {

  // Clean up the string: 
  // 1 | replace all non alphanumerics characters and underscores from the string using regex (/[\W_]/g)
  // and replace method, wich sustitutes all with an empty string
  // --> fivefour is the result
  // 2 | convert all characthers to lower case  
  const cleanedUpStr = str.replace(/[\W_]/g, "").toLowerCase();

  // Reverse the order of the string letters
  // 1 | Convert the cleaned string into an array using the spread operator ([...string])
  // --> ['f', 'i', 'v', 'e', 'f', 'o', 'u', 'r'] is the result
  // 2 | Reverse the resulting array items using Array.prototype.reverse() method
  // --> ['r', 'u', 'o', 'f', 'e', 'v', 'i', 'f'] is the reversed array
  // 3 | Join all the items in a single string using Array.prototype.join("") method, no spaces ("", empty string is used)
  // --> ruofevif is the resulting reversedStr variable
  const reversedStr = [...cleanedUpStr].reverse().join("");

  // Console log the obtained variables and too actually see it
  console.log({cleanedUpStr}, {reversedStr});

  // Return a boolean comparing both variables
  return cleanedUpStr === reversedStr;
}

palindrome("five|\_/|four");

```

You can use this tool [JavaScript coding tutor - Learn JavaScript by visualizing code execution](https://pythontutor.com/javascript.html#mode=edit) to visualize the result of any code step by step. It is a brilliant recomendation from @syllie; read her posts, they are really well documented and well explained ones.

Keep calm, keep going and dont give up. You ALL can do it!!! ✊  
Happy coding!

---

<div class="post-metadata">

**Author:** ![sandrahirsch2923199](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@sandrahirsch2923199](https://group.classcentral.com/u/sandrahirsch2923199)\
**Post date:** [September 20, 2022, 9:34am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/4 "2022-09-20T09:34:25Z")

</div>

That’s my solution - maybe a bit complicated as I haven’t done yesterday’s homework before writing it. I had a bit of a hard time figuring out how to test the arrays for equality (the toString() method), as I forgot that we cannot check the arrays directly for equality.

```auto
function palindrome(str) {
  //regex to get rid of symbols and make everything uppercase:
  let myRegex = /[a-z0-9]/gi;
  let strUpper = str.toUpperCase();
  strUpper = strUpper.match(myRegex);

  //find length of the clean string:
  let laenge = strUpper.length;

  //reverse the string
  let strBackwards = [];

  for (let i = 0; i < laenge; i++) {
    strBackwards.unshift(strUpper[i]);
  }

  
  //as arrays cannot be directly tested for equality, I changed them to strings
  if (strUpper.toString() == strBackwards.toString()) {
    return true;
  } else {
    return false;
  }
}

console.log(palindrome("1 eye for of 1 eye."))

```

---

<div class="post-metadata">

**Author:** ![carlost2672543](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/carlost2672543/32/4162_2.png) [@carlost2672543](https://group.classcentral.com/u/carlost2672543)\
**Post date:** [September 20, 2022, 9:51am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/5 "2022-09-20T09:51:22Z")

</div>

Hi @sandrahirsch2923199

I would recommend you use always the strict equality === operator to avoid issues and keep your variables under your control not under JavaScript.

Your solution looks great!  
The code is clean and It is also easier to read and understand than mine, so be proud!!

Happy coding! You are doing great!

---

<div class="post-metadata">

**Author:** ![sandrahirsch2923199](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@sandrahirsch2923199](https://group.classcentral.com/u/sandrahirsch2923199)\
**Post date:** [September 20, 2022, 10:22am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/6 "2022-09-20T10:22:28Z")

</div>

thanks for the feedback - much appreciated ☺

---

<div class="post-metadata">

**Author:** ![sandrahirsch2923199](https://avatars.discourse-cdn.com/v4/letter/s/ecccb3/32.png) [@sandrahirsch2923199](https://group.classcentral.com/u/sandrahirsch2923199)\
**Post date:** [September 20, 2022, 10:27am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/7 "2022-09-20T10:27:00Z")

</div>

I really like your approach for the loop - that was what I was looking for, but my brain didn’t figure it out 😉

---

<div class="post-metadata">

**Author:** ![Timiphil](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/timiphil/32/5367_2.png) [@Timiphil](https://group.classcentral.com/u/Timiphil)\
**Post date:** [September 20, 2022, 10:30am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/8 "2022-09-20T10:30:17Z")

</div>

I did three solutions, but here is the shortest one:

 ![Screenshot 2022-09-20 at 12.28.19](https://us1.discourse-cdn.com/flex016/uploads/ccstudygroup/original/2X/2/20491cd8864dc9427d73dfcbbaf805011e5a22c2.png)

---

<div class="post-metadata">

**Author:** ![Timiphil](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/timiphil/32/5367_2.png) [@Timiphil](https://group.classcentral.com/u/Timiphil)\
**Post date:** [September 20, 2022, 10:39am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/9 "2022-09-20T10:39:29Z")

</div>

I did three solutions and this is the shortest one:

```auto
function palindrome(str) {
  let newStr = str.toLowerCase().replace(/[^a-z\d]/g, "");
   return newStr.split("").reverse().join("") === newStr;
 }
console.log(palindrome("A man, a plan, a canal. Panama"));

```

---

<div class="post-metadata">

**Author:** ![Tzerio](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/tzerio/32/6142_2.png) [@Tzerio](https://group.classcentral.com/u/Tzerio)\
**Post date:** [September 20, 2022, 10:39am UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/10 "2022-09-20T10:39:46Z")

</div>

You still got too much lines, fixed that for you, no need to thank me 🤣

```auto
const palindrome = str => str.toLowerCase().replace(/[^a-z\d]/g, '') === str.toLowerCase().replace(/[^a-z\d]/g, '').split('').reverse().join("");

```

I tend to not introduce new variables when I can reuse them…have to be careful with arrays 😅  
My solution:

```auto
function palindrome(str) {
  str = str.replace(/\W|_/g, ""); //remove non-alphanumeric characters
  str = str.toUpperCase(); // all characters to uppercase
  for (let i = 0; i <= str.length / 2; i++) {
   //compare first letter to last letter etc
   if (str[i] !== str[str.length - 1 - i]) {  
     return false;
   }
  }
  return true;
}

```

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 20, 2022, 12:45pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/11 "2022-09-20T12:45:14Z")

</div>

Mine was like yours except for using the spread operator, I went for the more traditional `const reversed = result.split('').reverse().join('');` 🙂

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 20, 2022, 12:48pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/12 "2022-09-20T12:48:23Z")

</div>

lmao 🤣 love how you fixed @Timiphil 's code so it becomes a one liner. I pity the developer that someday has to fill your shoes. love your humour!

---

<div class="post-metadata">

**Author:** ![Timiphil](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/timiphil/32/5367_2.png) [@Timiphil](https://group.classcentral.com/u/Timiphil)\
**Post date:** [September 20, 2022, 1:16pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/13 "2022-09-20T13:16:50Z")

</div>

Damn! How did you do that? That’s cool. I want to learn that.

---

<div class="post-metadata">

**Author:** ![williambradle2950093](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/williambradle2950093/32/6027_2.png) [@williambradle2950093](https://group.classcentral.com/u/williambradle2950093)\
**Post date:** [September 20, 2022, 1:47pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/14 "2022-09-20T13:47:25Z")

</div>

I wanted to do my solution before watching the video so I guess this is not quite as elegant as it could be. I am sure I don’t need \s+ in my variable nospace and I am fairly sure I don’t need to bring the const newPhrase in at all!!

But, what the heck, it works and I think that is what counts - love some of the shortcuts some of you folk have used and I hope we get to learn them soon. But compared to my (also working) solution for Roman Numerals, this is positively glowing with elegance!!!

Cheers folks

Bill

```auto
function palindrome(str) {
 let nospace = /\s+|\W+|_/gi; 
 let replace = (str.replace(nospace,"")); 
 const newPhrase = replace.toLowerCase();
  for(let i = 0; i < newPhrase.length; i++){  
    if(newPhrase[i] === newPhrase.charAt(newPhrase.length-i-1)){     
    }else{
     return false;
    }
  }
  return true;
}
palindrome("almostomla")

```

---

<div class="post-metadata">

**Author:** ![michaelleonid2923599](https://avatars.discourse-cdn.com/v4/letter/m/9fc29f/32.png) [@michaelleonid2923599](https://group.classcentral.com/u/michaelleonid2923599)\
**Post date:** [September 20, 2022, 5:32pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/15 "2022-09-20T17:32:38Z")

</div>

Here’s my solution below.

Thanks,  
Michael

// removes all non-alphanumeric characters  
function cleanUpNonAlpha(str) {  
let fixRegex = /\W|\_/g; // Change this line  
let replaceText = ‘’; // Change this line  
return str.replace(fixRegex, replaceText);  
}

// reverses string  
function reverse(str) {  
let result = ‘’;  
for (let i = str.length - 1; i \>= 0; i–) {  
result += str[i];  
}  
return result;  
}

function palindrome(str) {  
let strippedStr = cleanUpNonAlpha(str);

// make string lowercase  
strippedStr = strippedStr.toLowerCase();

// return  
const reversed = reverse(strippedStr);

return strippedStr === reversed;  
// return reversed;  
}

palindrome(‘eye’);

---

<div class="post-metadata">

**Author:** ![Tzerio](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/tzerio/32/6142_2.png) [@Tzerio](https://group.classcentral.com/u/Tzerio)\
**Post date:** [September 20, 2022, 6:12pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/16 "2022-09-20T18:12:28Z")

</div>

> [@Timiphil](#):
>
> Damn! How did you do that? That’s cool. I want to learn that.

That’s an arrow function with parameters, it was introduced at the beginning von ES6, maybe you forgot it already, and then substituting newStr in the return statement with your definition of newStr. 😇  
It’s kinda the same function but less readable.

When you wrote that was your shortest solution, my fingers just started itching to help you changing it into one line 🤣

---

<div class="post-metadata">

**Author:** ![Jane2963637](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/jane2963637/32/6117_2.png) [@Jane2963637](https://group.classcentral.com/u/Jane2963637)\
**Post date:** [September 20, 2022, 7:26pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/17 "2022-09-20T19:26:39Z")

</div>

Here’s mine- I was following along with Ramón, so the start is the same, then I paused the lesson and broke away and finished the checker with help from MDN and previous lesson notes.

> function cleanUpNonAlpha(str) {  
> return str.replace(/[\W\_]/g, “”);  
> }
> 
> function makeUppercase(str) {  
> return str.toUpperCase();  
> }
> 
> function flipStr(str) {  
> return str.split(“”).reverse().join(“”);  
> }
> 
> function palindrome(str) {
> 
> const cleanedUpStr = cleanUpNonAlpha(str);
> 
> console.log(cleanedUpStr);
> 
> const uppercaseStr = makeUppercase(cleanedUpStr);
> 
> console.log(uppercaseStr)
> 
> const reversedStr = flipStr(uppercaseStr);
> 
> console.log(reversedStr);
> 
> return uppercaseStr === reversedStr?  
> true: false;
> 
> // If they are the same, it’s a palindrome (return true)
> 
> }  
> console.log(palindrome(“otto”));  
> palindrome(“Otto”);

---

<div class="post-metadata">

**Author:** ![donnar2671543](https://avatars.discourse-cdn.com/v4/letter/d/ecb155/32.png) [@donnar2671543](https://group.classcentral.com/u/donnar2671543)\
**Post date:** [September 20, 2022, 8:33pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/18 "2022-09-20T20:33:49Z")

</div>

So I did 2 versions. First up a recursive function just because I wanted to practice this.

```auto
/*
So, I wanted to have a play writing a recursive function to do this just for the practice.
Even though there is a much simpler way to do it (reversing the string and checking equality).
Recursion logic:
- start with full string e.g. tacocat
- compare first and last characters (= t and t)
- they are a match so a recursive call to same function with substring
  excluding these characters - so this case string will be acoca
- and so on until either first and last chars don't match - not palindrome or reach base case
- base case is length of zero or one characters - if this is reached, the string is a palindrome.
 */

function recursiveIsPalindrome(str) {
    // if the string has been reduced down to one or no chars, all previous pairs of
    // first and last chars match so the string is a palindrome
    if (str.length <= 1 ) {
        return true;
    }
    else if (str.charAt(0) === str.charAt(str.length - 1)) {
        // if first and last char in string are a match, recursive call of this function with a substring
        // excluding these characters.
        return recursiveIsPalindrome(str.substring(1, (str.length - 1)));
    }
    else {
        // if characters don't match, definitely not a palindrome, no further checking needed.
        return false;
    }
}

function palindrome(str) {
    // convert string to lower case and strip out all non alphanumeric chars
    let testString = str.toLowerCase().replace(/[^a-z0-9]/g, "");
    console.log(`Original string = ${str}, stripped string = ${testString}`);
    // check if converted string is a palindrome
    return recursiveIsPalindrome(testString);
}

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

```

Second when I started watching the class video and realised just reversing the string was much simpler.

```auto
function palindrome(str) {

    console.log("Original string: " + str);
    // convert string to a single case (all lower)
    const lowerCaseStr = str.toLowerCase();

    // Clean up string by removing non-alphabetic characters
    const cleanedUpString = lowerCaseStr.replace(/[^a-z0-9]/g, "");
    console.log("Cleaned up string: " + cleanedUpString);

    // reverse string
    // Thank you https://www.freecodecamp.org/news/how-to-reverse-a-string-in-javascript-in-3-different-ways-75e4763c68cb/
    // then later "Lukas" during the Class Central video session
    // for this info on how to reverse a string.
    const reversedString = cleanedUpString.split("").reverse().join("");
    console.log("Reversed string: " + reversedString);

    // compare original cleaned up string and reversed string
    // if they match, string is a palindrome
    return cleanedUpString === reversedString;
}

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

```

hope these help someone.

---

<div class="post-metadata">

**Author:** ![damian2665429](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/damian2665429/32/2314_2.png) [@damian2665429](https://group.classcentral.com/u/damian2665429)\
**Post date:** [September 20, 2022, 9:17pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/19 "2022-09-20T21:17:08Z")

</div>

Here goes my solution, I had trouble with the regex expression as I was not familiar with the .replace() method

```auto
function palindrome(str) {
  const justAlpha = str.replace(/\W|_/g, "").toLowerCase()
  const reversedStr = justAlpha.split("").reverse().join("")
  return (justAlpha === reversedStr ? true : false);
}

```

---

<div class="post-metadata">

**Author:** ![syllie](https://sea2.discourse-cdn.com/flex016/user_avatar/group.classcentral.com/syllie/32/6257_2.png) [@syllie](https://group.classcentral.com/u/syllie)\
**Post date:** [September 20, 2022, 9:44pm UTC](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750/20 "2022-09-20T21:44:41Z")

</div>

Way to go Jane!

Only thing I want to point out is that your ternary operator in the return is a bit overboard. Remember that the conditon `uppercaseStr === reversedStr` already evaluates to either true or false, so no need to make it explicitly. You could do just

```auto
return uppercaseStr === reversedStr;

```

But you have this! Well done!

@damian2665429 - this one is for you too! 🙂 well done!

[Next page](https://group.classcentral.com/t/post-your-palindrome-checker-solutions-here/35750.md?page=2)
