2016年3月25日 星期五

javascript basic algorithm(12)--Mutations

Description: 
                  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.

Review: indexOf()

Solution 1:

Thinking process:
      1. convert each element into lowercase.
      2. iterate                                                   // by using indexOf(elem[i]) to search  

Code:  
               funciton mutation(arr){
                var elem0 = arr[0].toLowerCase;
                var elem1 = arr[1].toLowerCase;
                for(var i=0; i < elem1.length; i++){
                var check = elem0.indexOf(elem1[i]);
                if (check === -1) {
                 return false;
}
               else{return true;}
}
}


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

沒有留言:

張貼留言