0

I need to make a button that is able to replace array positions(text). e.g. array position 3 needs to become array position 1. It only has to work one time, although learning how to make it do multiple times is always welcome. It has to be done with a for loop. If modulo is a solution, please explain it to me since I am not entirely sure how it works. Here is my code:

<body>
<button type='button' onclick="Husselaar()">Husselen!</button>
        <br>
        <script>
            var games = ["Minecraft", "Assassin&#39;s Creed", "Rise Of The Tomb Raider", "Far Cry", "Tom Clancy&#39;s Rainbow Six Siege", "Call of Duty", "Grand Theft Auto V", "Hotline Miami", "American Truck Simulator", "Life is Strange"];
            var arrayLength = games.length;
            for (var i = 0; i < arrayLength; i++) {
                document.write(games[i] + "<br>");
                }
            function Husselaar(){
                    var husselaar = document.getElementById("");
                    Math.floor((Math.random(husselaar) * 9)+ 1);
                }
        </script>
</body>

Thanks in advance! :)

1 Answer 1

4

You can extract a part of your array with Array.splice(startposition,amount).

Use Array.concat to merge the old array with the resulting one.

var games = ["Minecraft", "Assassin&#39;s Creed", "Rise Of The Tomb Raider", "Far Cry", "Tom Clancy&#39;s Rainbow Six Siege", "Call of Duty", "Grand Theft Auto V", "Hotline Miami", "American Truck Simulator", "Life is Strange"];

function toStart() {
  var promptStr = "Choose the game to send to start:\n";
  for (var i = 0; i < games.length; i++) {
    promptStr += i + ": " + games[i] + "\n"
  }
  var index = parseInt(prompt(promptStr));

  games = games.splice(index, 1).concat(games);
  console.log(games);
}
<button type='button' onclick="toStart()">Husselen!</button>

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

1 Comment

Thank you ever so much for getting me out of this situation! :)

Your Answer

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