The 8--of JS case is randomly taken from an array

Source: Internet
Author: User

In recent projects encountered a demand, from a list of random display of the list of parts, the demand is small, JS is also very easy to implement. It mainly uses the random method of the Math object, and the splice method of array.

The idea is to create a new array, hold all the lists, then figure out the random number, take the value of the random index from the array, and make a random array.

The source code is as follows:

1 <!DOCTYPE HTML>2 <HTML>3 <Head>4 <Metahttp-equiv= "Content-type"Content= "text/html; charset=utf-8;">5 <title>Random fetch from an array</title>6 <Metaname= "Author"content= "Rainna" />7 <Metaname= "keywords"content= "Rainna S js Lib" />8 <Metaname= "description"content= "JS" />9 </Head>Ten  One <Body> A <Script> - //The total length of the count array randomcount the number of random displays - varSelrandomnum= function(count,randomcount) { the     varCount=Count|| Ten; -     varRandomcount=Randomcount|| 3; -      -     varTotalarray=[],randomarray= []; +      for(varI=0, L=Count;i<L;i++){ - Totalarray.push (i); +     } A      for(varI=0, L=Randomcount;i<L;i++){ at         varRandomindex=Math.floor (Math.random ()*totalarray.length); -         varSelectindex=Totalarray.splice (Randomindex,1)[0]; - Randomarray.push (Selectindex);  -     } - Console.log (Totalarray+ '-----' +Randomarray); - } in  - selrandomnum (); to </Script> + </Body> - </HTML>

The 8--of JS case is randomly taken from an array

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.