Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Stop at the First Match

In this video, you searched your train ride playlist for the first song that starts with a given letter, and saw the loop stop as soon as it found one.

Key Points

  • A count has to visit every item, because any item it skips might be one more match.
  • A search needs only the first match, so it can stop as soon as it finds one. Later items can’t change which match came first.
  • A return inside the loop hands back the matching item and leaves the whole function on the spot, so the loop stops too. It works like a break that also hands back the item.
  • When nothing matches, the if check is false on every pass, the loop visits every item, and the return inside it never runs.
  • The not-found answer comes from a second return after the loop. Only once the loop has checked the last item can the function be sure that nothing matched. Here, that answer is undefined.
  • An empty list gets the same not-found answer, because the loop makes no passes at all.

Final Code

Language: JavaScript Filename: playlist.js
function firstStartingWith(songs, letter) {
  for (const song of songs) {
    if (song[0] === letter) {
      return song;
    }
  }
  return undefined;
}

const songs = [
  "Rainy Day Waltz", "Mountain Echo",
  "Last Train Home", "Midnight Ferry"
];
console.log(firstStartingWith(songs, "M"));

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 A little earlier, you counted the songs on a train ride playlist that start with the letter M. Here it is with the songs shuffled. Your count visited every song, and it found two.

00:11 Now you want to pick an opening song for the ride. Any song that starts with M will do, so you’ll take the first one in the list. For that job, a count does more work than it needs.

00:23 Once you’ve found one M song, there’s no reason to look at the rest.

00:28 Here’s a function that searches instead of counting.

00:33 It takes the songs and a letter, and it has the same for...of loop and the same if check you used for the count. The difference is inside the if block.

00:43 Where the count added one, this function returns the song itself.

00:49 A return leaves the whole function on the spot, so it stops the loop too. It’s like a break that also hands the song back. Below the loop sits a second return, in the same place your count returned its total.

01:03 Your playlist has two songs that start with M. The script calls the function with the letter M and logs what comes back. Which song gets logged? Your turn: pause the video and predict the song.

01:23 It’s Mountain Echo. Here’s why, one pass at a time.

01:31 On the first pass, song holds Rainy Day Waltz. It starts with R, so the if check is false, and the loop moves on. On the second pass, song holds Mountain Echo.

01:46 Its first character is M, so the check is true. The function returns Mountain Echo right away. And that’s the end of the search. The loop never gets to Last Train Home or Midnight Ferry.

02:00 Two passes were enough, out of four. Now change the letter in the call to Z. No song on your playlist starts with Z. What does the script log this time? Your turn: pause the video and predict the output.

02:22 It’s undefined, JavaScript’s way of saying there’s nothing here. This time, the if check is false on every pass. So the loop visits all four songs, and the return inside it never runs.

02:40 When the loop ends, JavaScript reaches the return after the loop, which hands back undefined as the answer.

02:48 Only after the last song can the function be sure that nothing matched. An empty list gets the same answer, because the loop makes no passes at all.

03:00 Why can a search stop early when a count can’t? A count needs every match, and any song you skip might be one more. A search needs only the first match, and later songs can’t change which one came first.

03:16 So here’s the rule. A search returns the first match from inside the loop, and gives its not-found answer after the loop. Next up is Totals, Extremes and Empty Lists.

03:29 It’s a lesson where you’ll total a list, find its largest value, and decide what each pattern should give back for an empty list.

Become a Member to join the conversation.