Wednesday, July 24, 2024
HomeJavaScriptUtilizing Labeled Loops In JavaScript

Utilizing Labeled Loops In JavaScript


Earlier this week, I checked out utilizing labeled loops in ColdFusion. Labeled loops will let you break and proceed an outer loop from inside the context of an inside loop by explicitly naming your loop statements. I had by no means used this performance in ColdFusion earlier than; and, it seems, the identical performance is out there in JavaScript. As such, I needed to strive it out within the browser as effectively.

Run this demo in my JavaScript Demos undertaking on GitHub.

View this code in my JavaScript Demos undertaking on GitHub.

Fashionable ColdFusion seems more-or-less like trendy JavaScript, particularly if you take-out the type-checking (simply another excuse why ColdFusion is such an incredible language). The truth is, I can pretty-much copy the “Fuzzy Matching” demo from my earlier put up and paste it in a JavaScript runtime with solely slight modification.

Within the following exploration, we’ll try to “fuzzy match” search textual content in opposition to a given goal textual content. To do that, we’ll loop over the search textual content and – for every character within the search textual content – loop over the goal textual content characters, looking for a sequential match. This offers us the chance to discover a nested loop context.

ASIDE: This algorithm might be refactored to work with out labeled loops, as Emre Yucel identified in my earlier put up. However, the purpose of this demo is to take a look at labeled loops. So, thanks for simply going with the movement.

When an identical character is discovered inside the inside loop, we’ll both proceed or break out of the outer loop – utilizing the outer loop label – based mostly on the present situation:

<!doctype html>
<html lang="en">
<physique>

	<h1>
		Utilizing Labeled Loops In JavaScript
	</h1>

	<script kind="textual content/javascript">

		// Fuzzy matches.
		console.log( isFuzzyMatch( "horse", "s" ) );
		console.log( isFuzzyMatch( "horse", "hs" ) );
		console.log( isFuzzyMatch( "horse", "horse" ) );

		// No matches.
		console.log( isFuzzyMatch( "horse", "horses" ) );
		console.log( isFuzzyMatch( "horse", "check" ) );
		console.log( isFuzzyMatch( "horse", "" ) );

		// --------------------------------------------------------------------------- //
		// --------------------------------------------------------------------------- //

		/**
		* I decide if the given goal textual content is a fuzzy-match for the given search textual content.
		*/
		perform isFuzzyMatch( targetValue, searchValue ) {

			var searchChars = [ ...searchValue ];
			var targetChars = [ ...targetValue ];
			var matchFound = false;
			var s, t;

			searchLoop:
			whereas ( s = searchChars.shift() ) {

				targetLoop:
				whereas ( t = targetChars.shift() ) {

					// We discovered an identical CHARACTER within the goal string.
					if ( s == t ) {

						if ( ! searchChars.size ) {

							matchFound = true;
							// If we have run out of search-characters to devour, it means
							// that the whole lot of the search key phrase was situated (in
							// elements) inside the goal textual content. In different phrases, we HAVE a
							// fuzzy-match. Yay! At this level, there may be nothing left to
							// search and we will get away of BOTH the INNER and OUTER
							// loops.
							break searchLoop;

						}

						// If we've got extra search characters to devour, transfer onto the
						// NEXT ITERATION of the OUTER loop and the subsequent search character.
						proceed searchLoop;

					}

				}

				// If we have totally consumed the goal characters, there isn't any sense in
				// persevering with to devour the search characters - we is not going to discover a match.
				break;

			}

			return( matchFound );

		}

	</script>

</physique>
</html>

As you may see, every of the whereas-loops above is preceded by a label: assertion. The outer loop is labeled as searchLoop:, which permits my inside loop to make use of break searchLoop and proceed searchLoop statements. These will get away of or proceed onto the subsequent iteration of the outer loop, respectively.

FORMATTING NOTE: You’ll be able to, technically, embody the loop label on the identical line because the loop itself. Nevertheless, this must be prevented because it seems an excessive amount of like Object-key project; and, is extra more likely to trigger confusion.

Now, if we run this web page within the browser we get the next console output:

true   // isFuzzyMatch( "horse", "s" )
true   // isFuzzyMatch( "horse", "hs" )
true   // isFuzzyMatch( "horse", "horse" )

false  // isFuzzyMatch( "horse", "horses" )
false  // isFuzzyMatch( "horse", "check" )
false  // isFuzzyMatch( "horse", "" )

As you may see, by utilizing labeled loops, we have been in a position to exert management movement on the outer loop from inside the context of the inside loop. My demo makes use of whereas-loops however this additionally works for for-loops. The truth is, you may even get away of a block – however that is simply plain bananas!

Wish to use code from this put up?
Try the license.



RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments