0

I have array with 10 items, I calls one random item using a random number from 1 to 10, what to use to make a random number from 1 to 10, which will not happen again, and when all 10 is used, the program stops randomly? code

const num = () => Math.floor(Math.random() * 10);
  const postWord = () => {
    randomWord = word.innerText = words[num()].PL;
  }

  postWord();

  submit.addEventListener("click", function() {
    postWord();
  });
wwwkuba
  • 1
  • 5
  • 1
    So you basically want to [shuffle](https://stackoverflow.com/questions/2450954/how-to-randomize-shuffle-a-javascript-array) the array? – str Oct 07 '18 at 18:50
  • Yeah, that's a *random order* of a fixed list of numbers, not a list of *random numbers*. – deceze Oct 07 '18 at 19:04
  • I'm not sure how the program "stops randomly" after a list of exactly 10 non-repeating numbers has been used. Sounds like it will pretty predictably stop after exactly 10 numbers. – deceze Oct 07 '18 at 19:05
  • This program is about the number of words (10 for example). One word is random and you have to translate it, and if you translate it, the next one is drawn, and when the words end, the game is over. But in total with this end of the program I can do it differently. – wwwkuba Oct 07 '18 at 20:40

1 Answers1

0

Have you ever considered to move the array items?

var range = 10; // global variable

const num = () => Math.floor(Math.random() * range);
  const postWord = () => {
    randomWord = word.innerText = words[num()].PL;
    for (var i=num(); i < range; i++) {
        var temp = words[i];
        words[i] = words[i+1];
        words[i+1] = temp;
    }
    range--;
  }

  postWord();

  submit.addEventListener("click", function() {
    if (range) {
      postWord();
    }
  });

I am not that familiar with JS but I guess my code can at least demonstrate my point.