# Help Me Make My Code More Efficient (Palindrome Checker)

**URL:** <https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816>\
**Category:** General\
**Created:** [February 13, 2020, 4:40am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816 "2020-02-13T04:40:04Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [February 13, 2020, 4:40am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/1 "2020-02-13T04:40:05Z")

</div>

I just completed the first problem in the final section of the JavaScript course, and while I’m happy that I’ve become able to write things like this, I can’t help but feel that my current code isn’t as efficient as it could be. I mostly just want to see if anyone has any recommendations on how to make the following code smaller and more efficient, as a learning opportunity for me.

Thank you in advance!

```auto
function palindrome(str) {
let arr1 = str.toLowerCase().replace(/[\W_]/g, "").split("");
let arr2 = arr1.splice(Math.floor(arr1.length / 2)).reverse();
if (arr1.length !== arr2.length) {
  arr2.pop();
}
console.log(arr1, arr2);
return arr1.every(target => target === arr2[arr1.indexOf(target)]);
}

palindrome("_eye")
palindrome("0_0 (: /-\ :) 0-0");

```

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [February 13, 2020, 5:05am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/2 "2020-02-13T05:05:06Z")

</div>

If you want to rewrite this to be more concise I would suggest that you don’t think of the strings as an array of characters that need to be compared by index but rather as just two strings that can be compared using the === operator.

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [February 13, 2020, 5:09am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/3 "2020-02-13T05:09:04Z")

</div>

If you look for efficiency you should avoid creating additional arrays.  
My take would be to just take the input, clean it up and the compare character by character:

```auto
function palindrome(str) {
  const _clean = str.toLowerCase().replace(/[\W_]/g, '')
  let start = 0;
  let end = _clean.length - 1;

  while (start < end) {
    if (_clean[start++] !== _clean[end--]) {
      return false
    }
  }
  return true;
}

```

---

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [February 13, 2020, 5:09am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/4 "2020-02-13T05:09:14Z")

</div>

That’s a very solid point, thank you very much!

I assume `reverse()` works on strings as well as arrays?  
If not, is there a different way to handle it?

---

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [February 13, 2020, 5:12am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/5 "2020-02-13T05:12:34Z")

</div>

> [@jenovs](#):
>
> `function palindrome(str) { const _clean = str.toLowerCase().replace(/[\W_]/g, '') let start = 0; let end = _clean.length - 1; while (start < end) { if (_clean[start++] !== _clean[end--]) { return false } } return true; }`

Thank you very much! One of the problems I ran into while writing the code I made is that it couldn’t handle the unique character in the center of the palindrome caused by an odd number of characters. The original code I wrote was something like this, but with the aforementioned excess of arrays. I take it this version isn’t confused by that extra character?

---

<div class="post-metadata">

**Author:** ![bbsmooth](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/bbsmooth/32/203556_2.png) [@bbsmooth](https://forum.freecodecamp.org/u/bbsmooth)\
**Post date:** [February 13, 2020, 5:13am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/6 "2020-02-13T05:13:04Z")

</div>

Actually, reverse() only works on Arrays, so you are doing that part correctly. After you reverse the array you can easily put it back together as a string with join().

---

<div class="post-metadata">

**Author:** ![Marmiz](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/marmiz/32/74845_2.png) [@Marmiz](https://forum.freecodecamp.org/u/Marmiz)\
**Post date:** [February 13, 2020, 5:13am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/7 "2020-02-13T05:13:20Z")

</div>

As @jenovs suggested, you don’t really need any additional step to check if a **cleaned** string is palindrome, all you can do is just loop till half the string, and compare head and tail.

As soon as one of those letter differs, means that’s not a palindrome.

It has an execution time of O(n/2) (half the length of the string) 🙂

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [February 13, 2020, 5:16am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/8 "2020-02-13T05:16:01Z")

</div>

> [@AndrewGarrison](#):
>
> One of the problems I ran into while writing the code I made is that it couldn’t handle the unique character in the center of the palindrome caused by an odd number of characters.

If there is an odd character count `start` and `end` will be equal, that will break the while loop.

---

<div class="post-metadata">

**Author:** ![AndrewGarrison](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/andrewgarrison/32/167947_2.png) [@AndrewGarrison](https://forum.freecodecamp.org/u/AndrewGarrison)\
**Post date:** [February 13, 2020, 5:21am UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/9 "2020-02-13T05:21:08Z")

</div>

Thank you all very much for the help!

I think I have a better idea of how to cut out unnecessary steps from my code.

I’ll keep this in mind as I continue to complete the problems in this test section.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 7:35pm UTC](https://forum.freecodecamp.org/t/help-me-make-my-code-more-efficient-palindrome-checker/349816/10 "2021-06-01T19:35:39Z")

</div>


