I’m a newbie in coding. so this may be a small problem.
So I need a script which can retype a block of text into another block of text when you click on a button.
Example:
“Hello” to “Goodby”.
Hello > Hollo > Hoolo > Goolo > Gooloe > Goodoe > Goodye > Goodbye.
The script must pick a random letter and change it into the letter from another text. There must be a short delay after each change, so it looks like text is randomly retyping.
This is the scheme of the solution I see.
Firstly I tried to do it with console.log to see how it works.
Unfortunatelly it doesn’t work.
It changes strings randomly as supposed, but it doesn’t end. And my browser constantly loading the page, so I cannot interact with it at all.
The code below
"use strict";
var desc1 = 'Hello';
var desc2 = 'Goodbye';
desc1 = desc1.split('');
desc2 = desc2.split('');
function randomInteger(min, max) {
let rand = min + Math.random() * (max + 1 - min);
return Math.floor(rand);
}
console.log (desc1);
if(desc1 > desc2){
var descLenght = desc1.length;
} else {
var descLenght = desc2.length;
}
// function retext
while (desc1 != desc2) {
let i = randomInteger(0, descLenght);
if (desc1[i] !== desc2[i]) {
desc1[i] = desc2[i];
console.log (desc1);
}
}
The first question:
Can you help me find the mistakes in my code?
The second question:
To make it slowly apper on a page I need to create a function with settimeout(0.05 sec) and use element.innerHTML instead of console.log. Am I right?
Thanks in advance for your help