Mutations- About curly braces

There is another question like this, but it is a bit different. As a total beginner I had thought conditionals were based around if/else always.

Then I realized you could have a series of ifs w/o elses, if they are seperated (at least I think I’ve seen this).

My point? I have no idea why the braces in this are so important.

This does not fulfill the challenge-

function mutation(arr){
 var target = arr.splice(0,1);
 var test = arr;

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

But this does
> for(var i=0;i<test.length;i++){
> if (target.indexOf(test[i]) < 0) //missing brace HERE is only diff

   return false
 }
 return true

First- how does this even run? Normally a missing brace causes the code to fail, at least in my browsers and node.

Second- how does it work? Since this exact syntax is imperative, and this kind of thing has bitten me before, were can I find info on this?

Docs all refer to the basic syntax and never how to know about case like this?

if (condition) {
code
} else {
code

When is it possible to leave a brace off?

Not exactly sure I understand your questions. When you ask “First, how does this even run?”, which code section (you posted 2) are you referring to? When you ask “Second- how does it work?”, which code section are you referring to?

The section specifically is

if (target.indexOf(test[i]) < 0)
return false
}
return true

Since there is not brace after the if statement, and normally I always get an error in this case.

And since I put a brace, which is my normal practice, the answer was incorrect with said brace.

I only figured this all out by looking at the solution because I couldn’t pass

mutation(["hello", "hey"]) should return false.

I guess I thought braces were mandatory after conditionals.

How does it work- I meant, usually I thought U would see this

if (conditional){
code
} else {
code
}

or

[…]
if (conditional){
code
}
if(conditional){
code
}
[…]

But even the above one I am unsure of. When to use if after if, rather than an if / else.

OK, now that you added more info in your post, I think I understand what you are asking.

When you write:

if (condition)
  code
else 
  code

it is the same as writing:

if (condition) {
  code
} else {
  code
}

but ONLY if code is one statement.

JavaScript by default will treat the next single statement after the if statement as part of the true condition and treat the next single statement after the else as part of the else (or false in this instance) condition. It is a way to simply your code if you only need to do one thing after the if.

You can even do the following:

if (condition)  code
else  code

Again, the above assumes code only is one statement. As soon as you need more statements to execute, you have to include the curly brackets {}. Does this answer your question?

1 Like

It definitely helps!! I guess I just don’t understand why that single brace makes it fail?

So when you say one statement what do you mean exactly? Like one ifand one else only, as opposed to multiple if/elses?

And so in the example solution there is no else at all. How does that work?

*** Oh I just re-read and you pretty much answer this.
If there were more, then there would be braces needed. Got it.

This helps, especially as I haven’t been able to track down any solid documentation on the issue.

I think you are still missing some important things. for(var i=0;i<test.length;i++){ if (target.indexOf(test[i])< 0){ return false } return true }
and this:

 for(var i=0;i<test.length;i++){
  if (target.indexOf(test[i]) < 0) //missing brace HERE is only diff
   return false
 }
 return true

are very differen’t. So let me break this down with some comments.

for(var i=0;i<test.length;i++){
if (target.indexOf(test[i]) <0){
    return false;  
//Okay for 1 return ends the function, so as soon as this runs the function stops.
  } 
 return true; //Same problem here. I will only check the first letter and then return;
}
 for(var i=0;i<test.length;i++){
  if (target.indexOf(test[i]) < 0) //missing brace HERE is only diff
   return false; //This only returns if the "if" statment is true
 } //This is now the end of the for loop
 return true; //This only gets called after the for loop if the "if" statement is never true

The important thing here is that the reason the first one fails is because the return true statement is inside the for loop. That causes it to only run the check for the first letter. It’s not because of the if esle thing.
Now lets talk about you last post.[quote=“arssonist, post:5, topic:104377”]
So when you say one statement what do you mean exactly? Like one ifand one else only, as opposed to multiple if/elses?

And so in the example solution there is no else at all. How does that work?

*** Oh I just re-read and you pretty much answer this.
If there were more, then there would be braces needed. Got it.

This helps, especially as I haven’t been able to track down any solid documentation on the issue.
[/quote]

ok so its not the number of if statements, its the command that follows it. IE:

if( a===b ) a+=1; //This is fine
if( a===b) 
  a+=1; //this will be called if the above is true
  b+=1; //This is not part of the if statement and would get called regardless
//So if you wanted to do both commands you would need { }
if( a===b ){
 a+=1;
b+=1; //Now both will be called if the statement is true
}
//You could easly do multiple if statements in a row
if( a===b ) a+=1;
else b+=1;
if( b===c )b+=1;
if( c===d ){ d-=1; c+=1; }
else d+=1;

I hope that clears things up. Feel free to ask if you have more questions.

2 Likes