Split not a function

Hello, I am currently doing the Palindrome project. I though my code would work(is super simple, beginner like) but I get an error saying : “lowChar.split is not a function”. Can someone explain to me what I am doing wrong please.

function palindrome(str) {
  var char= str.replace(/[\W_]/g, ""); 
  var lowChar= char.toLowerCase;
  var revChar= lowChar.split("").reverse().join(""); 
  
  if(lowChar === revChar){
    return true;
  }
  else{
    return false;
  }
}

palindrome("eye");

The issue is with this line:

var lowChar= char.toLowerCase;

toLowerCase is a method, thus the line should be:

var lowChar= char.toLowerCase();

Without the () your just getting the toLowerCase property and not executing it as a function, thus your essentially trying to do this:

var myExample = {
  toLowerCase: function() {} // Doesn't matter what this does, only that it is a function
};
myExample.toLowerCase.split(''); // this will produce the same error for the same reason

Thank you so much!! :sweat_smile:

1 Like