Javascript Palindrome Function Not Working

67 views Asked by At

When I run this function:

var isPalindrome = function (string) {
  if (string == string.split('').reverse().join('')) {
    console.log(string + ' is palindrome.');
  }
  else {
    console.log(string + ' is not palindrome.');
  }
}

console.log(isPalindrome("phone")) ---> "phone is not a palindrome"

console.log(isPalindrome("anna")) ---> "anna is a palindrome"

It works. However, I am completely puzzled as to why this function will not do the same thing?

var isPalindrome = function(string) {
  var stringArray = string.split("");
  var reverseStringArray = string.split("");
  reverseStringArray.reverse();

  if (stringArray == reverseStringArray) {
    console.log("Victory!")
  } else {
    console.log("Defeat")
  }
}

console.log(isPalindrome("phone")) ---> "Defeat"

console.log(isPalindrome("anna")) ---> "Defeat"

Any ideas?

0

There are 0 answers