The value of match when not matched


I wrote the code to solve the problem

function mutation(arr) {
    if (s1.match(x)===null){return false;}
  return true;

mutation([“hello”, “hy”]);
Why the result is true,not false?

I don’t think .map is appropriate to solve this. No matter what happens in .map, your function will always return true.

Try using a for-loop. You might also be better off using .toLowerCase for both s1 and s2.

it is a typo for toUpperCase.
No matter what happens in .map, your function will always return true.

.map returns a new array (based on s2 in your code). The function in .map only tells what the elements in the new array will look like. It has virtually no effect on the mutation function.

Also. note that nothing catches the value returned by .map, so the entire {...}); business essentially does nothing. After that you hit return true, every time.

Another way to think about it is that return is attached to the function it resides in. So the return false is attached to the function in .map and is in no way related to the mutation function. The only return attached to mutation is the return true line at the bottom, so that’s only what mutation returns.

It was solved with for loop.

function mutation(arr) {
  for(var i=0;i<s2.length;i++){
    if (!s1.match(s2[i])){return false;}
  return true;