0

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. No case-sensitivity and order doesn't matter only the letters matter. For ex - ["Hello","hello"] returns true and so does ["Alien","lien"] and also ["Mary", "Aarmy"]. I think you get it. If not return false.

I could solve it with Array.indexOf() === -1 (in the first for loop) but can it work with this code, it's the opposite. I just can't make it return false. Ultimately, I wanna know, can you make it return false without changing the method.

  function mutation(arr) {
  var split = arr[1].toLowerCase().split("");
  var splitSecond = arr[0].toLowerCase().split(""); 
  for(k=0;k<=arr[0].length;k++){
   for(i=0;i<=arr[1].length;i++){
     if(split[i]===splitSecond[k]) {
      return true
   }
  }
 } return false
}

mutation(["hello", "hney"], "");  

If using any other method, explain :)

4
  • 2
    {E,H,L,O} ⊈ {E,H,Y}, so why would ["Hello","hey"] return true? Commented Sep 22, 2015 at 12:54
  • Sorry, I didn't see that. Edited Commented Sep 22, 2015 at 12:56
  • Did any of these answers solve your issue? If so, please mark the best answer as the accepted answer by checking the green checkmark to the left of that answer. That will indicate to the community that your question has been answered. Then both you and the person who provided the answer will earn some reputation points that can lead to more privileges on StackOverflow. Commented Sep 29, 2015 at 0:24
  • Nope, it didn't really help me. I wanted to delete this post, but I couldn't because it had answers :) Commented Sep 29, 2015 at 8:34

3 Answers 3

1

The problem with your code is that you return true; as soon as one letter matches.

What you need to do is check if all letters match, which is easier achieved by checking if any letter doesn't match.

 mainloop:
 for(k=0;k<=arr[0].length;k++){
   for(i=0;i<=arr[1].length;i++){
     if(split[i]===splitSecond[k]) {
       continue mainloop; // found the letter, move on to next search
     }
   }
   return false; // failed to find letter, fail here
 }
 return true; // haven't failed yet and end of input reached. Success!

Another alternative would be:

for(k=0;k<arr[0].length;k++) {
  if( arr[1].indexOf(split[k]) < 0) {
    // letter not found
    return false;
  }
}
// not failed yet? Excellent!
return true;
Sign up to request clarification or add additional context in comments.

Comments

0
function mutation(arr) {
    var test = arr[0].toLowerCase(),
        chars = arr[1].toLowerCase(),
        len=chars.length;
    for(var i=0;i<len;i++)
        if(test.indexOf(chars[i])==-1) //this char not exist in test string
            return false;
    return true;//all chars already checked
}
mutation(["hello", "he"]);

https://jsfiddle.net/hb2rsm2x/115/

Comments

0

Here is an interesting way using regular expressions. escapeRegExp was taken from here.

function mutation(arr){
  var matcher = new RegExp('[^'+escapeRegExp(arr[1])+']', "i");
  return arr[0].match(matcher) ===  null;
}

function escapeRegExp(s) {
    return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.