More low, see the Great God help supplement
1. Given an array:, define a function to get all the odd numbers in the array, and return a new array;
var arr1=[1,3,4,5,6,7,8,3,4,2,3,6];
function Odd (arr) {
var newarr=[]
for (var i =0; i<arr.length;i++) {
if (arr[i]%2!=0) {
Newarr.push (Arr[i])
}
}
return NEWARR;
}
Console.log (Odd (arr1));
2. Given an array: Define a function to get the most repeated occurrences of the number in the array; there is a bug when the two occurrences are as much as sad.
There is a bug when two occurrences are as many as the sad reminder
var arr2=[1,3,4,5,6,7,8,3,4,2,3,6];
function MaxLen (arr2) {
var maxlen =0,maxkey; Maxkey Most of the time, the maximum number of initial value 0 (guaranteed full traversal);
for (var j = 0;j<arr2.length;j++) {
var len=0; The count of each loop is initialized with a Len value of 0;
for (Var k=0;k<arr2.length;k++) {
if (Arr2[k]===arr2[j]) {
len++; How many times each item appears
}
}
if (Maxlen<len) {
Maxlen=len;
MAXKEY=ARR2[J];
};
}
Console.log ("The maximum number of times is:" +maxlen+ "," +maxlen+ "occurs")
}
MaxLen (ARR2);
3. Given an array: sort by the age of the field from large to small;
var arr3 = [{age:2,id:0},{age:12,id:9},{age:14,id:8},{age:22,id:6}];
Objectorder (ARR3, "age"); Calling functions
function Objectorder (Arr,pro) {//The object is sorted by pro size
for (var i = 0;i<arr.length;i++) {
for (Var j =i+1;j<arr.length;j++) {
var temp;
if (typeof (Arr[i][pro]) = = "undefined") {
Alert ("format does not meet the requirements");
Return
}
if (Arr[i][pro] < Arr3[j][pro]) {//Put the small back
temp = Arr[j];
ARR[J] = Arr[i];
Arr[i] = temp;
}
}
}
Console.log (arr); Output ordered ARR
}
4. Give you a string of numbers, convert to currency format, two digits after the decimal point, currency prefix determined by the entry parameter
Problem: The money has shrunk after the change. "
"$" + num + ". 00" This is the equivalent.
var num = "1234567"; 012,345.670
var arr = num.split (""); Array ["1", "2", "3", "4", "5", "6", "7"]
function $currency (num,$) {
$ = $ || "¥";
var num = num+ "";
var arr = num.split ("");
var len = arr.length;
Switch (len%3) {//Processing data format
Case 0://001.234.56
Arr.unshift ("0", "0");
Break
Case 1://012.345.67
Arr.unshift ("0");
Break
Default://Two remainder
Arr
Break
}
Console.log (arr);
var L = Math.floor (ARR.LENGTH/3);
var s= "";
for (Var k=0;k<l;k++) {
if (K<L-1) {
S+=parseint (Arr.splice (0,3). Join ("")) + ","
}else{
S+=parseint (Arr.splice (0,3). Join ("") + "."
}
}
Console.log ($+s+arr.join (""));
}
$currency (num, "¥"); ¥12,345.67
JS algorithm problem