document.getElementById("result4").innerHTML = `${key} : ${count[key]}`
Will override the innerHTML.
Consider 'adding' the content using appendChild or insertAdjacentHTML:
Is it possible to append to innerHTML without destroying descendants' event listeners?
const res = document.getElementById("result4");
for (const key in count) {
res.insertAdjacentHTML('beforeend', `<p>${key} : ${count[key]}</p>`);
}
function ex44() {
let string = document.getElementById("sarc4").value
string = string.toLowerCase();
const count = {}
string.split("").forEach(ch => {
if (!count[ch]) {
count[ch] = 1
return
}
count[ch]++
})
const res = document.getElementById("result4");
for (const key in count) {
res.insertAdjacentHTML('beforeend', `<p>${key} : ${count[key]}</p>`);
}
}
<label for="sarcina4"> Enter text</label>
<br>
<textarea name="sarcina4" id="sarc4" cols="60" rows="5"></textarea>
<br>
<button onclick="ex44()">Afisare</button>
<p id="result4"></p>
Note: I've wrapped the ${key} : ${count[key]} inside a <p></p> tag so we see each letter on a separate row
Another option is to create an output string by using map and join like so:
const output = Object.keys(count).map((key) => `<p>${key} : ${count[key]}</p>`);
document.getElementById("result4").innerHTML = output.join('');
function ex44() {
const input = document.getElementById("sarc4").value.toLowerCase();
const count = {}
input.split("").forEach(ch => {
if (!count[ch]) {
count[ch] = 1;
} else {
count[ch]++
}
});
const output = Object.keys(count).map((key) => `<p>${key} : ${count[key]}</p>`);
document.getElementById("result4").innerHTML = output.join('');
}
<label for="sarcina4"> Enter text</label>
<br>
<textarea name="sarcina4" id="sarc4" cols="60" rows="5">test</textarea>
<br>
<button onclick="ex44()">Afisare</button>
<p id="result4"></p>