I know there’s an easier way to do this, and I did successfully achieve it by stripping out non-word (and underscore) characters. However, I thought I’d get silly with it and really make it a palindrome checker – that even checks (some) reversed symbols.
Here’s the trouble: all of FCC’s tests pass except for one. The one for 0_0 (: /-\ :) 0-0
fails, but the code DOES correctly determine that it is a palindrome and displays it appropriately in the #result div!
If the result is arrived at legitimately and is displayed as required, I don’t think it should fail. Seems like a bug in the test.
The output looks correct, until I run innerText on it.
result.innerText
'0_0 (: /-\\ :) 0-0 is a palindrome'
Edit: Actually, I guess that is how it is supposed to be.
If I check using my own passing code, if I manually add 0_0 (: /-\ :) 0-0 and log the input value I get the same string back, but if I set the .value property on the input element I get 0_0 (: /- :) 0-0 which is what the test does.
Inspecting the page to make sure there isn’t some trick happening, I see <div id="result">0_0 (: /-\ :) 0-0 is a palindrome</div>
The funny thing here is that in my case, I’m re-displaying the string taken straight from the textbox (with theInput.value), so there should be no circumstance where the part before “is a palindrome” shouldn’t match. I can make the test pass by changing my checkPalindrome function to just use .replace(/(\W|_)/g, ''); on the string – but the only thing checkPalindrome returns is “is” or “is not”!
@lasjorg 's reply is interesting, and maybe there’s something behind the scenes with that where that’s what is causing this to fail? But as far as the result div is concerned, the output does indeed match what the test says it requires.
Well, your checkPalindrome is returning “is not” for that test. Your code is kind of hard to read and reason about with all those replace happening and the splitting.
All you have to do is remove non-alphanumeric characters and lowercase it, save that in a variable. Then split/reverse/join that variable and save that to a different variable. Now compare the two variables.
@torgborg I guess if you just remove the $ from your replace it should pass.
Oh yeah, the code is definitely goofy and unnecessary. I did successfully pass the test, but then I was just messing around to try to flip symbols (like \ and /) so they’d be actual mirror image-style palindromic. That’s when I noticed that for whatever reason, code that produces the required result is marked as non-passing.
I don’t think this is a huge deal, since it’s unlikely others will try to do this same thing I did, but I just wanted to report it in case it helps refine the testing mechanism!
you are not removing non-alphanumeric characters, so this fails.
Why it happens like that, I think it is some issue with escape characters
does it count as a bug with the tests? probably. if you want you can open an issue for it, but considering that the issue is with characters that should be removed anyway, I would consider it really low priority. Unless we find that the output text has issues and is making the tests fail.
For that specific test all you should be doing is comparing 0000 to 0000 because after you have removed all non-alphanumeric characters that is what is left. I’m not sure if it really matters what the exact input is.
I was trying to figure out why pasting the string in the input field showed is passing, while the test failed, it looks like it’s one of those weird string behaviours
But I might even consider it a feature and not a bug.
The requirement is to remove all non-alphanumeric characters, the reason why we have tests for hardcoding is that you shouldn’t be relying on knowing the input.