# Loop /return/ is not possible

**URL:** <https://forum.freecodecamp.org/t/loop-return-is-not-possible/133058>\
**Category:** General\
**Tags:** loop, return\
**Created:** [June 29, 2017, 11:33pm UTC](https://forum.freecodecamp.org/t/loop-return-is-not-possible/133058 "2017-06-29T23:33:28Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![eldarmammadov](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/eldarmammadov/32/62582_2.png) [@eldarmammadov](https://forum.freecodecamp.org/u/eldarmammadov)\
**Post date:** [June 29, 2017, 11:33pm UTC](https://forum.freecodecamp.org/t/loop-return-is-not-possible/133058/1 "2017-06-29T23:33:28Z")

</div>

```auto
javascript
function mutation(arr) {
      var a = arr[0];
      var b = arr[1];
  	// for (var i = 0; i < b.length; i++) {
  	// console.log(b[i]);
  	// console.log(a.indexOf(b[i],0));
  	// }
  	for (var i = 0; i < b.length; i++) {
  		//console.log(b[i]);
  		 if (a.indexOf(b[i],0)) {
  		 	//console.log(a.indexOf(b[i],0));
  		   return true;          
         } 
         //console.log(a.indexOf(b[i],0));  
         return false;
  	}
}

//mutation(["hello", "hey"]);
mutation(["floor", "for"]);
//mutation(["voodoo", "no"]);

```

Guys, hi; how to loop here if return statement just stops the loop in first

---

<div class="post-metadata">

**Author:** ![kevinSmith](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kevinsmith/32/72934_2.png) [@kevinSmith](https://forum.freecodecamp.org/u/kevinSmith)\
**Post date:** [June 30, 2017, 12:05am UTC](https://forum.freecodecamp.org/t/loop-return-is-not-possible/133058/2 "2017-06-30T00:05:48Z")

</div>

That is what a _return_ is supposed to do. If you don’t want it to exit the function, then you shouldn’t be using _return_.

But _return_ is good here, I just think that you’re using it wrong.

In my _for_ loop, I checked if that character was in the other strings and if it wasn’t then I knew I could _return false_ - there was no need to keep checking as I’d found a char that wasn’t there.

**_Outside_** of the _for_ loop I had a _return true_ - if I’d made it out of the _for_ loop then I’d know what all of the chars of the second string were in the first.

You also need to account for uppercase and lowercase. I just converted each string to lowercase.

---

<div class="post-metadata">

**Author:** ![mattsouthward](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/mattsouthward/32/50855_2.png) [@mattsouthward](https://forum.freecodecamp.org/u/mattsouthward)\
**Post date:** [June 30, 2017, 12:06am UTC](https://forum.freecodecamp.org/t/loop-return-is-not-possible/133058/3 "2017-06-30T00:06:57Z")

</div>

You need to reverse that logic. If this letter in b _isn’t_ in a, then return false, then if you get all the way through that for loop, return true.

---

<div class="post-metadata">

**Author:** ![Kyrizu](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/kyrizu/32/67670_2.png) [@Kyrizu](https://forum.freecodecamp.org/u/Kyrizu)\
**Post date:** [June 30, 2017, 7:26am UTC](https://forum.freecodecamp.org/t/loop-return-is-not-possible/133058/4 "2017-06-30T07:26:47Z")

</div>

if you want the loop to only end once the if statement condition is true then move that (return false) line outside of the for loop

---

<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 16, 2021, 5:51pm UTC](https://forum.freecodecamp.org/t/loop-return-is-not-possible/133058/5 "2021-01-16T17:51:53Z")

</div>

Closed on January 16, 2021, 5:51pm UTC


