Not sure how to fix my code for 'Check for Palindromes'

Not sure how to fix my code for 'Check for Palindromes'
0

#1

Tell us what’s happening:
I’m sure my code is more complicated than needed, but I tried to lay it out in a sequence that is step by step rather than combining many factors into a single step or loop. Im pretty sure my problem is in the variable check but I’m not sure what or how to fix.
Thank you for your help in advance.

Your code so far

function palindrome(str) {
  var re = /\W+/gi;
  var check =  
  str.replace(re,"") //regex to remove non alphanumeric characters, which doesnt stop after it finds one match
  .toLowerCase() //changes to lower case
    .split("").reverse().join(""); //reverses the string
    if (check === str) {
      return true; 
    } else {return false;
           }
}



palindrome("_eye");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 OPR/50.0.2762.67.

Link to the challenge:


#2

I have realized I can’t define a variable like I’ve done, however still any suggestions is appreciated to help me understand better, thank you.


#3

If you notice, all the tests you are failing are all cases that are all supposed to return false. That means your code is always returning true. Try using console.log statements to figure out what happens after every function you chain to str.


#4

I see what your saying but it says my str function is not defined however if I try to define it in a variable theres a message saying already defined.

function palindrome(str) {
	var re = /[\W_]/g;
	var process = str
		.replace(re, '') //regex to remove non alphanumeric characters, which doesnt stop after it finds one match
		.toLowerCase() //changes to lower case
		.split('')
		.reverse()
		.join(''); //reverses the string
		console.log(process, str)

	if (process === str) {
		return true;
	} else {
		return false;
	}
}

palindrome('_eye');

In my process variable I can’t tell why but it won’t run through it and change the code. So I see that none are changing from the original question. I still can’t tell why. I know the if else statement is working in theory because I can change what they are equal to and that changes my outputs but not sure why the regex or when I try to turn it into an array it doesn’t work.

Thank you for your help


#5

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.


#6

The problem with your solution is str is still the same value as when it was passed to the function. In the following test case, when your if statement checks if process === str, it is checking if ‘eye’ === ‘_eye’. They are not equal, the function returns false.

palindrome('_eye');

#7

Thank you, I was able to solve it quickly after that.
Here was my solution. I will now look to improve it and make it more readable, thanks again.

function palindrome(str) {
  
  
  var re = /[\W_]/g; 
  
   var first = str.replace(re,"") //regex to remove non alphanumeric characters, which doesnt stop after it finds one match
   .toLowerCase(); //changes to lower case
   var second =
   str.replace(re,"")
   .toLowerCase()
    .split("").reverse().join(""); //reverses the string
   
  
  
    if (first === second) {
      return true; 
    } else {
      return false;
           }
}