0

I made a JavaScript array randomizer and it show the elements one at a time with interval. I want to make them show one at a time without the interval, but with button. When I click the button it shall switch to the next element, but until then it remains the same. Can you help me do that?

var numberStrings = ["One", "Two", "Three", "Four", "Five", "Six"];

var i = 0;
setInterval(function() {
  document
    .getElementById('numberList')
    .innerHTML = numberStrings[i++];
  if (i == numberStrings.length) i = 0;
}, 2000);

function shuffleUsingRandomSwapping(array) {
  var j, x, i = 0,
    len = array.length;
  for (i; i < len; i++) {
    j = Math.floor(Math.random() * len);
    x = array[i];
    array[i] = array[j];
    array[j] = x;
  }
}
<button onclick="shuffleUsingRandomSwapping(numberStrings);updateNumberList(numberStrings);">Shuffle Using Random Swapping</button>

<div id="numberList"></div>

1
  • what is the problem then? just delete that interval and move its function to your updateNumberList function Commented Jul 7, 2018 at 9:09

1 Answer 1

1

var numberStrings = ["One", "Two", "Three", "Four", "Five", "Six"];

var curElement = 0;

function updateNumberList() {
  document
      .getElementById('numberList')
      .innerHTML = numberStrings[curElement++];
    if (curElement == numberStrings.length) curElement = 0;
}

function shuffleUsingRandomSwapping(array) {
  var j, x, i = 0,
    len = array.length;
  for (i; i < len; i++) {
    j = Math.floor(Math.random() * len);
    x = array[i];
    array[i] = array[j];
    array[j] = x;
  }
}
<button onclick="shuffleUsingRandomSwapping(numberStrings);updateNumberList(numberStrings);">Shuffle Using Random Swapping</button>

<div id="numberList"></div>

I don't know you situation, but if you just want to display a random element from an array each time you press the button, it should be done in a much easier way, without shuffling the array.

var numberStrings = ["One", "Two", "Three", "Four", "Five", "Six"];

function displayRandomElem() {
  document
      .getElementById('numberList')
      .innerHTML = numberStrings[Math.floor(Math.random() * numberStrings.length)];
}
<button onclick="displayRandomElem()">Shuffle Using Random Swapping</button>

<div id="numberList"></div>

To show the values in random orders, so that they don't repeat themselves, use the next code:

var numberStrings = ["One", "Two", "Three", "Four", "Five", "Six"];

function displayRandomElem() {
  if (numberStrings.length == 0) return;

  document
      .getElementById('numberList')
      .innerHTML = numberStrings.splice(Math.floor(Math.random() * numberStrings.length), 1);
}
<button onclick="displayRandomElem()">Shuffle Using Random Swapping</button>

<div id="numberList"></div>

Sign up to request clarification or add additional context in comments.

4 Comments

Yes, but I want to make them to not repeat themselves, until all are shown.
Do you want to show them in the order they appear in array, or random order?
in random order
Thank you so much!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.