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:
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;
}
}
ilenia
July 4, 2022, 5:54pm
11
It haven’t changed anything tho
Split and tolowercase don’t have side effects, you can’t use them like that
udaaff
July 4, 2022, 6:02pm
12
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.
ilenia
July 4, 2022, 6:04pm
13
Split returns an array, to be precise
1 Like
okay. So the .split() method is not needed here?
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.
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;
}
}
Right before your if statement at the end, why don’t you try adding a log statement to help debug your issue?
console.log(newStr, newStrArrReverseJoinMatch);
You should see that those values are of different types, which means you can’t really compare them.
Also, when you’re putting code in a post, be sure to place 3 back ticks on the line before, and 3 back ticks on the line after the code. Like this ↓
```
code here
```
To match all alphanumerical characters not including whitespaces
Lets decipher it:
\w+ means 1 or more alphanumerics
\S+ means 1 or more non-space char-s
If we have the whole thing:
HollyBomb:
/\w+\S+/
What does it mean?
system
Closed
January 4, 2023, 5:35am
25
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.