Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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.
- //(ignoring case)
- //MY CONVOLUTED SOLUTION
- function mutation(arr) {
- //convert each array element to a separate string and change to lowercase
- let str1 = arr[0].toLowerCase();
- let str2 = arr[1].toLowerCase();
- //create new array for comparison
- let newArr = [];
- //search for second string characters in first string
- for (let i = 0; i < str2.length; i++){
- if (str1.indexOf(str2[i]) != -1){
- //add second string characters to array only if they're found in first string
- newArr.push(str2[i]);
- }
- }
- //join array elements into one string
- newArr = newArr.join('');
- //compare
- return newArr == str2;
- }
- mutation(["Alien", "line"]);
- //MY REFACTORED SOLUTION (based on suggested solution)
- function mutation(arr) {
- //convert each array element to a separate string and change to lowercase
- let str1 = arr[0].toLowerCase();
- let str2 = arr[1].toLowerCase();
- //search for second string characters in first string
- for (let i = 0; i < str2.length; i++){
- //return false if a second string character isn't found in first string
- if (str1.indexOf(str2[i]) < 0){
- return false;
- }
- }
- //return true if all second string characters are found in first string
- return true;
- }
- mutation(["Alien", "line"]);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement