I can't understand what the problem is, I'm writing a script that will have to output the text on its own, as if someone is typing, but I'm constantly throwing out the same error, do not tell me what can be done in this case, I will be grateful.
the error is that the substring method does not work Uncaught TypeError: Cannot read properties of undefined (reading 'substring')
let quoteArray = [];
let index = 0;
let textPosition = 0;
let flag = true;
let destination = document.querySelector('#typedtext');
window.addEventListener('load', typewriter);
function loadQuote() {
const url = "https://api.quotable.io/random";
fetch(url)
.then(response => {
if(response.ok) {
return response.json();
} else {
console.log(response.status);
}
})
.then(data => {
quoteArray[index] = data.content;
});
}
function typewriter(){
if(flag) {
loadQuote();
quoteArray[index] + ' ';
flag = false;
}
destination.innerHTML = quoteArray[index].substring(0, textPosition); the error is here
if(textPosition++ != quoteArray[index].length){
setTimeout('typewriter()', 100);
} else {
quoteArray[index] = ' ';
setTimeout('typewriter()', 3000);
textPosition = 0;
flag = true;
}
}