I Built Wordle in Vanilla JS — the Duplicate-Letter Trap

작성자

카테고리:

← 피드로
DEV Community · Devanshu Biswas · 2026-06-27 개발(SW)

Devanshu Biswas

Wordle looks simple — until you try to color a guess with a repeated letter correctly. That edge case is the whole interview question hiding inside this game. Here’s a playable clone in vanilla JS.

🟩 Play it: https://dev48v.infy.uk/game/day18-wordle.html

The two-pass coloring trap

Guess KAYAK, answer APPLE (one A). The naive “is this letter in the word?” colors BOTH A’s yellow — wrong. The answer only has one A to give.

Correct algorithm is two passes:

  1. First pass: mark greens (letter right + position right) and “use up” those answer letters.
  2. Second pass: for the rest, mark yellow only if an unused copy of that letter remains in the answer — otherwise gray.

Track a per-letter remaining-count so duplicates color exactly right.

The rest

A 6×5 board, the active row captures typing, Enter reveals with a CSS flip, and an on-screen keyboard colors each key by its best-known state (green > yellow > gray). Win on match, reveal the answer on loss.

🔨 Full build (board → input → two-pass scoring → reveal + keyboard → win/lose) on the page: https://dev48v.infy.uk/game/day18-wordle.html

Part of GameFromZero. 🌐 https://dev48v.infy.uk

원문에서 계속 ↗

코멘트

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다