Generate a random array of [1,2,,3,4,5,6,7,8,9]?

Source: Internet
Author: User

Today suddenly saw a question about JS, think about it, did not think of how to do, I was a small white, in the study of the road, at this time thinking seriously pondering it, the following is the process of their own pondering.

The first step: Of course, the declaration of a random number of the array, the number of times the group has 1-9, it takes 9 values, it is clear that the loop, in what conditions to loop it, because the array is counted from 0, so the loop condition

The value of I takes 0-8, i.e. i<9 or i<=8.

I printed the results at the end of the run: [3,9,4,4,8,3,0,2,9] generated 9 numbers, but obviously there are three errors: (1) The result contains "0" (2) has a duplicate number (3) number is not sorted

As long as the three problems solved the 0k.

Step two: First solve the first problem

The random number is first judged to be greater than 0 before it is put into the array, but what is the result of the operation: [7,9,7,9,3,3,1,6], what? Why only 8 number, the original is a value of 0, was cruel in the outside of the array, but it is also wearing a numbered clothes, then what to do, resulting in a lot less value, let us encounter this situation to an else, run the result for [9,7,2,7,3,1,5,4,6], The first problem is solved.

The third step: the array to the weight, that is, the generated random number and the array before all the numbers are compared, the results are not the same, and then put in, if there is a same, is not also to take it with the number of clothes off Ah, now the new random number is not any other than the existing one can be put in, We add a counter, the final value of the counter and the length of the existing array is equal to the new random number can be put into the array, the counter must be added in the generation of random number loop, because each generation of a new random counting timer to be emptied.

Fourth step, array sorting, we all know the array of the sort () method, but the number of sorting problems, such as: 1,2,11,22, sorting will be problematic, output: 1,11,2,22, it will only be sorted according to the first digit, so we want to add a sort function Sortnum (A, A, b) {return a-B;}, from small to large is a-a, from big to small is b-a, the alphabet only with sort ()

Summary: The individual is small white, only slowly, from the simple to do, 1.1 points of mastery. If a great God accidentally passing by, do not spray.

  

Generate a random array of [1,2,,3,4,5,6,7,8,9]?

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.