LearnFrenzy
Sign in Take a free test

Functions · Exercise 3 of 4

Functions Find the Error

  • 15 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Debug fifteen JavaScript function programs: SyntaxErrors from rest parameters, duplicate parameters, object-returning arrows and nameless declarations; runtime errors from hoisting, missing returns and endless recursion; and silent bugs from forgotten returns, || defaults and mutated arguments.

Question 1 Easy

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function report(...scores, name) {
  return `${name}: ${scores.join(", ")}`;
}
console.log(report(90, 85, "Asha"));
A It prints Asha: 90, 85
B It prints undefined: 90, 85, Asha
C TypeError: scores is not iterable
D SyntaxError: a rest parameter must be the last parameter – write report(name, ...scores)
Question page & discussion
Question 2 Hard

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function price(amount, amount = 100) {
  return amount;
}
console.log(price(50));
A It prints 50
B SyntaxError: duplicate parameter names are not allowed when a parameter has a default, even outside strict mode
C It prints 100
D It prints undefined
Question page & discussion
Question 3 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
const makeUser = (name) => { name: name, active: true };
console.log(makeUser("Asha"));
A It prints { name: 'Asha', active: true }
B It prints undefined
C SyntaxError: the braces are read as a function body, not an object – wrap it: (name) => ({ name, active: true })
D TypeError: makeUser is not a function
Question page & discussion
Question 4 Medium

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function (a, b) {
  return a + b;
}
console.log("ready");
A SyntaxError: a function statement needs a name (or must be an expression, e.g. assigned or wrapped in parentheses)
B It prints ready
C It defines an anonymous function and prints ready
D ReferenceError
Question page & discussion
Question 5 Hard

JavaScript refuses to run the code below. Which statement correctly describes the problem?

main.js
function connect(host = "localhost", port = 3306) {
  "use strict";
  return `${host}:${port}`;
}
console.log(connect());
A It prints localhost:3306
B It prints undefined:undefined
C ReferenceError: host is not defined
D SyntaxError: a function with default, rest or destructured parameters cannot contain a "use strict" directive
Question page & discussion
Question 6 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
console.log(formatPrice(499));
var formatPrice = function (p) {
  return "₹" + p.toFixed(2);
};
A It prints ₹499.00
B ReferenceError: formatPrice is not defined
C TypeError: formatPrice is not a function – the var is hoisted as undefined, the function is assigned later
D SyntaxError
Question page & discussion
Question 7 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
console.log(discount(1000));
const discount = (amount) => amount * 0.1;
A It prints 100
B TypeError: discount is not a function
C ReferenceError: discount cannot be accessed before initialization (TDZ)
D It prints undefined
Question page & discussion
Question 8 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function makeCounter() {
  let count = 0;
  () => ++count;
}
const next = makeCounter();
console.log(next());
A It prints 1
B It prints undefined
C ReferenceError: count is not defined
D TypeError: next is not a function – makeCounter creates the arrow function but never returns it
Question page & discussion
Question 9 Medium

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function countdown(n) {
  return countdown(n - 1);
}
countdown(5);
A It returns 0
B It loops forever without an error
C RangeError: Maximum call stack size exceeded – there is no base case to stop the recursion
D SyntaxError
Question page & discussion
Question 10 Easy

The code below stops with an uncaught error. Which statement correctly describes the problem?

main.js
function shout(message) {
  return message.toUpperCase() + "!";
}
console.log(shout("hello"));
console.log(shout());
A It prints HELLO! and UNDEFINED!
B It prints HELLO!, then TypeError: message is undefined in the second call – give it a default or validate it
C It prints HELLO! and !
D SyntaxError
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links