Build a Real Time Counter - Build a Real Time Counter

Tell us what’s happening:

Hi, is this not doing what the test demands? I’m failing test #6.

Your code so far

<!-- file: index.html -->
<!DOCTYPE html>
<html lang="en">
  <head>
    <link rel="stylesheet" href="styles.css" />
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <main>
      <div id="text-container">
        <p id="char-count">Character Count: 0/50</p>
        <textarea id="text-input"></textarea>
      </div>
    </main>
    <script src="script.js"></script>
  </body>
</html>

/* file: styles.css */
#text-area {
  width: 200px;
  max-width: 400px;
  height: 100px;
  max-height: 400px;
  margin-top: 20px;
  font-size: 16px;
  padding: 10px;
  box-shadow: 0 4px 8px rgba(255, 252, 252, 0.1);
  font-family: Arial, Helvetica, sans-serif;
}

div {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.capped {
  color: red;
}

/* file: script.js */
    const textArea = document.getElementById("text-input");
const textDisplay = document.getElementById("char-count");

textArea.addEventListener("input", () => {
  const currentLength = textArea.value.length;
  textDisplay.textContent = `Character Count: ${currentLength}/50`;
  if (currentLength >= 50) {
    textDisplay.classList.add("capped");
    textArea.value = textArea.value.substring(0, 49);
  }
});

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Safari/537.36 Edg/141.0.0.0

Challenge Information:

Build a Real Time Counter - Build a Real Time Counter

the character count is not being adjusted tho

that I don’t think it makes tests fail tho

check the length of the string after trimming

I think it’s slightly bugged because I passed the test when it allows 51/50 characters.

const textArea = document.getElementById("text-input");
const textDisplay = document.getElementById("char-count");

textArea.addEventListener("input", () => {
  const currentLength = textArea.value.length;
  textDisplay.textContent = `Character Count: ${currentLength}/50`;
  if (currentLength >= 50) {
    textDisplay.classList.add("capped");
    textArea.value = textArea.value.slice(0, 50);
  }
});

I would double check the length of textArea.value after slicing, or douuble check slice syntax: Array.prototype.slice() - JavaScript | MDN

that’s not 51 characters

right, I just mean the char count says 51 but it lets me pass. Thank you for your help with this though, DOM stuff is tricky, but the lessons are helping a lot.

if you want to report it as a bug, you can, it’s the same issue I pointed out earlier

Thank you for helping make FCC better. Bugs can be reported as GitHub Issues. Whenever reporting a bug, please check first that there isn’t already an issue for it and provide as much detail as possible.

1 Like