Edin Tin wrote:
I figured if I add under it'll work because as soon as it found one match then it'll turn to true and break out the loop but I don't think this is the best way to do it right?
I'm not too picky about putting
break in the middle of a loop as long as the enclosing method is small and focused, as is your method. There's really no need to set exists = false in the loop. Here's the story, in plain English:
Which translates directly to:
So what if I exited the for-loop before it could go through the whole list, ¯\_(ツ)_/¯, I didn't really need to continue once I found something so why bother? It may be a personal preference but I actually like that better than
while (!found && i < array.length) -- it has one less boolean expression to look at and it's clearer that we're trying to go through the list.
Edit: If this were a method that had 100 lines of code and multiple places where you used
break and/or
return, then that's a different story. Then we'd have to have a little heart-to-heart talk about good coding practices.