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, "");
//console.log(str)
let arr = str.split('')
console.log("this is " + arr)
let rev_str = arr.map(x => arr.pop())
console.log("This is reverse string:" + rev_str)
return true;
}
palindrome("eye");
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.