Can you please check my code

Tell us what’s happening:
in a first loop while using regex i am getting this result why it generating false?
n0true

e1false

v2true

e3false

r4true

5false

o6true

d7false

d8true

9false

o10true

r11false

12false

e13true

v14false

e15true

n16false
Your code so far


function palindrome(str) {
let regex=/[a-z0-9]/ig;
let strArr=str.split("");
let newArr=[];
let flag=0;
for(let i=0;i<strArr.length;i++)
{ let test=regex.test(strArr[i]);
  console.log(strArr[i],i,test);
  if(test)
  {
   newArr.push(strArr[i]);
  }
}

newArr=newArr.join("").toUpperCase();
for(let i=0;i<newArr.length;i++){
if(newArr[i]!==newArr[newArr.length-(i+1)])
{
  flag=1;
  break;
}
}
if(flag===0)
{
  return true;
}
else
{
  return false;
}
}



palindrome("never odd or even");
  **Your browser information:**

User Agent is: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36.

Challenge: Palindrome Checker

Link to the challenge:

remove the g flag

there is a part in this link that explains what happens when you use the g flag with the test method:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test

2 Likes

It worked and thanks for providing the link.