Problem overview
The quiz shows a function and asks what happens when the code is run:
function sayMyName() {
console.log("Heisenberg!");
}
The four options are: it prints Heisenberg!, it produces errors, it prints nothing, or it prints sayMyName. The answer goes into a variable named answer as a one-letter string.
Approach: ask what actually runs
The way to answer any "what does this print" question is to find the lines that execute, not the lines that merely exist. Here the whole program is one function declaration. Before picking an option, ask two things: is the code valid, and does anything call the function?
The code is valid: it has a header, parentheses and a body in curly braces. Nothing calls the function, though. There is no sayMyName() anywhere in the file.
Step by step
- Read the header.
function sayMyName()declares a function. TypeScript now knows the name, but no code has run. - Read the body. The
console.logline sits between the braces, so it is part of the function and is stored with it, not executed. - Look for a call. A call is the name followed by parentheses. There is none, so the body never runs.
- Match the options. Option A needs the print to run. Option B needs an error, and there isn't one. Option D would need the program to print the function's name, which would take something like
console.log("sayMyName")or a printed function reference. Option C, nothing, is what remains. - Assign the letter. The answer is
"C", written aslet answer: string = "C";.
The solution in TypeScript
let answer: string = "C";
/* Explanation:
Remember, the code inside a function will not be executed unless we call that function!
In our code, we only define the function sayMyName(), without calling it afterwards, so the console.log() won't be executed and nothing will be printed to the console.
*/
A few details about it:
- The annotation checks nothing at run time.
: stringtells the compiler the variable holds text. It is removed when the code is compiled, so the value is simply the string"C". - The letter is text. It needs quotes. Writing
Cwithout them would refer to a variable namedC. - The comment is ignored. A block comment starts with
/*and ends with*/, and may span several lines. It doesn't affect the program.
Why not the other options
- Printing
Heisenberg!. This would be true if the file also containedsayMyName();. The definition alone never triggers the body. - Errors. The function is valid TypeScript. An uncalled function is not an error.
- Printing
sayMyName. A function's name is never printed just because it was declared. Even writing the name without parentheses would not print it by itself.
Edge cases and common mistakes
- Confusing definition with execution. Seeing a
console.login the code makes many learners assume it prints. What matters is whether that line is reached, and a function body is reached only through a call. - Thinking the file is "empty" and so an error. A program that does nothing visible is still a valid program.
- Adding a call changes the answer. With
sayMyName();on a new line, the output would beHeisenberg!, which is option A. With two calls, it would print twice. - The call needs parentheses. A line that only says
sayMyName;mentions the function without calling it, and still prints nothing.
Testing your understanding
- The function plus one line
sayMyName();printsHeisenberg!once. - The function plus two such lines prints it twice.
- The function alone prints nothing.