'Return true if the string in the first element of the array contains all of the letters of the string in the second element of the array

My code is running except for mutation([“Mary”, “Army”]) should return true, but it is returning false and mutation([“Mary”, “Aarmy”]) should return true, but it is returning false.

Can someone please explain why this is happening?

My code so far

function mutation(arr) {
    a=arr.slice(0,1); var a_str  = a.toString();  a_arr  =  Array.from(a_str);  
    b=arr.slice(1,2); var b_str  = b.toString();  b_arr  =  Array.from(b_str);
    var  flag=0; 

    for(var  i=0;i<b_arr.length;i++){  
        for(var  j=0;j<a_arr.length;j++){
            if(b_arr[i]==a_arr[j]){
                flag+=1;
            }
        }
    }

    if(flag>=b_arr.length){
        return  true;
    } else  return  false;
}

console.log(mutation(["Mary", "Aarmy"]));


Solution 1:[1]

Problem :

The problem with your code is that JavaScript is case-sensitive while your code doesn't expect that so here is what you can do :

Solution :

if both are equal in that case :

then you can basically do anagram check :

anagram = _ => _.toLowerCase().split('').sort().join('').trim();

const isAnagram = _ => anagram(_[0]) === anagram(_[1])

Other wise you can do something along these lines :

check = a => !a[0].split ``.map(_ => _[X = `toLowerCase`]()).filter(_ => !a[1].includes(_[X]())).length

console.log(check(['Mary','Aarmy']))

Solution 2:[2]

You can do this really simply if you use Set. For example:

const mutation = arr => {
  var a = new Set(arr[0].toLowerCase())
  var b = new Set(arr[1].toLowerCase())
  return [...a].every(ch => b.has(ch))  
}

console.log(mutation(["Mary", "Army"]))
console.log(mutation(["Mary", "Aarmy"]))
console.log(mutation(["Mary", "Aary"]))
console.log(mutation(["ab", "abba"]))
console.log(mutation(["voodoo", "no"]))

I still feel like there is some ambiguity in the question, but if exactness was required, you could also just check that the set sizes were equal (as they contain all unique letters)

Solution 3:[3]

You can use map, filter and toLowerCase functions to achieve this.

Example:

function mutation(arr){ 
    return !arr[0].split("")
    .map(x => x.toLowerCase())
    .filter(l => !arr[1].includes(l.toLowerCase())).length
}

var res = mutation(["Mary", "Aarmy"]);

console.log(res); //true

Solution 4:[4]

You need to use toLowerCase() to compare each character with case insensitivity. And you can further remove the extra inner loop and do something like this:

function mutation(checkArray){
  var firstElement = checkArray[0];
  var secondElement = checkArray[1];
  var splitSecondElement = secondElement.split('');
  var matched = true;
  for(var i=0; i<splitSecondElement.length; i++){
    var firstElementLower = firstElement.toLowerCase();
    var characterLower = splitSecondElement[i].toLowerCase();
    if(firstElementLower.indexOf(characterLower) === -1){
      matched = false;
      break;
    }
  }
  return matched;
}

console.log(mutation(["Mary", "Army"]));
console.log(mutation(["Mary", "Aarmy"]));
console.log(mutation(["Mary", "Aarmyxyz"]));

Solution 5:[5]

var yes;
function mutation(arr){
arr[1]=arr[1].toLowerCase();
arr[0]=arr[0].toLowerCase();

for (var i=0; i<a[1].length; i++){
    if(!(arr[0].includes(arr[1].charAt(i))){
    return yes= false;
    }
    else{
         yes= true;
          }
   }
   console.log(yes);
  }
  mutation(["hello","hey"]);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Muhammad Salman
Solution 2
Solution 3 Mohamed Abbas
Solution 4
Solution 5 Samanja Cartagena