Finding letters in 2nd array element in 1st array element

Using javascript, I want to check if the string in the 1st array element contains all the letters of the 2nd array element. If so, return true. For example. ["Mary", "Aarmy"] => true; ["hello", "hey"] => false.

I've tried the following code, which works for ["Mary", "Aarmy"] and ["voodoo", "no"], but doesn't work for ["hello", "hey"]. Appreciate any help, thanks!

function mutation(arr){
var str1 = arr.pop().toLowerCase();
 var str2 = arr.pop().toLowerCase();
 for(var i = 0; i < str2.length; i++){
  if(str1.indexOf(str2[i]) !== -1){
    return true;
  }
    else return false;
 }
} 

      

+3


source to share


6 answers


When you use it pop()

, it will return the last item in the array, not the first.

Also yours if else

is inside for

and has a return statement. This prevents the loop from for

executing completely and returning after the very first loop.



function mutation(arr){
 var str2 = arr.pop().toLowerCase();
 var str1 = arr.pop().toLowerCase();
 console.log("str1: " + str1);
 console.log("str2: " + str2);
 for(var i = 0; i < str2.length; i++){
  if(str1.indexOf(str2[i]) === -1){
    return false;
  }
 }
 return true;
}

arr = ["hello", "hey"];
console.log(mutation(arr));

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

Run codeHide result


+4


source


Considering that you can use Set

and lodash

, here's another solution:

const _ = require("lodash");

function mutation(arr) {

  var set1 = new Set(arr.pop().toLowerCase().split(''));
  var set2 = new Set(arr.pop().toLowerCase().split(''));

  return _.isEqual(set1, set2);

}

console.log(mutation(["Mary", "Aarmy"])); //true
console.log(mutation(["hello", "hey"])); //false
console.log(mutation(["voodoo", "no"])); //false

      



Check out a working sample: https://jsfiddle.net/sayan751/3q8rtqy3/

+3


source


Split and sort to speed up your work - now only ONE comparison per set

var arr = ["Mary", "Aarmy","hello", "hey", "voodoo", "no"]

function mutation(){
  var str1 = arr.pop().toLowerCase().split("").sort().join("");
  var str2 = arr.pop().toLowerCase().split("").sort().join("");
  return str1.indexOf(str2) !=-1;
} 
while (arr.length) console.log(arr[arr.length-2],mutation())
      

Run codeHide result


+1


source


Recursively for sports only. If you are dealing with long (> 100 char) strings, this is risky as it relies on stack space.

const contains = function (a, b) {
  if (!b) return true;
  return a.indexOf(b[0]) !== -1 && contains(a, b.substring(1));
}

console.log(contains('mary', 'army')); // true
console.log(contains('hello', 'hey')); // false
      

Run codeHide result


+1


source


How can a function return more than one value;) (line 6) And if the second array is array length 3, it just checks the first 3 arrays of arrays. (line 4)

0


source


function mutation(arr) {

  var set1 = arr[0].toLowerCase();
  var set2 = arr[1].split('');

  return set2.every(function(element, index, array){
      if(set1.indexOf(element.toLowerCase()) != -1)
        return true;
    else
        return false;
  });

}

console.log(mutation(["Mary", "Aarmy"]));   //return true
console.log(mutation(["hello", "hey"]));    //return false
console.log(mutation( ["voodoo", "no"]));   //return false
console.log(mutation( ["voodoo", "vo"]));   //return true

      

0


source







All Articles