I'm trying to make an effect of replacing one string with another, but without completely erasing the contents of the first one. As if it were a special effect, each letter of a string is transformed into another word. At first I'm trying to do it like a typewriter, but later I want to do it randomly. However, I can't get it to work, it looks like it's a bug.
let title1 = "Your sales place";
let title2 = "Software Hub";
var k = 0;
var speed = 150;
function typeWriter() {
if (k < title1.length) {
if (k >= title2.length) {
title2 += title1.charAt(k);
}
title2 = title2.replace(title2.charAt(k), title1.charAt(k));
document.getElementById("demo").innerHTML = title2;
k += 1;
setTimeout(typeWriter, speed);
}
}
typeWriter();
<div id="demo"></div>
output:
YO AESRL SUPLACE