Array de-weight and sorting/0~10 random numbers/characters and times in a string

Source: Internet
Author: User

Array de-weight and sort:

var arr = [1,5,1,2,6,8,1,81,9,0];for (var i=0;i<arr.length;i++) {for (Var j=i+1;j<arr.length;j++) {if (arr[i] = = Arr [j]) {Arr.splice (j,1); j--;}}} Arr.sort (A, b) {return a-b;//from small to Large}) alert (arr);//0,1,2,5,6,8,9,81arr.sort (function (A, b) {return b-a;// From large to small sort}) alert (arr);//81,9,8,6,5,2,1,0

Random number between 0~10:

Alert (Math.Round (Math.random () *10));
Math.random ()  random decimals between 0~1
Math.Round ()   rounding

The most occurrences of characters and times in a string:

var str = ' Ssaaaasdfssszsssaaw '; function test (str) {var obj = {};var num = 0;var value = '; for (Var i=0;i<str.length;i++ {if (!obj[str[i]]) {obj[str[i]] = [];} Obj[str[i]].push (Str[i]);} for (var attr in obj) {if (num<obj[attr].length) {num = Obj[attr].length;value = Obj[attr][0];}} Return ' The most characters are: ' +value+ ', appeared: ' +num+ ';} Alert (Test (str))

Array de-weight and sorting/0~10 random numbers/characters and times in a string

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.