JavaScript Algorithms and Data Structures Projects - Palindrome Checker

function palindrome(str) {
  let arrCopy = str.slice();
  let arrLowerRemove = arrCopy.replace(/[\W_]/g,"").toLowerCase();
  let palindromeArray = [];
 
//  console.log(arrLowerRemove);
  for( let i = arrLowerRemove.length - 1; i >= 0; i--){
    palindromeArray.push(arrLowerRemove[i]);
  }
  // console.log(palindromeArray);
  for( let j = 0; j < palindromeArray.length; j++){
    // console.log(arrLowerRemove[j]);
    // console.log(palindromeArray[j]);
    if(arrLowerRemove[j] === palindromeArray[j]){
    return true;
    } else{
      return false;
    }
  }
 
}
 
let result = palindrome("2_A3*3#A2");
console.log(result);
function palindrome(str) {
  
*// creating a copy of string*

let arrCopy = str.slice(); 

*//remove the charcatcher and update the string to lower case*

 let arrLowerRemove = arrCopy.replace(/[\W_]/g,"").toLowerCase();

*//An empty array for reverse string* 
 
 let palindromeArray = [];

*//loop for push string character* 
 
 for( let i = arrLowerRemove.length - 1; i >= 0; i--){
    palindromeArray.push(arrLowerRemove[i]);
  }

*//compare the string or array*

  for( let j = 0; j < palindromeArray.length; j++){
    if(arrLowerRemove[j] === palindromeArray[j]){
    continue;
    } else{
      return false;
    }
  }
  return true;

}
*// end of function*

 palindrome("1 eye for of 1 eye.");

Thanks you for rply…
Problem: I passed all the input but failed for following test.

palindrome("almostomla") should return false.

solution: the for loop watch only compare the first element of array or string and return the true. I need to continue the loop and than return and break as soon as the there is unmatched value in array.

I updated the my solution and a better solution approach to this problem too.

function palindrome(str) {

  let arrLowerRemove = str.replace(/[\W_]/g, "").toLowerCase();

  let reversedStr = reverseString(arrLowerRemove);

  if (arrLowerRemove == reversedStr){
    return true;
  }else{
    return false;
  }
}

function reverseString(str){
  let reversedArr = str.split('').reverse();
  return reversedArr.join('');
}

let result = palindrome("almostomla");

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.