JavaScript - Simple Rearrange Array Position
Submitted by razormist on Friday, May 17, 2019 - 17:30.
In this tutorial we will create a Simple Rearrange Array Position using JavaScript. This code will rearrange the array index position in a different position when clicked. The process use JavaScript for loop to compress an array and rearrange the index in order to display a different array index position. You can apply this script to your system that use image gallery, this is a user-friendly program feel free to modify it.
We will use JavaScript to add some new feature to the website interface by actually written into an HTML page. It is what gives your page a different interactive elements and animation that engage a user.
There you have it we successfully created a Simple Rearrange Array Position using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!
Getting Started:
This is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.The Main Interface
This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-6">
- <br />
- </div>
- </div>
- </body>
- </html>
Creating the Script
This code contains the script of the application. This code will rearrange the array index position when the button is clicked. To do this just copy and write these block of codes as shown below inside the text editor and save it as script.js inside the js directory- display();
- function arrayRear(arr) {
- for (var i = 0, rear = [], randomIndex = 0; i < arr.length; i++) {
- randomIndex = Math.floor(Math.random() * arr.length);
- while (rear.indexOf(arr[randomIndex]) !== -1) {
- randomIndex = Math.floor(Math.random() * arr.length);
- }
- rear.push(arr[randomIndex]);
- }
- return rear;
- }
- function display(){
- var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
- var html = "";
- for(var i=0; i<arr.length; i++){
- html += "<div width='200' height='200' style='margin:10px; font-size:50px; float:left;'>"+arr[i]+"</div>";
- }
- html += "<br style='clear:both;'/>";
- document.getElementById('result').innerHTML = html;
- }
- function reorder(){
- var arr = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"];
- var html = "";
- arrayRear(arr).forEach(function(item){
- html += "<div width='200' height='200' style='margin:10px; font-size:50px; float:left;'>"+item+"</div>";
- })
- html += "<br style='clear:both;'/>";
- document.getElementById('result').innerHTML = html;
- }
Add new comment
- 217 views