JavaScript bootcamp · Lab 16

Anagram check

mediumStrings12 minLesson: String Methods

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

QuestionHint and solution stay closed until you open them

Read two words (one per line) and log yes if they are anagrams, otherwise no.

Anagrams use exactly the same letters rearranged. Compare case-insensitively.

Input. Two lines, each a word.

Output. yes or no (lowercase).

Examples

Example 1 — Same letters, different order.
Input
listen
silent
Output
yes
Example 2
Input
hello
world
Output
no
Hint
  1. Sort the letters of each word — anagrams become identical strings.
  2. [...word].sort().join("") gives a canonical form.
Show correct code

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

const a = [...readLine().trim().toLowerCase()].sort().join("");
const b = [...readLine().trim().toLowerCase()].sort().join("");
console.log(a === b ? "yes" : "no");
main.jsconsole.log · readLine() · Ctrl + Enter
ResultIdle
Run to see output. Check grades the tests.