LearnFrenzy
Sign in Take a free test

Strings · Find the Error · Question 11

Bug: an update is not offered Answer, explanation & discussion

  • Find the error
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 11 Back to Exercise

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 Correct
Correct Answer: Option D

String comparison looks only at the first differing character. Split and compare numerically: const [a, b] = latest.split(".").map(Number); … (or localeCompare with { numeric: true }).

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links