I have this exercise in my course where they ask us to create a function that counts all the characters in a random text and to count all the occurences of each character in that text.
I tried to do all of that and i have both codes done, but i can't make them work in the website.
function to count all the characters in the box (textarea total characters) - this doesnt work
function to count the occurrences of each character in the box (text area total occurrences of a character - this works
let myText = document.getElementById("my-text");
myText.addEventListener("input", () => {
let count = (myText.value).length;
document.getElementById("count-result").textContent = `Character count: ${count}`;
});
function search() {
var myText = document.getElementById("my-text").value;
//console.log(textareaValue);
var characterToSearch = document.getElementById("characterToFind").value;
//console.log(characterToSearch);
var count = 0;
for (var i = 0; i < myText.length; i++) {
if (characterToSearch == myText[i]) {
count++;
}
}
document.getElementById("totalCharacters").innerHTML = count;
}
<div class="container">
<label>Introduce el texto que desees</label>
<br>
<textarea id="my-text" placeholder="escribe un texto"></textarea>
<h1 id="count-result">Character Count: </h1>
<br>
<label>Qué letra quieres que encontremos?</label>
<br>
<textarea placeholder="añade el caracter que quieres" id="characterToFind"></textarea>
<br>
<input type="button" onclick="search();" value="Search">
<br>
<label>Veces repetida : </label><label id="totalCharacters"></label>
</div>