Reputation: 2764
Please help me think this through... I am trying to check and see if all of the letters in the second element of an array are contained within the letters of the first element, ignoring case. So "Aliens" and "lines" should return true because all of the letters in "lines" are included in "Aliens" even though they are not in order. My problem is when I try to loop through the first element my code appears to only be checking the first letter for a match and not continuing through the entire word.
function mutation(arr) {
var firstElement = arr[0].toLowerCase().split('');
var secondElement = arr[1].toLowerCase().split('');
for (var i = 0; i < firstElement.length; i++) {
return firstElement.indexOf(secondElement[i]) != -1;
}
}
mutation(["hello", "neo"]);
Any ideas?
Upvotes: 1
Views: 556
Reputation: 878
Don't split the first element -- treat it as a string and use the indexOf method on that with the single characters from the second element.
Update -- yes, like Depperm's code below, but throw a toLowerCase on firstElement.
Upvotes: 0
Reputation: 1552
it's checking only for the first letter because as it enters into the loop, the return statement gets executed, and hence the function terminates.
function mutation(arr) {
var firstElement = arr[0];
var secondElement = arr[1];
return secondElement.split('').every(function(elem) {return firstElement.indexOf(elem) != -1;} );
}
mutation(["Aliens", "lines"]);
Upvotes: 1
Reputation: 695
You can use map
and reduce
in order to save lines of code
function mutation(arr) {
var secondElement = arr[1].toLowerCase().split('');
var map = secondElement.map(function(a){return arr[0].indexOf(a) != -1});
return map.reduce(function(a,b) {return a&&b;}, true);
}
On second line you check if each letter from second word is in first word (and save true/false). Third line just accumulate those results into a final variable
Reference https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Array/reduce https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_objects/Array/map
Upvotes: 1
Reputation: 1696
There are two issues with the function:
Firstly, it is returning after checking only the first letter, as you say.
Secondly, you need to use the length of the second element as your loop termination condition, since that's the one whose letters you are intending to loop through.
You need something more like this:
function mutation(arr) {
var firstElement = arr[0].toLowerCase().split('');
var secondElement = arr[1].toLowerCase().split('');
for (var i = 0; i < secondElement.length; i++) {
if(firstElement.indexOf(secondElement[i]) === -1) {
return true;
}
}
return false;
}
You might also want to handle some obvious boundary cases, like null/undefined/empty strings, but I've left that out for clarity.
Upvotes: 1
Reputation: 10756
Using what Paul Kienitz mentioned, also change your loop to loop through the second element to check each character in the first element to see if it finds that character in the first element.
function mutation(arr) {
var firstElement = arr[0];
var secondElement = arr[1].toLowerCase().split('');
var match=true;
var temp;
for (var i = 0; i < secondElement.length; i++) {
temp = firstElement.indexOf(secondElement[i]);
if(temp==-1)
match=false;
}
return match;
}
mutation(["hello", "neo"]);
Upvotes: 0