Display messages from a list sequentially

Asked

Viewed 66 times

-4

I created this function random, but as I’m still learning JS... I’m confused.

This code causes the above variables to appear in mode Random. But how do I get them to appear normal one by one without being Random...?

(function() {
  var quotes = [
    {
      text: "  apenas <span class='text-primary'><strong>15 moedas.</strong></span>",
    },          
    {
      text: "evolua connosco! <i class='fas fa-heart'></i>.",
    },  
    {
      text: " Procura alugar uma casa de f&eacute;rias? Visite eira Vacation</span>.</a>",
    },          
    {
      text: "  Compre j&aacute; em <a href=''><span class='text-primary'>Comprar Moedas</span>.</a>",
    }          

  ];
  function random() {
    var quote = quotes[Math.floor(Math.random() * quotes.length)];
    document.getElementById("quote").innerHTML =
    '<i class="fas fa-graduation-cap"></i> ' + quote.text + '';
  }
  random(); // Executa a primeira vez.
  setInterval(random, 4000);
})();

Editing

  function random() {

    var quote = quotes[Math.floor(Math.random() * quotes.length)];
  }

  for(var i = 0; i < quotes.length; i++) { 
        document.getElementById("quote").innerHTML = '<i class="fas fa-graduation-cap"></i> ' + quote.text + '';        
  }    

  random();
  • 1

    You created the function, so I assume you know what the Math.random() does, right? So what you need is a controlled counter, which is incremented one by one, showing the messages sequentially.

  • Yes I know that this function makes the count and shows in Andom, but demomento I was confused because I only wish that show sequential.

1 answer

2

Just create a loop to scroll through the array sequentially:

for(var i = 0; i < quotes.length; i++) quote = quotes[i];

It is not necessary to place repeated instructions between keys because it is just an instruction. But if you need more, it is done like this:

for(var i = 0; i < quotes.length; i++) {
  //Funções aqui
}

Reminder: When creating this variable i in the loop, it only exists in the loop!

  • humm like this way? It continues to do Random *ː Function Random() { var quote = Quotes[Math.floor(Math.Random() * Quotes.length)]; for(var i = 0; i < Quotes.length; i++) { Document.getElementById("quote"). innerHTML = '<i class="fas fa-Graduation-cap"></i> ' + quote.text + '; } }**

  • continues Random..

  • No, outside the Random function, even before it is executed.

  • above I put the edit as said, but error equal "Undefined" @mutlei

  • Ah, now I get it. Within the value that will be assigned to innerHTML, you are using quote.text. Usa quotes[i].text.

  • simply stands still at the last quote and does not advance to the remaining

  • Can you tell me what’s wrong? @mutlei

  • You want him to list the quotes, right? Then just change the innerHTML = (string) for innerHTML += (string). If you want to put each one in a different row, you can put the string inside a <p> or put a <br> at the end of the string

  • Now... none of that.. What I want is.. that he shows every 1 sentence 1 by 1 in every 4 seconds... As my code above showed, it shows 1 after another, then another. The problem with my code is.. it does Random, it can simply show 1 fake 3x in the same 4 sec.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.