Palindrome Project

Tell us what’s happening:

My code returns a boolean, however FCC is suggesting that it’s not. I don’t quite understand why this is happening. any help would be greatly appreciated.
My code is basically breaking the letters down to their charCodes and comparing a string of numbers instead of letters.

If you would like to see the results of ‘base’ and ‘reversed’ just replace the returned statement with the following: return [base, reversed] so you can see the results in an array.

Your code so far



function palindrome(str) {
// Good luck!
let re = /[a-z]/gi;
let base = str.match(re)
              .map(e => [e.toLowerCase().charCodeAt()])
              .flat()
              .join('');

let reversed = str.split(' ')
                 .sort((a,b) => b - a)
                 .map(e => e.split('').sort((a,b) => b - a))
                 .map(e => e.join('').toLowerCase())
                 .join('')
                 .match(re)
                 .map(e => e.charCodeAt())
                 .join('');
 return base == reversed ;
}


palindrome("eye");




Your browser information:

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

Challenge: Palindrome Checker

Link to the challenge:

When I run my code in my editor, it returns a boolean, every time.

At a quick glance your last line is comparing 2 values/of their respective variables…
it should return a boolean i think… :thinking:

 return base == reversed ;

Hey man, the issue is not that it returns a boolean or not, but rather that it fails in two test cases. It seems to be that the code does not properly remove all non-alphanumeric characters. The test case palindrome(“0_0 (: /-\ : ) 0-0”); give TypeError: str.match(…) is null.

yea it does. I tested this in my editor. …

I even tried using the following :neutral_face:

return base === reversed ? true : false

for some reason it only acknowledges the true values…

yea i figured that one would fail ( i still need to fix that)…but heres the thing…

test with

Blockquote ```
not a palindrome

It comes out as false in my editor(which is correct)… but when I apply it in FCC, it says that it should return false?

try testing it in your editor to see what i’ve referring to.

I don’t know what’s wrong, but let me point a few issues:

you first purposefully make it an array of arrays and then you flat it
why then make it an array of arrays?

why do you make the string in reverse alphabetical order?

standby folks, i’ll explain my logic in a moment.

I think that the issue is that you are not including the numeric values in your regex variable re.

My logic::

When I drew it out on paper, this is what i was thinking:

" All I have to do is reverse the letters and compare them to see if they match it’s original counterpart."

For the reversed

  1. break the string into an array

  2. Take each element ( words ) in the array and place those into an array to be sorted in reverse order).

" .map(e => e.split(’’).sort((a,b) => b - a)) "

// [ [anamaP], [lanac], [a], [nalp] ] etc…

The only reason I did #2 was because I needed a way to reverse EACH word in the sentence.

  1. for each reversedWord in the array, join them and transform to lowerCase.

  2. match both the reversed and original.

keep in mind, that this is a rough draft. i generally toss the code together to get it to work,
then i go back and refactor things to make it more readable and logically sound.

…now that i’m explaining my code… it don’t see a reason for charCodeAt, so that will likely be removed.

ieahleen:

I see what you mean, that flat() can be removed: not needed. Thanks

I’m confused… even if I use the following?

return base === reversed ? true : false

big advice: check what your code is doing at each step.

I removed the charCodeAt() from your code as you said, but look at this:

function palindrome(str) {
// Good luck!
let re = /[a-z]/gi;
let base = str.match(re)             //  ["n","e","v","e","r","o","d","d","o","r","e","v","e","n"]
              .map(e => [e.toLowerCase()])     // [["n"],["e"],["v"],["e"],["r"],["o"],["d"],["d"],["o"],["r"],["e"],["v"],["e"],["n"]]
              .flat()    // ["n","e","v","e","r","o","d","d","o","r","e","v","e","n"]
              .join(''); //  "neveroddoreven"

let reversed = str.split(' ')   // ["never","odd","or","even"]
                 .sort((a,b) => b - a)  //   ["never","odd","or","even"]
                 .map(e => e.split('').sort((a,b) => b - a))  // [["n","e","v","e","r"],["o","d","d"],["o","r"],["e","v","e","n"]]

/* I would have stopped here to find out why it is not happening what I want */
}


palindrome("never odd or even")

yes, even then. See my post above.

even if it works… are you sorting based on which criteria? alphabetic order maybe? is that what you want?

hmm…

base on you code I can simply use map to just pass in the lowerCase letters ( without the array and charCode); while also getting rid of flat().

  let base = str.match(re)
                .map(e => e.toLowerCase())
                .join('')  // neveroddoreven

I didn’t want to have it in reverse alphabetical order per’se, i just wanted it in reverse order.

this is what I got for the “reversed” variable:

  let reversed  = str.split(' ')
                   .sort((a,b) => b - a) //  [ "even", "or", "odd", "never" ]

                   //.map(e => e.split('').sort((a,b) => b - a))                                  
                  /* .map(e => e.join('').toLowerCase())
                   .join('')
                   .match(re)
                   .map(char => char.charCodeAt())
                   .join('') */
   return reversed;

};

// the problem with [ “even”, “or”, “odd”, “never” ] is that if I choose to join them, then it’s essentially words in the reversed order.

So I needed away to reverse the order of the words in the sentence AND reverse the letters within those words; establishing a mirror effect; its like holding up a word on a piece of paper in a mirror and looking at the complete opposite of what you hold.

Got it, thanks! for the input, guys! Much appreciated.

Ok… this is really hard for me to get over. I understand that the sort method shouldn’t work the way how I set up my code… but when i run it on my PC it’s actually reversing the order of the array.

FYI - I took a few moments and got rid of the clutter in my code…

I’ve attached pictures of what i’m seeing…In capture one, you can see within FCC that the sort method works as suggested.(FCC won’t let me upload two at a time, so i’ll put the other pic in a separate reply)

However, the second capture of my editor, shows the sort method literally reversing the text in my array.

Can you explain that?..this is driving me nuts.

function palindrome(str) {
      // Good luck!
      let re = /[a-z]|[0-9]/gi;
      let base = str.match(re)
                    .map(e => e.toLowerCase())
                    .join('');
       console.log(base)
       
       let flipped = [].concat(...base)
                       .sort((a,b) => b - a)
                       //.join('')
       
       console.log(flipped);

      return base === flipped
};
let text2 = "nope"

console.log(palindrome(text2));

So the sort function swaps the numbers inside the array. So if you swap all then you will have the same thing as reverse of the array. You can also try: foo.sort((a,b) => {return 1});

When you say swap numbers, i’m assuming you’re referring to the index numbers? correct?

FYi everyone, I solved the challenge using the following:

function palindrome(str) {
      // Good luck!
      let re = /[a-z]|[0-9]/gi;
      let base = str.match(re)
                    .map(e => e.toLowerCase())
                    .join('');
      
      let flipped = [].concat(...base)
                      .reverse()
                      .join('')

      return base === flipped
};
console.log(palindrome("nope"));

I would like to thank everyone for their assistance.