The value of match when not matched

The value of match when not matched
0

#1

‘xyz’.match(‘w’);
null

I wrote the code to solve the problem

function mutation(arr) {
  s1=arr[0].toLowerCase();
  s2=arr[1].toLowerCase().split('');
  s2.map(function(x){
    if (s1.match(x)===null){return false;}
  });
  return true;
}

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


#2

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.


#3

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


#4

.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
s2.map(function(x) {...}); 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.


#5

It was solved with for loop.

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