-
Notifications
You must be signed in to change notification settings - Fork 4
/
000022.考查数组方法.html
60 lines (56 loc) · 1.69 KB
/
000022.考查数组方法.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>考查数组方法</title>
</head>
<body>
<script>
// 问题1.请写出判断一个变量名为arr的方法
var arr = [1,2,3];
console.log(arr instanceof Array);
// 问题2.如何实现数组的随机排序(两种方法以上)?洗牌算法
var oldArr = ["a","b","c","d","e","f","g"];
function randomSort(oldArr) {
var newArr=[];
while (oldArr.length!=0){
var randomIndex = parseInt(Math.random()*(oldArr.length-1));
newArr.push(oldArr[randomIndex]);
oldArr.splice(randomIndex,1);
}
return newArr;
}
console.log(randomSort(oldArr));
// 问题3:实现对该数组的倒排?实现对该数组的降序排序(根据a从大到小排序)?
var array = [{a:4,b:5},{a:2,b:3},{a:5,b:2},{a:0,b:5}];
//倒排
function revise(oldArray) {
var newArray = [];
for(var i=array.length-1;i>-1;i--){
console.log(i);
newArray.push(oldArray[i]);
}
return newArray;
}
console.log(revise(array));
//降序排序
function mySort(array) {
for(var j=0;j<array.length;j++){
for(var w=0;w<array.length-1;w++){
if(array[w].a<array[w+1].a){
var v = [];
v = array[w];
array[w] = array[w+1];
array[w+1] = v;
}
}
}
return array;
}
console.log(mySort(array));
// 问题4:请使用Math.max取出最大值(请不要使用遍历)。
var a = [1,3,4,0,2,55]
console.log(Math.max(a));
</script>
</body>
</html>