Need Help With Some Simple Code (Beginner)

Joined
Feb 12, 2020
Messages
2
Reaction score
1
I'm experimenting with a site that cycles through an array of quotes, and I'm trying to make my code more concise. However, when I try to execute this, the button does nothing.

var quotes = ["The human race has one really effective weapon, and that is laughter. - Mark Twain", "Books serve to show a man that those original thoughts of his aren't very new at all. - Abraham Lincoln", "When it is obvious that the goals cannot be reached, don't adjust the goals, adjust the action steps. - Confucious", "Only two things are infinite, the universe and human stupidity, and I'm not sure about the former. - Albert Einstein"]

function getquote() {
var y = document.getElementById("demo");
var z = 0;
while (true) {
if (y.innerHTML === quotes[z]) {
z = z + 1;
y.innerHTML = quotes[z];
return;
} else {
z = z + 1;
}
}
}
 
Last edited:
Joined
Feb 12, 2020
Messages
8
Reaction score
0
Why are you using a while loop? It seems unnecessary here.

the variable "z" should be outside of the function, the way you have it, every time you run the function , Z goes back to 0.

Code:
var z = 0;
function getQuote(){
     var y = document.getElementById('demo');
     if(quotes[z]==undefined) z=0;
     else z++;
     y.innerHTML = quotes[z];
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top