JS implementation randomly extracts 5 different values from an array of length 100

Source: Internet
Author: User

Method One: This is my method, I hope you correct me ...

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Randomly extract 5 different values from a length of 100 array</title></Head><Body>    <Script>        vararr= NewArray ( -); varflag_a= NewArray ( -); /*this is a feature. itself is to set a flag property for each element of the array, but it does not work. So just create an array of the same size Boolean type*/         for (varI= 0; I<arr.length; I++) {Arr[i]= "This is the first"+I+"a"; Flag_a[i]= false; }         for (varI= 0; I< 5; i) {            varR=Math.floor (Math.random ()* -); if(!Flag_a[r]) {document.write (i+1+":"+Arr[r]+"<br/>"); FLAG_A[R]= true; I++;/*I think it's very creative to put i++ in this place. (*^__^*) hehe ...*/            }        }    </Script></Body></HTML>

Method Two: This method with the above is similar to the wonderful, is what others think, for everyone to reference ...

<!DOCTYPE HTML><HTMLLang= "en"><Head>    <MetaCharSet= "UTF-8">    <title>Randomly extract 5 different values from an array of length 100 02</title></Head><Body>    <Script>        vararr= NewArray ( -);  for (varI= 0; I<arr.length; I++) {Arr[i]= "This is the first"+I+"a"; }         for (varI= 0; I< 5;) {            varR=Math.floor (Math.random ()* -); if(Arr[r]!=NULL) {document.write (i+1+":"+Arr[r]+"<br/>"); ARR[R]= NULL;/*It is also good to use the set to NULL to determine whether it is used or not.*/I++; }        }    </Script></Body></HTML>

JS implementation randomly extracts 5 different values from an array of length 100

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.