Mutation - Understand a piece of code

What’s difference between these pieces of code

1-

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

2-

 var str0 = arr[0].toLowerCase();
  var str1 = arr[1].toLowerCase();
  for (var x =0 ; x <str1.length ; x++){
    if (str0.indexOf(str1[x]) < 0){
      return false ;
    }else {
      return true ;
    }
  }

In the first code snippet, the program iterates through the entire array before returning true, and returning false if any of tests are failed.
In the second snippet, however, the program only iterates through the first value, and if that passes the test, it returns true, and doesn’t test the rest of the items in the array.

2 Likes

Yes, as hijer says, the second example will never make it past the first iteration. Once it hits that if statement, it’s going to return something. It will never make it for a second pass. Because of that, the second code sample is functionally equivalent to:

 var str0 = arr[0].toLowerCase();
 var str1 = arr[1].toLowerCase();
 
 if (str0.indexOf(str1[0]) < 0){
   return false ;
  } else {
    return true ;
  }
}

I’m guessing that’s not what you want.

As a side note, anytime you have an if/else that just returns true/false, you are doing more writing that you need. This would do the same thing.

 var str0 = arr[0].toLowerCase();
 var str1 = arr[1].toLowerCase();
 
return str0.indexOf(str1[0])>=0;

But that’s a moot point since that isn’t what you want.

2 Likes

Sorry, I can’t understand what you mean! Can you kindly give more explanation?

That’s right, That’s not what I want. I want it to iterate for all the str0 characters. What should it be ?

Essentially what is happening in the first snippet is that it runs your desired test on the first character in the string. If that passes the test, it essentially does nothing, and moves on to the next character in the string die to the for loop. However, if that particular character on the string fails the test, it returns false, and then exits the function, as it’s done its job. If it has run through every character and none failed the test,then it returns true.

I’m the second snippet, it looks at the first character and decides whether or not it passes the test. It then returns true or false, depending on whether or not that first character passed the test.

A return statement is essentially an “exit function here” statement. For instance, if you had

function print stuff() {
  console.log("Stuff");
  return true;
  console.log("More stuff");
}

Here the output would only be “stuff”. Once the function sees the return statement, it returns whatever it needs to and exits the function. You could write 100 or 1000 lines of code below the return statement in that function, but they would never be executed.

So you want what the first code snippet. That should work for the task (I haven’t looked to deeply at it, and since I’m on mobile at the moment, I can’t test it.) That should solve the challenge.

If you need any more clarification, don’t hesitate to ask! :slight_smile: Hope this helped!

1 Like

Great, It’s more clear now but still have a question: What makes the code executed after only the first letter and prevents it from continuing to the rest of the string ? I understand that return is finishing the function but it exists at both piece of coding

This is the same one I was working on last night. You need to remove the else and put the return true outside of the for loop. That way, if the for loop never triggers a false, it will return true AFTER the for loop has finished.

1 Like

In the second snippet that you have, what is returned is based solely on the first letter of the string. It returns true or false based on that first letter. Since it returns a value, the subsequent iterations in the for loop are never actually executed. Since they are never executed, it doesn’t return the values you are expecting.

Also, as a side note, most programmers use “i” as the variable in a for loop. If there is another for loop inside that for loop, they use “j” and so on through the alphabet. While what you are doing isn’t in any way wrong, some programmers may be slightly thrown off by it. Feel free to use what you prefer, but it’s just something to be aware of.

Hope this helped! :slight_smile:

1 Like

I think this may make sense for me. Thank you

I think I got it and that what I think of how the code work (( please correct me if I am wrong ))

In the second piece of code ,that else is restricting the For Loop from checking the entire array because I gave it an order if the IF condition doesn’t apply so it will execute after the first letter. The main idea is to give a true if all the strings apply and a false if one string doesn’t apply. So, the first piece is right because it will work as this way : check for the IF condition applied for each element and when it applied it returns false, otherwise it will continue until it finish without executing the code inside the if and will return True.

Sounds like you’ve got it! :smiley:

1 Like

Thanks to you and other friends. :wink: :smile:

Now that you better understand the use of an early return from inside a for loop here’s an alternative version using a single return statement

This version is more structured because the guard of this for loop - the condition that determines whether the loop body is entered - is a complete boolean expression of the continuation or termination of the loop - this makes it easier to reason about the correctness of the loop and thus of the function

function mutation(arr) {
  const str1=arr[0].toLowerCase()
  const str2=arr[1].toLowerCase()
  // boolean all_str2_letters_in_str1 is return value of function
  let all_str2_letters_in_str1=true
  for(let i=0; all_str2_letters_in_str1 && i < str2.length; ++i) {
    const c=str2[i]
    all_str2_letters_in_str1 = str1.indexOf(c) >= 0
  }
  return all_str2_letters_in_str1
}
1 Like

Thank you for helping but, What is LET ?

let and const are new ways to declare javascript variables

1 Like

Thank you I’ll check it out :slight_smile: