Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <body>
  <header class="nav-bar">
    <h1 class="brand">Wordle Clone</h1>
  </header>
  <div class="loading show">
    <div class="spiral">🌀</div>
  </div>
  <div class="game">
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
    <div class="letter"></div>
  </div>

  <script src="script.js"></script>
</body>
              
            
!

CSS

              
                * {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0;
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}

.brand {
  margin-bottom: 0;
}

.letter {
  width: 60px;
  height: 60px;
  margin: 5px;
  border: 5px solid black;
  display: flex;
  align-items: center;
  justify-content: center;
}

.nav-bar {
  text-align: center;
}

.game {
  max-width: 400px;
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  margin: 0 auto;
}

.loading {
  display: flex;
  align-items: center;
  justify-content: center;
  visibility: hidden;
}

.show {
  visibility: visible;
}

.spiral {
  font-size: 40px;
  animation: spin 1.5s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.correct {
  background-color: lightgreen;
}

.close {
  background-color: lightyellow;
}

.wrong {
  background-color: lightcoral;
}

.invalid {
  animation: tilt-shaking 0.5s;
}

@keyframes tilt-shaking {
  0% {
    transform: rotate(0deg);
  }
  25% {
    transform: rotate(5deg);
  }
  50% {
    transform: rotate(0eg);
  }
  75% {
    transform: rotate(-5deg);
  }
  100% {
    transform: rotate(0deg);
  }
}

.rainbow {
  animation: rainbow 2.5s linear;
  animation-iteration-count: infinite;
}

@keyframes rainbow {
  100%,
  0% {
    color: rgb(255, 0, 0);
  }
  8% {
    color: rgb(255, 127, 0);
  }
  16% {
    color: rgb(255, 255, 0);
  }
  25% {
    color: rgb(127, 255, 0);
  }
  33% {
    color: rgb(0, 255, 0);
  }
  41% {
    color: rgb(0, 255, 127);
  }
  50% {
    color: rgb(0, 255, 255);
  }
  58% {
    color: rgb(0, 127, 255);
  }
  66% {
    color: rgb(0, 0, 255);
  }
  75% {
    color: rgb(127, 0, 255);
  }
  83% {
    color: rgb(255, 0, 255);
  }
  91% {
    color: rgb(255, 0, 127);
  }
}

              
            
!

JS

              
                // creates array of letters
const letters = document.querySelectorAll(".letter");
const loadingDiv = document.querySelector(".loading");

const ANSWER_LENGTH = 5;
const ROUNDS = 6; // current guess starts at 0

function isLetter(letter) {
  // regex to check letter
  return /^[a-zA-Z]$/.test(letter);
}

function setLoading(isLoading) {
  // show the spinning spiral when its loading
  loadingDiv.classList.toggle("show", isLoading);
}

// keeps track of letter frequency in word
function makeMap(array) {
  const obj = {};
  for (let i = 0; i < array.length; i++) {
    const letter = array[i];
    // if it exists will return true if not false
    if (obj[letter]) {
      obj[letter]++;
    } else {
      obj[letter] = 1;
    }
  }
  return obj;
}

async function init() {
  let currentGuess = "";
  let currentRow = 0;
  let isLoading = true;
  const response = await fetch(
    "https://words.dev-apis.com/word-of-the-day?random=1"
  );
  const processedResponse = await response.json();
  const word = processedResponse.word.toUpperCase();
  const wordArray = word.split("");
  let done = false;

  setLoading(false);
  isLoading = false;

  function addLetter(letter) {
    if (currentGuess.length < ANSWER_LENGTH) {
      // add letter to guess
      currentGuess += letter;
    } else {
      // replaces the last letter
      currentGuess =
        currentGuess.substring(0, currentGuess.length - 1) + letter;
    }
    // 5 letters each row times row number to get correct starting position for word and currentGuess.length-1 to get position of word at the typed letter
    letters[
      ANSWER_LENGTH * currentRow + currentGuess.length - 1
    ].innerText = letter;
  }

  async function commit() {
    if (currentGuess.length !== ANSWER_LENGTH) {
      // do nothing if guess is not 5 char long
      return;
    }

    isLoading = true;
    setLoading(true);
    const response = await fetch("https://words.dev-apis.com/validate-word", {
      method: "POST",
      body: JSON.stringify({ word: currentGuess })
    });
    const processedResponse = await response.json();
    const validWord = processedResponse.validWord;

    setLoading(false);
    isLoading = false;
    // if word is not valid
    if (!validWord) {
      markInvalidWord();
      return;
    }

    // validate word
    const guessArray = currentGuess.split("");
    const map = makeMap(wordArray);

    // checks if its correct
    for (let i = 0; i < ANSWER_LENGTH; i++) {
      if (guessArray[i] === wordArray[i]) {
        letters[currentRow * ANSWER_LENGTH + i].classList.add("correct");
        // removes letter from frequency if they are correct
        map[guessArray[i]]--;
      }
    }

    // checks if incorrect or close
    for (let i = 0; i < ANSWER_LENGTH; i++) {
      if (guessArray[i] === wordArray[i]) {
        // do nothing, already did
      } else if (wordArray.includes(guessArray[i]) && map[guessArray[i]] > 0) {
        // if wordArray includes it and if hasnt been selected already
        letters[currentRow * ANSWER_LENGTH + i].classList.add("close");
        map[guessArray[i]]--;
      } else {
        letters[currentRow * ANSWER_LENGTH + i].classList.add("wrong");
      }
    }

    currentRow++;
    // Win and lose condition
    if (currentGuess === word) {
      document.querySelector(".brand").classList.add("rainbow");
      done = true;
    } else if (currentRow === ROUNDS) {
      alert(`You lose, the word was ${word}`);
      done = true;
    }

    currentGuess = "";
  }

  function backspace() {
    currentGuess = currentGuess.substring(0, currentGuess.length - 1);
    letters[ANSWER_LENGTH * currentRow + currentGuess.length].innerText = "";
  }

  function markInvalidWord() {
    for (let i = 0; i < ANSWER_LENGTH; i++) {
      letters[currentRow * ANSWER_LENGTH + i].classList.remove("invalid");

      setTimeout(function () {
        letters[currentRow * ANSWER_LENGTH + i].classList.add("invalid");
      }, 10);
    }
  }

  document.addEventListener("keydown", function handleKeyPress(event) {
    const action = event.key;

    if (done || isLoading) {
      // do nothing
    } else if (action === "Enter") {
      commit();
    } else if (action === "Backspace") {
      backspace();
    } else if (isLetter(action)) {
      addLetter(action.toUpperCase());
    } else {
      // do nothing
    }
  });
}

init();

              
            
!
999px

Console