mercredi 13 novembre 2019

Who can tell me where I am doing wrong? (RegExp.prototype.test())

I was doing this exercise: https://edabit.com/challenge/eCPim4XcssdZdvs32

Here is my code:

   function numInStr(arr) {

let newArr = []
  let re = /[0-9]/g

for (let i=0; i<arr.length;i++) {
  if(re.test(arr[i])){
    newArr.push(arr[i])
  }
}
return newArr
}


numInStr(["1a", "a", "2b", "b"]) //➞ ["1a", "2b"]

numInStr(["abc", "abc10"]) //➞ ["abc10"]

**numInStr(["abc", "ab10c", "a10bc", "bcd"]) //➞ ["ab10c", "a10bc"] (PROBLEM IS HERE)**

numInStr(["this is a test", "test1"]) //➞ ["test1"]

I passed all tests except one:

numInStr(["abc", "ab10c", "a10bc", "bcd"]) //➞ ["ab10c", "a10bc"]

My function return only ["ab10c"]. I tried to understand but still no sense. If I changed the order of elements, my function works:

numInStr(["abc", "a10bc", "bcd","ab10c"]) //[ "ab10c", "a10bc"]

I solved this problem in another way. But I want to know where I'm doing it wrong. Can you explain to me? Why Regex test method does not work?

Aucun commentaire:

Enregistrer un commentaire