0

I am new to coding and I am just practicing with some functions and I came across something with the "strict equal" of my conditional.

I am building a simple palindromeChecker in JS.

so I made this:

const w = "Apple";

const palindromeChecker = (phrase) => {
  const lowerWord = phrase.toLowerCase().split(" ").join("").split("");
  console.log("LowerWord: --->", lowerWord);
  const result = lowerWord.join("")
  console.log("Result: --->", result);
  const reverse = lowerWord.reverse().join("");
  console.log("Reverse: --->", reverse);

  if (lowerWord.join("") === reverse) {
    return true
  } else {
    return false
  }
};

console.log(palindromeChecker(w));

the return keeps coming back true even though the word is clearly not a palindrome. I'm know that I can exchange:

lowerWord.join("")

in the conditional with:

result

and I get the correct answer, but I am wondering why I am getting "true" with lowerWord.join("") when the string is not the same?

thanks in advance!

Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

1

Because reverse() reverses the array in-place and returns it:

const reverseChecker = (arr) => {
  console.log("arr: --->", arr);
  const reverse = arr.reverse(); 
  console.log("arr: --->", arr);
};

reverseChecker([1, 2, 3]);
enzo
  • 9,861
  • 3
  • 15
  • 38