LearnFrenzy
Sign in Take a free test

JavaScript Basics · Exercise 3 of 4

JavaScript Basics Find the Error

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

Debug fifteen JavaScript programs: some are rejected before they run (SyntaxError), some stop with a ReferenceError or TypeError, and some run but give the wrong answer. Choose the option that names the real problem.

Question 11 Medium

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

main.js
"use strict";
function addBonus(salary) {
  total = salary + 5000;
  return total;
}
console.log(addBonus(30000));
A It prints 35000
B ReferenceError: total is not defined – strict mode does not create globals; declare it with const
C SyntaxError: "use strict" must be inside the function
D TypeError: total is read-only
Question page & discussion
Question 12 Medium

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

main.js
const pi = 3.14;
console.log("Area:", pi * 2 * 2);
pi = 3.14159;
console.log("Area:", pi * 2 * 2);
A SyntaxError before anything is printed
B It prints both areas
C It prints the first area, then TypeError: Assignment to constant variable
D ReferenceError: pi is not defined
Question page & discussion
Question 13 Easy

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
const maths = "45";
const science = "38";
console.log("Total marks:", maths + science);
A console.log cannot print two values
B The variables should be declared with let
C + cannot be used on strings
D The marks are strings, so + joins them ("4538"); convert with Number() before adding
Question page & discussion
Question 14 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
let marks = 35;
if (marks = 100) {
  console.log("Full marks!");
} else {
  console.log("Keep practising");
}
A if conditions must be booleans, so this is a SyntaxError
B marks must be declared with const
C marks = 100 assigns 100 (truthy) instead of comparing, so the if branch always runs; use ===
D The else branch is missing braces
Question page & discussion
Question 15 Medium

The code below runs without an error, but its result is wrong. Which statement correctly identifies the bug?

main.js
function makeUser(name) {
  return
  {
    name: name
  };
}
console.log(makeUser("Asha"));
A Object keys must be in quotes
B The parameter name hides the property name
C makeUser must be called with new
D ASI ends the statement after return, so the function returns undefined; put { on the same line as return
Question page & discussion
Up next · Exercise 4JavaScript Basics Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links