Basic Algorithm Scripting: Mutations solution fails one case but passes all others

I fail to see why my solution fails this case: “mutation([“hello”, “hey”])` should return false” but passes every other one.

var container=arr[0].toLowerCase();
var contained=arr[1].toLowerCase();
for(var i = 0; i < container.length; i++){
    if(container.indexOf(contained[i]) >= 0){
        return true
    }
else{
    return false
    
}
}
}

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

Bear in mind, a return statement breaks out of all loops or ifs, and also terminates the function. You are returning based SOLELY on the first contained char, not the entire string.

1 Like

Thank you. But why does the official solution work though? Does it change if the return statement is false?

function mutation(arr) {
  var test = arr[1].toLowerCase();
  var target = arr[0].toLowerCase();
  for (var i=0;i<test.length;i++) {
    if (target.indexOf(test[i]) < 0)
      return false;
  }
  return true;
 }

The solution loops through the entire string looking for a false condition. If it finds one, it returns false and exits the function.

Only when the entire string has been tested and no false condition has been found will it return true.

2 Likes

So it completes the loop, regardless of the return?

In your version, the if and else both return, ending the loop. In that official one, there is no else so execution of the loop can continue.

1 Like

AAAAAAAH. So if I remove the else, the loop will continue as intended?

The for loop only runs to completion if there are no false conditions. Once a false condition has been found function returns false and exits.

Once you have found a false condition there is no need to test further - the two strings are not a match and it has been proven - return false right then and there.

But if you have found no false conditions the for loop runs to completion. All letters have been tested and no false condition was found so the only option is that it must be true.

1 Like

Thank you. So, in my version, it goes like this:
checks the first character, doesn´t return true, it goes to the else, returns false and escapes the function, right? If the else wasn´t there, the loop would go through all the way before returning, right?

Exactly right. What you said. ;D

1 Like

Thank you! I get it now!

Sorry, I don´t understand why this version doesn´t work. It´s basically the same as the solution:

function mutation(arr) {
  var test = arr[1].toLowerCase();
  var target = arr[0].toLowerCase();
  for (var i=0;i<test.length;i++) {
    if (target.indexOf(test[i]) >= 0){
      return true;
    }

  }
  return false;
 }

Your code is set up so that if any case is true then they are all assumed to be true. You need to be testing the opposite situation where if any one is false then the return value is false. And if none of them are false only then can you return true

 // three functions to prove that every item in arr is true
// one works and two do not

const arr = [true, true, true, true, false, true];

function areAllTrueGood(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === false) {
      return false;
    }
  }
 // if none false, then return true
  return true;
}

// false! - correct, all items are NOT true
console.log(areAllTrueGood(arr));  





function areAllTrueBad(arr) {
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === true) {
      return true;  // returns on first true element
    }
  }
  
  return false;
}

// true? - wrong, all items are NOT true
console.log(areAllTrueBad(arr));  





function areAllTrueReallyBad(arr){
  // forced to return something on first iteration
  // without checking any other elements
  for (let i = 0; i < arr.length; i++) {
    if (arr[i] === false) {
      return false;
    }else{
      return true;
    }
  }
}

// true? - wrong, all items are NOT true
console.log(areAllTrueReallyBad(arr));