Stuck on basic algorithm mutation but close?

Stuck on basic algorithm mutation but close?


Tell us what’s happening:
Hi fellow campers!
I feel I am very close to solving this but I can’t figure out how to return false the three other conditions of :

mutation([“hello”, “hey”]) should return false.
mutation([“hello”, “neo”]) should return false.
mutation([“voodoo”, “no”]) should return false.

any help without giving the answer so I can learn and understand? Thank you in advance!

Your code so far

function mutation(arr) {
  var newArr1 = arr[0].toLowerCase().split("");
  var newArr2 = arr[1].toLowerCase().split("");
  for (var i = 0; i < newArr1.length; i++) {
    for (var j = 0; j < newArr2.length; j++) {
      if (newArr1.indexOf(newArr1, 0) === newArr2.indexOf(newArr2, 0)) {
        return true;
      } else {
        return false;

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36.

Link to the challenge:


You have two main issues with your current logic, you are going to return true or false in the very first iteration of the inner for loop. Once a return statement is executed, it will return the value and exit the function, so it will not continue iterating through the for loop.

The other issue, which is causing your code to always return true is your use of the indexOf function.

The following:

newArr1.indexOf(newArr1, 0) 

will return -1, because you are attempting to find an index in the newArr1 array with your search value (first parameter) being the newArr1 array itself. The search value should not be an array, it should be string value, because you are looking for letters. You probably should review the indexOf function before proceeding.


Thank you for taking the time to answer i will follow your advice and try to understand the indexOf function better. One thin that i find challenging is how to access the letters of the array properly use indexOf(). Thank you though :smile:


Let’s say you have the following array defined:

var myArray = [1,5, 10,15, 20, 25];

You would use the indexOf function to check whether or not a specific value is in myArray. If I want to check if the value 20 is in myArray, then I could write:

if (myArray.indexOf(20) >= 0) {
  console.log("Found 20");
else {
  console.log("Did not find 20");

Remember that indexOf will return the index of the first element in the array (if it exists) which matches the value passed into the function’s first parameter. If the value can not be found, then it returns -1. That is why I check for values >= 0. In the example above, 20 is the 5th element in the array, so the index is 4. 4 is greater than or equal to 0, so the code above displays “Found 20” to the console.