Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1function add(a, b) { a + b; } console.log(add(2, 3)); | AThe result is stored in a but never returned, so undefined is printed. Add return a; at the end. | |
| 2let result = showTotal(5, 4); | Bconsole.log only prints the value, it does not return it, so answer stores undefined. Use return n * n; instead. | |
| 3A function that gives a value back to the caller must use return | Cresult holds undefined when showTotal only uses console.log and has no return | |
| 4A function that only does a job, like showing a message, can leave out return | Dfunction greet(name) { console.log('Hi ' + name); } | |
| 5function square(n) { console.log(n * n); } let answer = square(4); | EPrints undefined, because add never uses return so it gives back nothing | |
| 6function area(w, h) { let a = w * h; } console.log(area(3, 2)); | Ffunction double(n) { return n * 2; } |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
undefined — The value a function gives back when it has no return statement. console.log only shows a value on screen, so a variable storing the result will hold undefined.function add(a, b) {
return a + b;
}function hi() { console.log("Hi"); }
let x = hi();
console.log(x === undefined);function sq(n) { return n * n; }
let r = sq(3);
console.log(r);function greet() { console.log("Hi"); }
let msg = greet();
console.log(msg);function sq(n) { n * n; }
let r = sq(4);
console.log(typeof r);