[Solved] how to shuffle numbers with textarea in javascript


I found this great shuffle function from this answer:

function shuffle(array) {
  var currentIndex = array.length, temporaryValue, randomIndex;

  // While there remain elements to shuffle...
  while (0 !== currentIndex) {

    // Pick a remaining element...
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex -= 1;

    // And swap it with the current element.
    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
}

From there, you can simply get the value of the <textarea>‘s input, and split it based on linebreaks:

var numbers = document.getElementById("numberInput").value.split("\n");

Or if you prefer spaces:

var numbers = document.getElementById("numberInput").value.split(" ");

Then, just pass it into the function:

var shuffledNumbers = shuffle(numbers);

And show them on the page by iterating over them and writing them to the document:

shuffledNumbers.forEach(function(currentNumber) {
    document.write(currentNumber + "<br />");
})

And there you go!

EDIT:

If you want to display the shuffled numbers in another <textarea> instead:

var output = document.getElementById("output");
shuffledNumbers.forEach(function(currentNumber) {
    output.innerHTML += currentNumber + "\n";
})

6

solved how to shuffle numbers with textarea in javascript