Palindrome Challenge If Statement

Palindrome Challenge If Statement
0.0 0

#1

`

function palindrome(str) {
  str = String.prototype.toLowerCase(str);
  str = String.prototype.replace(/\s+/g,"");
  str = String.prototype.replace(/[!@#$%^&*()-;:'"\/?<>,.]/,"");
  str = str.split("");
  for (i=0;i<str.length-1;i++){
    if (str[i] != str[str.length - i]){
      return false;
    }
  }

  return true;
}

`
For some reason, it doesn’t “enter” the if statement. Any suggestions as to why?


#2

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#3

Remember that because indexing starts at one, the last item of the array is at length -1.


#4

Can you see what’s wrong with this?

str = String.prototype.toLowerCase(str);


#5

Removed the str inside the parenthesis. Still doesn’t work…


#6

Here are the instructions on the syntax to be used for String.toLowerCase:

Does your line of code follow the correct syntax?


#7

I replaced String with str and I get “Type error: Cannot read property ‘toLowerCase’ of undefined”. Do I have to assign it to a different variable in order for it to work?


#8

It should be this:

str = str.toLowerCase()

Now make sure the other functions you’re calling have the correct syntax so your code moves forward into the IF statement.