0
0
Javascriptprogramming~30 mins

Labeled break and continue in Javascript - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Labeled break and continue in JavaScript
📖 Scenario: Imagine you are managing a small warehouse. You have a list of shelves, and each shelf has boxes with different items. You want to find a specific item quickly and also skip certain shelves based on a condition.
🎯 Goal: You will write a program that uses labeled break and continue statements to control loops effectively. This will help you stop searching when you find the item or skip shelves that are empty.
📋 What You'll Learn
Create a nested array called shelves with 3 shelves, each shelf is an array of items (strings).
Create a variable called targetItem with the value 'apple'.
Use a labeled outer loop called searchShelves to go through each shelf.
Inside the outer loop, use an inner loop to go through each item in the shelf.
Use continue searchShelves to skip the shelf if it is empty.
Use break searchShelves to stop all searching when targetItem is found.
Print 'Found apple!' when the item is found.
💡 Why This Matters
🌍 Real World
In real warehouses or stores, quickly finding items and skipping empty shelves saves time and effort.
💼 Career
Understanding labeled break and continue helps in writing efficient loops in software development, especially when dealing with nested data or complex conditions.
Progress0 / 4 steps
1
Create the shelves data
Create a variable called shelves that is an array with these exact arrays inside: ['banana', 'orange'], [], and ['apple', 'grape'].
Javascript
Need a hint?

Remember, shelves is an array of arrays. The second shelf is empty.

2
Set the target item
Create a variable called targetItem and set it to the string 'apple'.
Javascript
Need a hint?

Use const targetItem = 'apple'; to create the variable.

3
Use labeled loops with break and continue
Write a labeled loop called searchShelves that loops over shelves with variable i. Inside it, if the shelf is empty (length 0), use continue searchShelves to skip it. Then loop over the items with variable j. If the item equals targetItem, use break searchShelves to stop all loops.
Javascript
Need a hint?

Use searchShelves: before the outer loop. Use continue searchShelves; to skip empty shelves. Use break searchShelves; to stop when found.

4
Print when the item is found
After the loops, write console.log('Found apple!'); to print the message when the targetItem is found.
Javascript
Need a hint?

Use console.log('Found apple!'); after the loops to show the message.