Palindrome problem

Hello
I am reading an article on coding problems. I have some problems to understand the following code. I have checked it and it is working.

<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function uff(a) {
        const validCharacters = "abcdefghijklmnopqrstuvwxyz".split("");
        const stringCharacters = a
          .toLowerCase()
          .split("")
          .reduce(
            (characters, character) =>
              validCharacters.indexOf(character) > -1
                ? characters.concat(character)
                : characters,
            []
          );
// I don`t understand how this strict equality is working.  Why are these two values are equal?
        return (
          stringCharacters.join("") === stringCharacters.reverse().join("")
        );
      }

      console.log(uff("racecar"));
    </script>
  </body>
</html>

Thanks for the help.

It might help to add these console.logs so you can better understand the individual values

function uff(a) {
  const validCharacters = "abcdefghijklmnopqrstuvwxyz".split("");
  const stringCharacters = a
    .toLowerCase()
    .split("")
    .reduce(
      (characters, character) =>
        validCharacters.indexOf(character) > -1
          ? characters.concat(character)
          : characters,
      []
    );
  // I don`t understand how this strict equality is working.  Why are these two values are equal?
  console.log(`stringCharacters array: ${stringCharacters}`)
  console.log(`stringCharacters string: ${stringCharacters.join("")}`)
  
  console.log(`reverse of the stringCharacters array: ${stringCharacters.reverse()}`)
  
    console.log(`reverse of the stringCharacters array turned back into a string: ${stringCharacters.reverse().join("")}`)
  
  
  return stringCharacters.join("") === stringCharacters.reverse().join("");
}

uff("racecar")

When you compare the two strings, then you will see that they do match and that racecar is a palindrome

Oh thanks :+1:
I have done it and now it is clear. May I have an other question :upside_down_face:
I understand that it checks stringcharacters against validcharacters. I don`t get the second half of the ternary operator ( : characters, ).
What does it send back? An empty array? Why is it not enough just characters or just the empty ?
Thanks

Why do I need the empty array for? Sth goes into it?
Thanks :+1:

I have read about reduce method. Now I understand your answer.
Thanks for your help :+1:

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.