Palindrome checker returns true not false

Tell us what’s happening:
The test returns the true but not the false

   **Your code so far**
function palindrome(str) {
 
 str.toLowerCase();

 let strAlphaNum = str.match(/\w+/);

 let reverseStrAlphaNum = strAlphaNum.reverse(); 
 
 if (strAlphaNum == reverseStrAlphaNum) {
   return true;
 }

 else {
 return false;
}
 
} 
  








palindrome("eye");
   **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.53 Safari/537.36 Edg/103.0.1264.37

Challenge: Palindrome Checker

Link to the challenge:

What do you think this line does?

turns all alphabetical characters to lowercase

but strings are immutable, how would that work?

let str = “UPPER”
str.toLowerCase()
console.log(str)//UPPER

Little test for you

1 Like

i should split the string first?

also strings made of single characters are immutable, so, how do you change a string?

declare a new variable of the string?

try, let’s see if it works!

nope.

function palindrome(str) {
let newStr = str;
newStr.split(“”)
newStr.toLowerCase(“”);

let strAlphaNum = newStr.match(/\w+/);

let reverseStrAlphaNum = strAlphaNum.reverse();

if (strAlphaNum == reverseStrAlphaNum) {
return true;
}

else {
return false;
}

}

It haven’t changed anything tho

Split and tolowercase don’t have side effects, you can’t use them like that

As it was said you cannot mutate the string, so the expression newStr.split("") returns NEW string value and as you don’t save this new value anywhere, it’s just discarded. And the initial value of the string that is in the variable newStr remains untouched.

Split returns an array, to be precise

1 Like

Yes, but the new variable should be assigned the return value of calling the toLowerCase method on the original string (str).

okay. So the .split() method is not needed here?

No it is not. The split method would create an array from a string. I think what you are missing is the str.toLowerCase() returns a new string and does not affect the original str at all. You can take the value returned from calling str.toLowerCase and assign it to a new variable (call it what you want). Then you can work with it for the remainder of the function.

Thank you Randall. I do get the purpose of creating a new variable with the value of str.toLowerCase() although the false tests are still not validated.

Would need to see your latest code to help further.

str.toLowerCase();
Isn’t actually changing all letters to be lowercase because it is not being assigned to any variable, therefore it’s just executing where it changed all the letters to be lower case, but then it’s no assigning it to anything therefore it’s useless alone

//so I’ve changed the code slightly by adding variables for each step and now it only returns the false//

function palindrome(str) {
let newStr = str.toLowerCase(“”);

let newStrArr = newStr.split(“”);

let newStrArrReverse = newStrArr.reverse(“”);

let newStrArrReverseJoin = newStrArrReverse.join(“”);

let newStrArrReverseJoinMatch = newStrArrReverseJoin.match(/\w+\S+/);

if (newStr == newStrArrReverseJoinMatch) {
return true;
}

else {
return false;
}

}