0

This works:

var animals = ["caterpillar", "dog", "bird"];
var catMatch = /cat/i;
var catFound = animals.some(function(animalName) {
  return catMatch.test(animalName);
});

console.log(catFound);

But this doesn't

var animals = ["caterpillar", "dog", "bird"];
var catMatch = /cat/i;
var catFound = animals.some(catMatch.test);

console.log(catFound);

Why doesn't the second version work?

Ry-
  • 209,133
  • 54
  • 439
  • 449
linuxdan
  • 3,898
  • 3
  • 26
  • 37

1 Answers1

2

RegExp.prototype.test depends on its this value to work. You can pass the required this value to some:

var catFound = animals.some(catMatch.test, catMatch);
Ry-
  • 209,133
  • 54
  • 439
  • 449