-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.js
More file actions
138 lines (91 loc) · 2.67 KB
/
6.js
File metadata and controls
138 lines (91 loc) · 2.67 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
let arr = [1,2,3,4,5,6]; //output: "123456"
let str=""
for(let val of arr){
str += val
}
console.log(str);
console.log("**********************************************************")
let str1=""
for(let index in arr){
let val =arr[index]
if (index<arr.length-1){
str1 += val+","
}else{
str1 += val
}
}
console.log(str1);
console.log("**********************************************************")
console.log(arr.join(","))
console.log("**********************************************************")
//find
let arr1=[2,1,3,4,5,6,7];
console.log(arr1.find(function(val2){
return val2 ==6;
}))
console.log("**********************************************************")
let arr2=[2,1,3,4,5,6,7];
console.log(arr2.find(function(val2){
return val2 ==8;
}))
console.log("**********************************************************")
let arr3=[10,1,9];
console.log(arr3.sort(function(a,b){
// if (a>b){
// return 1
// }else if(a<b){
// return 1
// }else{
// return 0
// }
return a - b;
}));
console.log("**********************************************************")
let arr4 = [1, 10, 11, 21, 55, 5, 65, 77, 7, 6, 56, 36, 33, 67, 2, 12];
arr4.sort((a, b) => {
let lastDigitA = a % 10;
let lastDigitB = b % 10;
return lastDigitA - lastDigitB;
});
console.log(arr4);
console.log("**********************************************************")
let arr5 = [1, 10, 11, 21, 55, 5, 65, 77, 7, 6, 56, 36, 33, 67, 2, 12];
console.log(arr5.sort(function(a,b){
return (a%10)-(b%10)
}))
console.log("**********************************************************")
let arr7 = ["ab", "cb", "da", "gh", "ia", "ic"];
arr7.sort((a, b) => {
let lastCharA = a[a.length - 1];
let lastCharB = b[b.length - 1];
return lastCharA > lastCharB ? 1 : lastCharA < lastCharB ? -1 : 0;
});
console.log(arr7);
console.log("**********************************************************")
/*Output
123456
**********************************************************
1,2,3,4,5,6
**********************************************************
1,2,3,4,5,6
**********************************************************
6
**********************************************************
undefined
**********************************************************
[ 1, 9, 10 ]
**********************************************************
[
10, 1, 11, 21, 2, 12,
33, 55, 5, 65, 6, 56,
36, 77, 7, 67
]
**********************************************************
[
10, 1, 11, 21, 2, 12,
33, 55, 5, 65, 6, 56,
36, 77, 7, 67
]
**********************************************************
[ 'da', 'ia', 'ab', 'cb', 'ic', 'gh' ]
*/