Mutations v96765

Mutations v96765
0.0 0

#1

Tell us what’s happening:
Can’t seem to get all the tests to pass. Anyone got any ideas?

Your code so far

function mutation(arr) {
  var b;
  var newArr;
  newArr = arr.map(function (arr) { //lowercases and splits word into letters
    return arr.toLowerCase().split('');
});
 b = newArr[0].indexOf(newArr[1]);  //compare letters in array 1 to array 0?
return b;
}


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



Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 10.0; rv:59.0) Gecko/20100101 Firefox/59.0.

Link to the challenge:


#2

You need to read the challenge instructions carefully. You are not supposed to find the exact string (the second element of arr) somewhere in the first string (1st element of arr). You are supposed to make sure that each letter in the string (second element) is in the first regardless of the order. For example, all of the following should return true:

mutation(["elephant", "eel"]); // true
mutation(["elephant", "tap"]); // true
mutation("elephant", "pat"]); // true
mutation("elephant", "plate"]); // true

#3

See updated code, I split the word into letters but can’t seem to get the letters to compare to each other. Am I getting closer?


#4

Now, you are assigning the result of the indexOf function, which is going to be either a -1 if the ordered letters is not found or a value representing the index where the ordered letters are found. You are still not understanding that you need to iterate through each letter of arr’s second element and test if each is found.

I will give you a hint. There is no reason to create a separate array for the letters. You can iterate through a string just as easy. Also, I recommend naming your variables so that you describe what is stored in each. See below:

function mutation(arr) {
  var lettersToCheck = arr[0].toLowerCase();
  var lettersToFind = arr[1].toLowerCase();
  for (var i=0; i < lettersToFind.length; i++) {
    var letter = lettersToFind[i];  // letter contains the current letter being iterated over in lettersToFind
    // This is where you can use the indexOf to check if letter is in lettersToCheck
    // Use an if statement with the indexOf.  When you find a letter not in lettersToCheck, you return false
  }
  return ??? // Replace ??? with a value which means that all the letters where found in lettersToCheck, because you never found a case where they were not
}