JS "Practice" with JS wrapper Java Shuffle function (scrambled array subscript method)

Source: Internet
Author: User

This method returns a completely new array so it does not return a reference array like the shuffle function in Java.

Ideas are as follows:

1. Create a new function passing in an array that needs to scramble the subscript

2. Get the length of the array

3. Create a new array to save and return the result

4, according to the length of the array to create a new random number (random number remember to take down the whole or will subscript out of bounds)

5, Loop added to the result array inside

The code is as follows:

<script type= "Text/javascript" >varTestarr =NewArray ("China", "United States", "England", "France", "Germany", "Spain", "Greece",
"Italy", "Japan", "Korea", "Nigeria", "India"); varNEWARR =Shuffle (Testarr); Console.log (NEWARR); functionShuffle (arry) {varSortarr =NewArray (); varlen = arry.length;//get Array length specify random number of times for(vari = 0; I <Len;) { varindex = Math.floor (Math.random () *Len); if(Arry[index]! =NULL) {Sortarr.push (Arry[index]); I++; Arry[index]=NULL; } } returnSortarr; } </script>

JS "Practice" with JS wrapper Java Shuffle function (scrambled array subscript method)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.