Problem overview
The starting program prints the same three lines three times, by repeating nine console.log calls. The task is to get the same output from a function named printMessages, which you define once and call three times.
The expected output is:
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Hello AlgoCademy!
Coding is amazing!
Functions are so useful!
Approach
Look for what repeats. The group of three lines appears three times, unchanged. Anything that repeats unchanged can move into a function: write the group once as the function's body, then replace each copy with a call.
The function needs no input, because every copy is identical. So it takes no parameters, and it returns nothing, because it only prints.
Step by step
- Define the function. Write the keyword
function, the nameprintMessages, empty parentheses and the return type: void, then an opening brace. The empty parentheses say the function takes no arguments. - Fill the body. Put the three
console.logcalls between the braces, in the original order. This is the only place in the program where those strings appear. - Call it three times. Under the second comment, write
printMessages();on three separate lines. Each call runs the body once, so three calls print the group three times.
The solution in TypeScript
// Define and implement the function printMessages here:
function printMessages(): void {
console.log("Hello AlgoCademy!");
console.log("Coding is amazing!");
console.log("Functions are so useful!");
}
// Call printMessages 3 times here:
printMessages();
printMessages();
printMessages();
Some details worth knowing:
- Defining does not run. If you wrote only the definition, nothing would be printed. The calls are what run the body.
- Parentheses make a call.
printMessages();runs the function. WritingprintMessages;only refers to the function and does nothing visible, even when the compiler accepts it. voidis a type annotation. It tells the compiler the function returns no useful value. It is removed when the code is compiled to JavaScript, so it has no effect at run time.- A function can be called before its definition appears. TypeScript, like JavaScript, makes function declarations available throughout their scope. Putting the definition first, as here, is still the clearest order to read.
Why this is better
The new program has three lines of text instead of nine. More importantly, the text lives in one place. To change "Coding is amazing!" you edit a single line, not three. To print the group five times, you add two calls instead of six lines. The three identical calls are still repetition, and loops, a later topic, remove that.
Edge cases and common mistakes
- Calling the function too few or too many times. The output must contain the group exactly three times, so make three calls.
- Keeping the original
console.loglines outside the function. That prints them in addition to what the function prints, so the output has extra lines. - Misspelling the name.
printMessagesis case-sensitive. A call toprintmessages()reports an error that the name cannot be found. - Changing the text. The strings must match exactly, including capital letters and the exclamation marks.
Testing
Run the program and count the lines: it should print 9, with the sequence repeating three times. Then try a fourth call and confirm that the group appears four times; that shows the function body is being reused. Remove the extra call afterwards.