i was trying to build a real time word counter bassed on the requirements here:
and im stuck in step 4,5, and 6 and i don’t know why !
HTML :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real Time Counter</title>
<link rel='stylesheet' href='styles.css'>
</head>
<body>
<div id='container'>
<h1> Real-Time Word Counter</h1>
<textarea rows=5 id='text-input'
placeholder='Type Your words here'></textarea>
<p id='char-count'>Character Count: 0/50</p>
</div>
<script src='script.js'></script>
</body>
</html>
JS :
const textArea = document.getElementById('text-input');
let p = document.getElementById('char-count');
let charCount = 0;
textArea.addEventListener('input', (event)=> {
if(event.inputType == 'insertFromPaste'){
if(textArea.value.length >= 50){
textArea.value = textArea.value.substring(0,50);
charCount = textArea.value.length;
p.style.color= 'red';
} else {
charCount = textArea.value.length;
p.style.color= 'black';
}
}
else if (event.inputType !== 'deleteContentBackward') // inserting text or insertLineBreak
{
if(charCount < 50)
{
charCount++ ;
p.style.color= 'black';
}
if(charCount >= 50){
textArea.value = textArea.value.substring(0,50);
p.style.color= 'red';
}
}
// deleteBackward
else{
if(charCount != 0)
{
charCount-- ;
p.style.color= 'black';
}
}
p.innerText= `Character Count: ${charCount}/50`;
});
CSS:
body{
padding: 0;
margin: 0;
height: 100vh;
background-color: hsl(50 , 40% , 60%) ;
display: flex;
justify-content: center;
align-items: center;
box-sizing: border-box;
}
#container{
width: 80%;
max-width: 500px;
min-width: 250px;
display: flex;
flex-direction: column;
padding: 10px;
}
#container h1 , p , #text-input{
align-self: center;
}
#text-input{
width: 80%;
}
i already had many tests and it gave good results but the compile doesnt belive my anwser ![]()
