LearnFrenzy
Sign in Take a free test

Strings · Exercise 3 of 4

Strings Find the Error

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

Debug fifteen JavaScript string programs: SyntaxErrors from mismatched quotes, raw line breaks, unclosed placeholders and invalid escapes; runtime errors from missing methods, null values and invalid arguments; and silent bugs from string comparison, case, whitespace, unsaved results and emoji length.

Question 11 Medium

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

main.js
const latest = "10.2";
const installed = "9.8";
console.log(latest > installed ? "Update available" : "Up to date");
A > cannot compare strings
B The ternary is reversed
C Version strings must not contain dots
D The versions are compared as strings, and "1" < "9" decides it – compare the parts as numbers
Question page & discussion
Question 12 Easy

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

main.js
const answer = "yes";
if (answer === "Yes") {
  console.log("Subscribed");
} else {
  console.log("Not subscribed");
}
A === cannot compare strings
B The else branch always runs
C === is case-sensitive – compare answer.toLowerCase() === "yes"
D answer must be declared with let
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 typed = "admin ";              // copied with a trailing space
console.log(typed === "admin" ? "Welcome" : "Access denied", typed.length);
A Usernames must be in capitals
B The input has a trailing space, so it is not equal to "admin" – compare typed.trim()
C === compares lengths only
D The ternary needs parentheses
Question page & discussion
Question 14 Easy

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

main.js
let msg = "Hello World";
msg.replace("World", "LearnFrenzy");
console.log(msg);
A replace only works with regular expressions
B let strings cannot change
C replace returns a new string; the result was not stored – msg = msg.replace(…)
D replace needs replaceAll
Question page & discussion
Question 15 Hard

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

main.js
const status = "Great job \u{1F389}\u{1F389}";   // "Great job 🎉🎉"
console.log(`${status.length}/12 characters`);
A Template literals add extra characters
B The space is counted twice
C length counts UTF-16 units and each emoji uses two – count code points with [...status].length
D length includes the quotes
Question page & discussion
Up next · Exercise 4Strings Coding Problems10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links