LearnFrenzy
Sign in Take a free test

Strings · Find the Error · Question 6

Error: reversing a string directly Answer, explanation & discussion

  • Find the error
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

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

main.js
const word = "level";
console.log(word.reverse() === word ? "Palindrome" : "Not a palindrome");
  • A It prints Palindrome
  • B It prints Not a palindrome
  • C SyntaxError
  • D TypeError: word.reverse is not a function – strings have no reverse; use [...word].reverse().join("") Correct
Correct Answer: Option D

reverse is an Array method. Convert, reverse and join: [...word].reverse().join("") === word.

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