Function palindrome (str)

Tell us what’s happening:

can anyone tell me what is missing to complete this lesson? Thanks

function palindrome(str) {
str = str.toLowerCase().replace(/[W_]/g, " ");
for (var i = 0; i<str.length -1; i++) {
  if (str [i] !== str[i].toLowerCase()){
    return false;
  } 
}
return true;
}

palindrome("eye");
  **Your browser information:**

User Agent is: Mozilla/5.0 (Linux; Android 6.0.1; P01T_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.181 Safari/537.36 (Ecosia android@88.0.4324.181)

Challenge: Palindrome Checker

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

can anyone tell me what is missing to complete this lesson? Thanks

What is missing is code that does what was asked and passes the test.

Snark aside, please ask better questions. I can’t answer the question you asked without writing out 3 paragraphs that tell you exactly how to solve the problem.

One step would be to put it in log statements to understand what is happening:

function palindrome(str) {
  str = str.toLowerCase().replace(/[W_]/g, " ");
  console.log(str);
  for (var i = 0; i<str.length -1; i++) {
    console.log('comparing', str[i], 'and', str[i].toLowerCase());
    if (str[i] !== str[i].toLowerCase()){
      return false;
    } 
  }
  return true;
}

palindrome("A man, a plan, a canal. Panama");

See what is happening and how that differs from what you expect.

Another step is to look at all the tests that are failing. Do you notice that they all have something in common? That is a big clue.

Also, what does your code test? I don’t think it tests what you think it tests.

Think through an explicit step by step process of how to do this. There is more than one way to do it. Imagine that you had the letters in front of you on note cards. How would you do this. You can’t just “look and know”, you need a step by step process.

Work with that and check back if you are still having trouble.

I’m not trying to blow you off - I’m trying to help you develop some debugging skills that are very important. Good coders are good debuggers, and good debuggers are good detectives.

And don’t get discouraged - everyone struggles with some of the algorithms and many have struggled with this one.

I struggled with a similar issue and I am a total newbie. Your regex may need to be refined. Look at this line:
if (str[i] !== str[i].toLowerCase())

you got the first element of the string but how would you get the last in a way that it decreases by one as you go along. You know how to get the last element in your code.

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