JavaScript bootcamp · Lab 14

Reverse a word

easyStrings8 minLesson: Strings

Read the question, write JavaScript on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read one word and log it reversed.

Strings have no reverse() method, but arrays do — so spread the string into characters first.

Input. One line: a word.

Output. The word, reversed.

Examples

Example 1
Input
code
Output
edoc
Example 2 — Case is preserved, just reordered.
Input
JS
Output
SJ
Hint
  1. [...word] makes an array of characters.
  2. Chain it: [...word].reverse().join("").
Show correct code

Peek only after you have tried. You can still Check your own version.

const word = readLine().trim();
console.log([...word].reverse().join(""));
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.