Palindrome Project "almostomla" doesn't work

Tell us what’s happening:
Describe your issue in detail here.

My code passes all the tests except for this one: “almostomla”
Why??
Thanks for any explanation

 function palindrome2(str) {

let cleanStr = str.replace(/\W+|_/g, “”).toLowerCase();
//Just wanted to see difference, W also gets rid of punctuation and symbols
//s only recognizes white spaces
//let cleanStr = str.replace(/\s/g, “”).toLowerCase();

//get the first half of the string
let firstHalf = ;
let secondHalf = ;
for (let i = 0; i < cleanStr.length; i++) {
if (i < cleanStr.length / 2) {
firstHalf.push(cleanStr[i]);
} else {
secondHalf.push(cleanStr[i]);
}//else
} //for

//check to see if two lengths are the same
if (firstHalf.length !== secondHalf.length) {
secondHalf.unshift(firstHalf[firstHalf.length-1]);
}//if
//console.log(secondHalf);

//reverse secondHalf
let secondHalfRev = ;
for (let i=secondHalf.length-1; i>=0; i–) {
secondHalfRev.push(secondHalf[i]);
}
console.log(firstHalf);
console.log(secondHalfRev);

//compare first half with second half reverse to get a boolean, return boolean
//cannot directly check if two arrays are equal, must loop thru each element
if (firstHalf.length !== secondHalfRev.length) {
return false ;
}
for (let i=0; i<firstHalf.length; i++) {
if (firstHalf[i] === secondHalfRev[i]) {
return true;
} else {
return false;
}//else
}//for
} //function

console.log(palindrome2(“almostomla”));
Your code so far

function palindrome2(str) {
let cleanStr = str.replace(/\W+|_/g, "").toLowerCase();
//Just wanted to see difference, W also gets rid of punctuation and symbols
//s only recognizes white spaces
//let cleanStr = str.replace(/\s/g, "").toLowerCase();

//get the first half of the string
let firstHalf = [];
let secondHalf = [];
for (let i = 0; i < cleanStr.length; i++) {
  if (i < cleanStr.length / 2) {
    firstHalf.push(cleanStr[i]);
  } else {
    secondHalf.push(cleanStr[i]);      
  }//else
} //for

//check to see if two lengths are the same
if (firstHalf.length !== secondHalf.length) {
      secondHalf.unshift(firstHalf[firstHalf.length-1]);
    }//if 
  //console.log(secondHalf);

//reverse secondHalf
let secondHalfRev = [];
for (let i=secondHalf.length-1; i>=0; i--) {
  secondHalfRev.push(secondHalf[i]);
}
console.log(firstHalf);
console.log(secondHalfRev);  

//compare first half with second half reverse to get a boolean, return boolean
//cannot directly check if two arrays are equal, must loop thru each element
if (firstHalf.length !== secondHalfRev.length) {
  return false ;
}
for (let i=0; i<firstHalf.length; i++) {
  if (firstHalf[i] === secondHalfRev[i]) {
    return true;
  } else {
    return false;
  }//else
}//for  
} //function

console.log(palindrome2("almostomla"));
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36

Challenge: Palindrome Checker

Link to the challenge:

Notice what is going on here. You just compare first elements and return the result. Instead, you may check inside the loop if the elements at index i are not equal, and return false in this case. Otherwise, return true outside the loop.

for (let i = 0; i < arr.length; ++i) 
  if (arr[i] !== secondArr[i])
    return false

return true

Thank you very very much Udaaff,
At first I did use the inequality condition in the if statement, but it didn’t work. I had to get rid of the else statement also. It seems weird to me that an else statement was messing up the logic. Thanks again :grinning:

1 Like