InterviewSolution
Saved Bookmarks
| 1. |
What does “javascript:void(0)” mean? |
|
Answer» Let’s say the following is your array: VAR a = [99, 150, 299, 340, 390, 450, 590, 600];Now, get the length of the array: var len = a.length;Loop until the length of the array is more than 0. Use Math.random() to RANDOMIZE the elements. Decrement the value of len (length) after every iteration for each element: while (len > 0) { index = Math.floor(Math.random() * len); len--; t = a[len]; a[len] = a[index]; a[index] = t; }The following is an example that shuffles an array: <html> <body> <script> function SHOW(a) { var len = a.length; var t, index; while (len > 0) { index = Math.floor(Math.random() * len); len--; t = a[len]; a[len] = a[index]; a[index] = t; } return a; } var a = [99, 150, 299, 340, 390, 450, 590, 600]; document.write("Array = "+a); document.write("<br>SHUFFLING the array = "+show(a)); </script> </body> </html>The output: Array = 99,150,299,340,390,450,590,600 Shuffling the array = 390,340,150,590,99,450,299,600 |
|