Reputation:
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's Creed", "Rise Of The Tomb Raider", "Far Cry", "Tom Clancy'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! :)
Upvotes: 0
Views: 63
Reputation: 6366
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's Creed", "Rise Of The Tomb Raider", "Far Cry", "Tom Clancy'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>
Upvotes: 4