Loop /return/ is not possible

Loop /return/ is not possible
0.0 0

#1
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


#2

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.


#3

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.


#4

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