Start learning free

LESSON 111 OF 348 · FOR LOOPS - LOOPING OVER ARRAYS

For Loop Execution Flow in TypeScript: Fixing Buggy Code II

Practice Read the full explanation

Inside the code editor we've tried to write a program that should print:

Hey, Andy
Hey, Mike
Hey, Mary
Let's start the class!

but it seems like we made some mistakes because when we run our code, it produces this output:

Hey, Andy
Let's start the class!
Hey, Mike
Let's start the class!
Hey, Mary
Let's start the class!

Assignment:

Your task is to fix our code such that it will print the desired output.

STEP 1 OF 1
    Ctrl + Enter to check

    TYPESCRIPT · FOR LOOPS - LOOPING OVER ARRAYS

    Buggy Code For Loops Execution Flow 2 in TypeScript, explained

    What the code is supposed to do

    The program greets three students and then announces the start of the class. The expected output has the three greetings first and the announcement once, at the end:

    Hey, Andy
    Hey, Mike
    Hey, Mary
    Let's start the class!
    

    The code you start with produces the announcement after every greeting instead:

    const students: string[] = ["Andy", "Mike", "Mary"];
    
    for (const student of students) {
        console.log("Hey, " + student);
        console.log("Let's start the class!");
    }
    
    Hey, Andy
    Let's start the class!
    Hey, Mike
    Let's start the class!
    Hey, Mary
    Let's start the class!
    

    What goes wrong

    This is the mirror image of putting a once-only line before a loop. The announcement sits between the loop's braces, so it is part of the body, and the body runs once for each of the three students. Each pass prints a greeting and then the announcement, which produces the alternating output.

    It helps to ask of each line: how many times should this happen? The greeting depends on the current student, so it happens three times. The announcement is about the whole class, and it should happen one time, when all the greetings are done. A line that should run once, after the work of the loop is finished, belongs after the closing brace.

    Fixing it step by step

    1. Keep the greeting in the body. It uses student, so it must stay inside the loop.
    2. Close the loop right after the greeting. Move the closing brace up, so that the body holds one statement.
    3. Put the announcement below the closing brace. It then runs once, as soon as the loop has handled the last student.

    The fixed code in TypeScript

    const students: string[] = ["Andy", "Mike", "Mary"];
    
    for (const student of students) {
        console.log("Hey, " + student);
    }
    
    console.log("Let's start the class!");
    

    This prints the four expected lines:

    Hey, Andy
    Hey, Mike
    Hey, Mary
    Let's start the class!
    

    A few notes on the code itself:

    • The blank line is only for readers. TypeScript ignores it. What separates the loop from the final statement is the closing brace.
    • Indentation shows structure, braces create it. Writing the announcement without indentation but before the closing brace would still keep it inside the loop. Move the brace, not just the whitespace.
    • const is fine for the loop variable. Each pass gives student a new value without reassigning one variable, so no error occurs.

    Tracing the fixed program

    Following the flow by hand is the best way to be sure of a fix. After the array is created, the loop starts:

    1. student is "Andy": the body prints Hey, Andy, and the loop goes back to the header.
    2. student is "Mike": it prints Hey, Mike.
    3. student is "Mary": it prints Hey, Mary.
    4. The array has no more elements, so the loop ends and the flow continues with the next line after the closing brace, which prints the announcement.

    With the original code, the body in steps 1 to 3 also contained the announcement, which is why it showed up three times.

    Common mistakes

    • Only removing the indentation. If you un-indent the announcement but leave the closing brace below it, the line is still in the body, and the output does not change.
    • Moving the announcement above the loop. It would print once, but first, and the class would start before anyone is greeted.
    • Using student after the loop. The variable exists only inside the loop. A line after the closing brace that mentions student fails with a ReferenceError.
    • Removing the announcement by accident. After moving things around, check that the last line is still there.

    Checking your fix

    Run the code in the editor above. The announcement should be the last line of the output and appear only once. Counting how many times each line appears is a quick check: three greetings, one announcement.

    Keep going with TypeScriptAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free