Display messages from a list sequentially

-3

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

This code causes above variables to appear in random mode. But how do I appear normal 1 by 1 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();
    
asked by anonymous 03.07.2018 / 20:18

1 answer

2

Just create a loop to traverse the array sequentially:

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

It is not necessary to place repeated statements in braces because it is just an instruction. But if you need more, it's 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!

    
03.07.2018 / 20:25