Skip to content

Instantly share code, notes, and snippets.

@mmloveaa
Last active April 7, 2016 21:08
Show Gist options
  • Select an option

  • Save mmloveaa/2fb24743db29a7f132008d0a8354a2e8 to your computer and use it in GitHub Desktop.

Select an option

Save mmloveaa/2fb24743db29a7f132008d0a8354a2e8 to your computer and use it in GitHub Desktop.
4-4 Q12 Mutation
Mutations
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.
For example, ["hello", "Hello"], should return true because all of the letters in the second string are present in the first, ignoring case.
The arguments ["hello", "hey"] should return false because the string "hello" does not contain a "y".
Lastly, ["Alien", "line"], should return true because all of the letters in "line" are present in "Alien".
Remember to use Read-Search-Ask if you get stuck. Write your own code.
Here are some helpful links:
String.indexOf()
function mutation(arr) {
var word1=arr[0].toLowerCase().split('');
var word2=arr[1].toLowerCase().split('');
for(var i=0; i< word2.length; i++){
if(word1.indexOf(word2[i])<0){
return false;
}
}
return true;
}
mutation(["hello", "hey"]);
Other methods:
function mutation(arr) {
var word1=arr[0].toLowerCase().split('');
var word2=arr[1].toLowerCase().split('');
return word2.reduce(function(accum,item){
return word1.indexOf(item)<0 ? false : accum;
},true);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment