Reputation: 41
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. E.g. ["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;
}
}
Upvotes: 3
Views: 113
Reputation: 2476
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
Upvotes: 0
Reputation: 4946
Considering you can use Set
, and lodash
, here is 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 the working sample: https://jsfiddle.net/sayan751/3q8rtqy3/
Upvotes: 3
Reputation: 177851
Split and sort to speed up things - 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())
Upvotes: 1
Reputation: 211982
Recursively, just for sport. If you're 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
Upvotes: 1
Reputation: 17
How could function return more than one value ;) (line 6) And if second array has an array length of 3 it just checks the first arrays 3 elements. (line 4)
Upvotes: 0
Reputation: 2452
When you use pop()
it'll return the last element in the array and not the first.
Also your if else
is inside for
and has a return statement. This does not let the for
loop run completely and returns 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));
Upvotes: 4