Mutations algorithm

Mutations algorithm
0

#1

Hi guys! After struggling for a LONG time on this challenge, I caved and checked the hint and found out that the reason my code wasn’t working was because I was using curly brackets after the if statement, like so:

if (word1.indexOf(word2[i]) === -1) { <------*

return false;

}
return true;

}

On the hint I saw that there was no curly brackets and as soon as I deleted the one after the if statement, my code worked…

My question is why do we not use curly brackets after the if statement here, because up until now I thought that an if statement has to be followed by a curly bracket?

Correct code (for context):

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


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

Link to the challenge:


#2

You don’t need to use curly braces if your if statement only needs to run one line of code. When there are multiple lines of code below that if statement, you will have to use curly braces.

Code below is valid

if(true)
  return 'Good'

Code below is invalid (it will only run the first line of code)

if(true)
  callFunction();
  return 'Not good'

#3

Aaah I see, Thank you so much!