Tell us what’s happening:
Your code so far
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
1 Like
one of function call requires _eye… and why that one is eliminated in regExp.
I cant understand that yet?
If that underscore is removed , how it will be true. it is not possible though.
Hey Rmdawson71,
I can understand that non alphanumeric instruction part,
But what i dont understand is _eye needs to be returned as true, as it is one of the challenge condition.
so when I pass the string “_eye” the replace function will eliminate the _ with regExp characters.
So how come the “_eye” condition will return true? because the underscore is eliminated with regExp
I hope you understand my question
Mean while the my code is running , but i dont feel this is a correct form of code. check and tell me.
function palindrome(str) {
low = str.toLowerCase();
regExp = low.replace(/[\W_]/g, "");
s = regExp.split("");
r = s.reverse();
j = r.join("");
if (regExp == j)
{return true;}
else {return false;}
}
palindrome("_eye");
Rmdawson71,
Thank you for helping me out
This displays whether the string is a palendrome or not
Click Me!
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');