Check for palindromes: toLowerCase not working

Check for palindromes: toLowerCase not working
0

#1

I’m slowly but surely butchering my through this exercise. As you can see I have most of the problems resolved, if by rather a brute force and totally ugly method. However, I am totally lost as to how to get toLowerCase to work. I’ve tried it as it is and as a separate str = str.toLowerCase(); nothing seems to be working. Any help would be greatly appreciated. I’m aware I could have clicked on the help button on the problem page, but it’s too easy to see the whole answer by doing that and I’m trying to avoid the easy way or I’ll never learn.


#3

I’m not sure what you mean by str = str.toLowerCase(); not working…

It does work:

var str = "Howdy";

console.log(str);
// Howdy

str = str.toLowerCase();

console.log(str);
// howdy

Use console.log statements to make sure everything is what you think it is. Whenever working on complex JS problems, I like to put them into codepen so I have more control. Learning to debug is part of learning to code.

And follow ShyC’s hints.


#4

Thanks both of you. I’ll go and see what I can cook up now :slight_smile:


#5

If you put a console.log after every transformation you’ll see the bug


#6

Can’t tell you how much that helped ppc.

I’ve managed to do everything but the penultimate character set. console.log give me this "00(::)00" in CodePen. Which looks like a palindrome to me. I’m at a loss as to why my code is returning false.

`function palindrome(str) {

// Good luck!
str = str.replace(/_/g, “”);
str = str.replace(/-/g, “”);
str = str.replace(/ /g, “”);
str = str.replace(/,/g, “”);
str = str.replace(/./g, “”);
str = str.replace(///, “”);
return str.toLowerCase().split("").reverse().join("") == str.toLowerCase();

}

palindrome(“0_0 (: /-\ :slight_smile: 0-0”);`


#7

As they say looks can be deceiving!

Write out its reverse under the string below

0 0 ( : : ) 0 0

Do you see the difference?


#8

I did not understand what the heck you were going on about for a good few minutes ppc. Not until I actually did as you suggested anyway.

Clearly, my way of passing this is quite hacky. I feel like I cheated somewhat by simply removing characters that got in the way of the result I was looking for.


#10

I would not call it hacky - the exercise specifically says to remove undesirable characters - that’s what you did - successful completion of an exercise is cause for celebration not criticism in case you missed the meaning of the big green checkmark you got for your solution

There’s always room for improvement - even the most experienced programmers do not get everything right the first time or even the second third or fourth time - Windows 8 is proof enough of that!

Few ways you can improve on this solution - can you remove all characters with just one replace - can you do it without a regex - can you detect a palindrome without removing any characters - can you solve the problem without using any other builtin objects like arrays or any string functions besides toLowerCase?


#11

Thanks. I’ve been finding all of this very hard. Which I guess I should, I’m right at the start. I’ll give these other variations a go. Thanks for the tips