I’ve sadly tried about 40 different ways, iteration through the array, moving variables around, I got it to work once but then the button only works once. why doesn’t this work, why wouldn’t length be defined??? I understand scope but what order do these things go in? how do i learn this?
I commented out what I was attempting because well, I want to understand why my every attempt has failed. here is my latest attempt which returns quoteMachine is not defined !!!???
function quoteMachine() {
for(let i = 0; i < quotes.length; i++){
newquote = quotes[i][0];
}return newQuote;
};
document.getElementById('text')innerHTML = newQuote;
function quoteMachine(){
for(let n of quotes){
document.getElementById('text').innerHTML = n[0];
document.getElementById('author').innerHTML =n[1];
}
};
this pushes the last index in and the button stops responding.
This will replace the contents of those elements with every one of the quotes in order and then stop on the last one. This is going to be fast enough that it won’t actually redraw the page for every one, and you’ll only see the final quote. It’s not that the button isn’t responding; it’s just that it does the same thing every time.
function quoteMachine() {
for(let i = 0; i < quotes.length; i++){
let newQuote = quotes[i][0];
document.getElementById("text")innerHTML = newQuote;
}
};
This is one of the many things ive tried: im still getting quoteMachine is undefined?
You don’t have any randomization, so it will always do the exact same thing. In this case it will go through all of the quotes in order and when the loop is done running newQuote will always be the last quote.