Mutations - curly braces positioning

Mutations - curly braces positioning
0

#1

I’m reviving this thread to ask about why the curly braces need to be removed on the for loop in this problem. Does anyone know the reasoning behind this? I’m guessing there is a scoping issue when blocking the for and if statements of my code. Originally, I had curly braces after the for loop and after the “if” condition. Code is below, and any insight would be helpful.

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

Mutations + Array.prototype.indexOf()
#2

As I remember it doesn’t has to be removed. Both ways should work. Here is the helpful link about {} curly braces https://stackoverflow.com/questions/4797286/are-curly-braces-necessary-in-one-line-statements-in-javascript

In this link Josh K. answered:

This is perfectly valid

if (cond) 
    alert("Condition met!")
else
    alert("Condition not met!")

However it is highly recommended that you always use braces because if you (or someone else) ever expands the statement it will be required.

This same practice follows in all C syntax style languages with bracing. C, C++, Java, even PHP all support one line statement without braces. You have to realize that you are only saving two characters and with some people’s bracing styles you aren’t even saving a line. I prefer a full brace style (like follows) so it tends to be a bit longer. The tradeoff is met very well with the fact you have extremely clear code readability.

if (cond) 
{
    alert("Condition met!")
}
else
{
    alert("Condition not met!")
}

#3

It is not a matter that they needed to be removed. It was simply a matter that they could be removed without changing the logic. @IlimaVIP explained it perfectly.


#4

Ok, now I’m pretty confused. I recreated my old code before I got rid of the curly braces, and it passes all except the [‘hello’, ‘hey’]. I ran mutation() through the chrome debugger and see that the values 0,1,-1 are console.logged for [‘hello’, ‘hey’], but the function is returning 0 for some reason. However, once the curly braces are removed, it runs perfectly. Old Code:

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

#5

I moved this discussion to a new topic, because it had nothing to do with the OP’s solution in the other thread.

Your code (you refer to as Old Code) above only makes it through one iteration of the for loop, because you either return true or false depending on how the if statement evaluates. Once any return statement is executed, the function is immediately exited and no further code in the function runs.

In the test case of mutation([‘hello’, ‘hey’]), in the first iteration i = 0, so array2[0] is ‘h’ and array1.indexOf(‘h’) evaluates to 0. Since 0 is not equal to -1, you return true and the function exits.

The solution above without curly braces is the equivalent of writing the following code. You will notice I made indentations to make it more clear where what lines go with the if vs. the for loop.

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

It works, because your for loop makes it all the way through without returning false (when array1.indexOf(array2[i]) === -1), that means all the letters in array2 were present in array1, so it is fine to return true. It would have never made it to the return true statement, if a letter wouild not have been present in array1, because of the return false.


#6

Thanks to you and llima for taking the time to respond. Your solution with curly braces was very helpful, and I see where I went wrong with my ‘old’ code.