Need Help With Some Simple Code (Beginner)

Discussion in 'Javascript' started by SirFancyGecko, Feb 12, 2020.

  1. SirFancyGecko

    SirFancyGecko

    Joined:
    Feb 12, 2020
    Messages:
    2
    Likes Received:
    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: Feb 12, 2020
    SirFancyGecko, Feb 12, 2020
    #1
    1. Advertisements

  2. SirFancyGecko

    pdiddles03

    Joined:
    Feb 12, 2020
    Messages:
    6
    Likes Received:
    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 (Text):
    var z = 0;
    function getQuote(){
         var y = document.getElementById('demo');
         if(quotes[z]==undefined) z=0;
         else z++;
         y.innerHTML = quotes[z];
    }
     
     
    pdiddles03, Feb 12, 2020
    #2
    1. Advertisements

  3. SirFancyGecko

    SirFancyGecko

    Joined:
    Feb 12, 2020
    Messages:
    2
    Likes Received:
    1
    oh duhhhhhh

    Thanks for your help!
     
    SirFancyGecko, Feb 12, 2020
    #3
    pdiddles03 likes this.
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.