Palindrome project question

Hi all

I am doing the palindrome project of the JS cert, and was just wondering what I was doing wrong.

What I wrote thus far:

function palindrome(str) {
  str = str.replaceAll("[^a-zA-Z0-9]", "").replace(/\s/g, "").replace(/\|/g, "");
  let arr = str.split('')
  console.log("this is " + arr)
  let rev_str = => arr.pop())
  console.log("This is reverse string:" + rev_str)
  return true;


This is an example the output I get:
this is n,o,t,a,p,a,l,i,n,d,r,o,m,e
This is reverse string:e,m,o,r,d,n,i,

So as you can see, the problem is @ the map line. What I don’t get is my intended logic seems to be working for building the first few elements of the reverse sentence but
why does it just suddenly stops randomly?

Is there a way to get the index of an element beside IndexOf(x) in this context? The latter will unfortunately return the index of the first element “x” in this context, so for the words “eye” it wouldn’t really work.

PS: I know I could use either loops or reverse(), but I am just trying to go the extra mile here.

You are mutating the array as you iterate over it. That’s never a good idea.

If you want to use an array method to reverse the array, I’d consider a reduce without side effects.

1 Like

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.