function palindrome(str) {
// Good luck!
t = str.toLowerCase();
x = t.split("");
y = x.reverse();
z = y.join("");
regExp = z.replace(/[\W_]/gi, "");
if(regExp === str)
{
return true;
}
else
{
return false;
}
}
palindrome("eye");```
**Your browser information:**
Your Browser User Agent is: ```Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/60.0.3112.78 Chrome/60.0.3112.78 Safari/537.36```.
**Link to the challenge:**
https://www.freecodecamp.org/challenges/check-for-palindromes
Let me help you break down what your code is doing for a specific test which you are failing:
palindrome("_eye");
See the comments to the right of each step below in your code:
function palindrome(str) {
// Good luck!
t = str.toLowerCase(); // t = "_eye"
x = t.split(""); // x = ["_", "e", "y", "e"]
y = x.reverse(); // y = ["e", "y", "e", "_"]
z = y.join(""); // z = "eye_"
regExp = z.replace(/[\W_]/gi, ""); // regExp = "eye"
if(regExp === str) // you are asking if "eye" is equal to "_eye" because str never changed
{
return true;
}
else
{
return false; // because the if statement condition evaluated to false, false is returned
}
}
Now you should have a better idea of what to compare. If you need any more assistance, just ask and someone will respond.
In the challenge instructions there is the following note:
You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything lower case in order to check for palindromes.
So, one of the first things you need to do is remove all all non-alphanumeric characters first. This means characters which are not 0 thru 9 and which are not the letters A thru Z and not the letters a thru z. The following line removes the non-alphanumeric characters from the variable z and assigns them to regExp, but the replace needs to occur after the str.toLowerCase and before the split statement.
So how come the “_eye” condition will return true?
Based on the following definition in the challenge of a palindrome:
A palindrome is a word or sentence that’s spelled the same way both forward and backward, ignoring punctuation, case, and spacing.
So, we can only determine if the characters are the same forwards and backwards if we ignore the punctuation and spaces, so that is why we replace them with blank strings before try to make any comparisons.
“_eye” becomes “eye” which should return true, because it spells the same forwards or backwards.
Does this answer your question?
As far as correct code form, I would make the suggestion to name your variables in such a way as they describe the data they hold. It makes it easier for others to read what your code is trying to do. For example, if you rewrote your solution as:
Another approach using the same logic is to chain the functions together with “.” and let the function names explain what is going on instead of using variable names:
function palindrome(str) {
cleanStr = str.toLowerCase().replace(/[\W_]/g, "");
reverseStr = cleanStr.split('').reverse().join("");
if (cleanSt == reverseStr)
{return true;}
else {return false;}
}
const numeric = "1234567890";
const alphaLower = "abcdefghijklmnopqrstuvwxyz";
const alphaUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
// constant time complexity, O(1),
// since the number of numeric, alphabets are constant
function convertToAlphaNumeric(c){
var indexUpper = alphaUpper.indexOf(c);
if(indexUpper >=0 )
return alphaLower(index);
if(alphaLower.indexOf(c) >=0 )
return c;
if(numeric.indexOf(c)>=0)
return c;
return '';
}
function palindrome(str) {
// shallow copied into new memory of length of str
var arr = Array.from(str, x=> convertToAlphaNumeric(x) );
// to prevent copying whole array into new memory space
// for every changes, used 2 pointers and change
// the value in existing (original) array, "arr".
var j=0;
for(var i=0; i<arr.length; i++){
if(arr[i] != ''){
arr[j++] = arr[i];
}
}
arr.length=j; // resize array without any consuming extra memory
// by using 2 pointers, p1 starts from 0 and moving forward,
// p2 starts from the end of the array, moving backward,
// so that we will need to look up only half length of the array
var p1 = 0;
var p2 = arr.length -1;
while(p1 < p2){
if(arr[p1] != arr[p2]) return false;
p1++;
p2--;
}
return true;
}
palindrome('_eye');