Randomly remove 10 numbers from 0~100

Source: Internet
Author: User

There are only two simple ways to learn

The first feature that can be rewritten using the array length

Idea: You can use a for loop to place the number from 0 to 100 in an array, then shuffle the 100 numbers using sort (), and then get 10 different numbers by rewriting the length of the array to 10.

<!     DOCTYPE html>varArr=[];  for(vari=0;i<100;i++) {//an array from 0 to 100Arr.push (i); } arr.sort (function(){//randomly scrambling this array        returnMath.random ()-0.5; }) Arr.length= 10;//Rewrite lengthConsole.log (arr);//the console will output 10 different numbers</script>

The second advantage is the unique character of the JSON object's key value.

Idea: Define an empty array and an empty JSON object to save the array first, respectively.

 <!     DOCTYPE html>// json object, the key value is unique, the key value can be a number     var  arr=[];     var  json={};  while  (Arr.length<10 var  k=math.round (math.random () *100 if  (! =true  ;        Arr.push (k); }} console.log (arr)  </script>

Randomly remove 10 number of repetitions from 0~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.