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: