Javascript Palindrome Checker

Hello, I am trying to solve the Palindrom algorithm and so far, when I check my code on my IDE it is working fine for all the conditions but fails in freecodecamp’s test on the followings :

  • (“race car”) should return true
  • (“A man, a plan, a canal. Panama”) should return true
  • (“never odd or even”) should return true
  • (“My age is 0, 0 si ega ym.”) should return true
  • (“0_0 (: /-\ : ) 0-0”) should return true
    all of the above conditions work appropriately with VS Code…
    I would appreciate if someone could guide me here. :slight_smile: Thanks
    Your code so far

let cleanStr = "";
let list1 = [];
let list2 = [];
let result ="";

function palindrome(str) {
  let finalStr = str.replace(/[^A-Z0-9]/ig, "");
  finalStr = finalStr.toLowerCase();
  
  for(let i in finalStr){
    list1.push(finalStr[i]);
  }

  for(let i in finalStr){
    list2.push(finalStr[i]);
  }
  
  list2 = list2.reverse();
  let i = 0;
  let _ = 0;
  for( i in list1)
    for( _ in list2)
      if (list1[i]!=list2[i]){
        result = "Not Palindrome"
      }
      
  if(result ==="Not Palindrome"){
    return false;
  } else{
    return true;
  }
      }

palindrome("eye");
  **Your browser information:**

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

Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Link to the challenge:

Your code contains global variables that are changed each time the function is run. This means that after each function call completes, subsequent function calls start with the previous value. To fix this, make sure your function doesn’t change any global variables, and declare/assign variables within the function if they need to be changed.

Example:

var myGlobal = [1];
function returnGlobal(arg) {
  myGlobal.push(arg);
  return myGlobal;
} // unreliable - array gets longer each time the function is run

function returnLocal(arg) {
  var myLocal = [1];
  myLocal.push(arg);
  return myLocal;
} // reliable - always returns an array of length 2

Thank you very much Jeremy :slight_smile:

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