# Mutations- About curly braces

**URL:** <https://forum.freecodecamp.org/t/mutations-about-curly-braces/104377>\
**Category:** General\
**Created:** [April 9, 2017, 3:01am UTC](https://forum.freecodecamp.org/t/mutations-about-curly-braces/104377 "2017-04-09T03:01:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![arssonist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arssonist/32/73145_2.png) [@arssonist](https://forum.freecodecamp.org/u/arssonist)\
**Post date:** [April 9, 2017, 3:01am UTC](https://forum.freecodecamp.org/t/mutations-about-curly-braces/104377/1 "2017-04-09T03:01:07Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![arssonist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arssonist/32/73145_2.png) [@arssonist](https://forum.freecodecamp.org/u/arssonist)\
**Post date:** [April 9, 2017, 3:08am UTC](https://forum.freecodecamp.org/t/mutations-about-curly-braces/104377/3 "2017-04-09T03:08:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![arssonist](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arssonist/32/73145_2.png) [@arssonist](https://forum.freecodecamp.org/u/arssonist)\
**Post date:** [April 9, 2017, 3:26am UTC](https://forum.freecodecamp.org/t/mutations-about-curly-braces/104377/5 "2017-04-09T03:26:40Z")

</div>

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 `if`and 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.

---

<div class="post-metadata">

**Author:** ![Josh5231](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/josh5231/32/3269_2.png) [@Josh5231](https://forum.freecodecamp.org/u/Josh5231)\
**Post date:** [April 9, 2017, 4:28am UTC](https://forum.freecodecamp.org/t/mutations-about-curly-braces/104377/6 "2017-04-09T04:28:51Z")

</div>

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:

```auto
 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.

```auto
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;
}
```

```auto
 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:

```auto
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.

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [January 17, 2021, 8:51pm UTC](https://forum.freecodecamp.org/t/mutations-about-curly-braces/104377/7 "2021-01-17T20:51:53Z")

</div>

Closed on January 17, 2021, 8:51pm UTC


