An arrangement that does not give the same result 1 to 2 times in a row

4

My program in js. What I want to do is that every time a button with a function is pressed, this is activated, but the problem is that the result is repeated often and is annoying.

var Loot = ["manzana", "pera","melon","sandia","limón","durazno"];



    var Hrandom = Loot[Math.floor(Loot.length * Math.random())];


        document.getElementById("demo").innerHTML =""+Hrandom;

I'm still noob, but I want to do it when I can. I know he's missing something ... something tells me he's missing an IF, but I do not know where. Or maybe a loop.

I just want apple, lemon ... etc. to come out ... no lemon, lemon, apple ... I just want it not to happen again two or three times in a row, that's all, thank you.

    
asked by Oscar Buentello 12.10.2016 в 20:38
source

5 answers

1

I did not understand very well what you want to do, but this adds the elements of your arrangement N times, without repeating them.

<p id="demo"></p>

for(var i = 0; i < 10; i++){
    var Loot = ["manzana", "pera","melon","sandia","limón","durazno"];
    var Hrandom = Loot[Math.floor(Loot.length * Math.random())];
    if(document.getElementById("demo").innerHTML.indexOf(Hrandom) === -1){
        document.getElementById("demo").innerHTML +=" "+Hrandom+" ";
    }
}
    
answered by 12.10.2016 в 20:55
0

If you do not want it to repeat itself, simply go into a cycle until it is not the same value you gave it the last time.

Something like:

var Loot = ["manzana", "pera","melon","sandia","limón","durazno"];
do
  var Hrandom = Loot[Math.floor(Loot.length * Math.random())];
while (""+Hrandom == document.getElementById("demo").innerHTML)
document.getElementById("demo").innerHTML =""+Hrandom;
    
answered by 12.10.2016 в 20:48
0
var Loot = ["manzana", "pera","melon","sandia","limón","durazno"];
var Hrandom;
 while(document.getElementById("demo").innerHTML == ""+Hrandom){
    var Hrandom = Loot[Math.floor(Loot.length * Math.random())];
 }
    document.getElementById("demo").innerHTML =""+Hrandom;

It's never going to repeat

    
answered by 12.10.2016 в 20:47
0

Here is another example with a function to detect possible duplicates in an array.

var Loot  = ['manzana', 'pera', 'melon', 'sandia', 'limón', 'durazno'];
var Hrandom = [];

for (var i = 0; i < Loot.length; i++) {  
  Hrandom.push(Loot[Math.floor(Loot.length * Math.random())]);        
}

function detectarDuplicado(item){
  
  var noDuplicado = [];
  
  item.forEach(function(item) {
  
      if (noDuplicado.indexOf(item) === -1) {
      
          noDuplicado.push(item);
      }
  });
  
  return noDuplicado.join(', ');
}


document.getElementById('demo').textContent = detectarDuplicado(Hrandom);
<p id="demo"></p>
    
answered by 12.10.2016 в 21:42
0

If I understand what you mean, this would be the code of a function that, when executed, randomly chooses an element of the array, and writes that value to the html content of a tag with demo id:

var Loot = ["manzana", "pera","melon","sandia","limón","durazno"];

var miFuncion = function(){
  var Hrandom = Loot[Math.floor(Loot.length * Math.random())];
  document.getElementById("demo").innerHTML = Hrandom;
}

Now, to click, I imagine you'll have a button on an html tag:

<button id="btnrandom" type="button">aleatorio</button>

You can add the onclick attribute to that button so that it executes your function when you click on it:

<button id="btnrandom" type="button" onclick="miFuncion();">aleatorio</button>

or by javascript:

document.getElementById("btnrandom").addEventListener("click",miFuncion);

and so each time the button is clicked the value in the div with id="demo" will change.

I leave the link of a Fiddle with the code running: link

    
answered by 12.10.2016 в 21:50