I'd like to write my own hackertyper site that prints itself when pressing some keys.
But when I try the code below, it prints everything at the same line. I used template literals (``) and couldn't make it. Then tried <br /> in the string but also not worked. How do I make it right?
Edit: I was using <p> tag in the HTML file, when I changed it to <pre> tag as Praveen said, it solved my problem. Thank you.
"use strict";
let str = `"use strict"
let str = "";
let emptyStr = "";
let i = 0;
document.addEventListener("keydown", function () {
emptyStr += str.charAt(i++) + str.charAt(i++) + str.charAt(i++);
document.querySelector("#string").textContent = emptyStr;
});`;
let emptyStr = "";
let i = 0;
document.addEventListener("keydown", function () {
emptyStr += str.charAt(i++) + str.charAt(i++) + str.charAt(i++);
document.querySelector("#string").textContent = emptyStr;
});
<p id="string"></p>