-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest4.js
More file actions
170 lines (161 loc) · 3.84 KB
/
Copy pathtest4.js
File metadata and controls
170 lines (161 loc) · 3.84 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//Bai:
let arr = [3, 451, 5, 2, 6, 76, 3, 5, 1, 243, 6, 4]
//Vong lap for:
// let doDaiMang = arr.length
// for (let i = 0; i < doDaiMang; i = i + 1){
// console.log('Gia tri mang %s = %s', i, arr[i])
// }
// //Dao nguoc mang va coi so lan xuat hien trong mang
// let arr1 = []
// let j = 0
// for (let i = doDaiMang - 1; i>=0; i--) {
// console.log('arr[%s] =', i, arr[i])
// arr1[j] = arr[i]
// j++
// }
// console.log(arr1)
// soLanXuatHien = {}
// for (let i = 0; i<doDaiMang; i++) {
// const phanTuthu1 = arr[i]
// soLanXuatHien[phanTuthu1] = (soLanXuatHien[phanTuthu1] || 0) + 1
// }
// for (const key in soLanXuatHien) {
// console.log(`So ${key} xuat hien ${soLanXuatHien[key]} lan`)
// }
//Vong lap while:
// let i = 0
// while (arr[i] != undefined) {
// console.log('Gia tri mang %s = %s', i, arr[i])
// i++
// }
// //bd)
// let tong = 0
// let tbc = 0
// while (arr[i] !== undefined) {
// tbc = tong / i
// tong = tong + arr[i]
// i++
// }
// console.log('Tong bang = ', tong)
// console.log('Trung binh cong bang = ', tbc)
// //c)
// let phanTuLonNhat = arr[0]
// let phanTuNhoNhat = arr[0]
// i = 0
// while (arr[i] !== undefined) {
// if (phanTuLonNhat < arr[i]) {
// phanTuLonNhat = arr[i]
// }
// if (phanTuNhoNhat > arr[i]) {
// phanTuNhoNhat = arr[i]
// }
// i++
// }
// console.log('Phan tu lon nhat: ', phanTuLonNhat)
// console.log('Phan tu nho nhat: ', phanTuNhoNhat)
// //e)
// i = 0
// let j = arr.length - 1
// let temp = 0
// while ( i< j) {
// i++; j--
// temp = arr[i]
// arr[j] = temp
// arr[i] = arr[j]
// }
// // console.log(arr)
// //f)
// i = 0
// const soLanXuatHienn = {}
// while ( arr[i] != undefined ){
// const phanTuTrongArr = arr[i]
// soLanXuatHienn[phanTuTrongArr] = (soLanXuatHienn[phanTuTrongArr] || 0) + 1
// i++
// }
// for (const key in soLanXuatHienn) {
// console.log(`So ${key} xuat hien ${soLanXuatHienn[key]} lan`)
// }
// //g)
// i = arr.length - 1
// while(arr[i] !== undefined) {
// arr[i + 1] = arr[i]
// i = i - 1
// }
// arr[0] = 117
// console.log('==>',arr)
// console.log(arr.length)
// //h)
// function mangTangDan(arr){
// let checkTangDan = true
// i = 0
// while (i< arr.length) {
// if (arr[i] > arr[i + 1]) {
// checkTangDan = false
// }
// i++
// }
// if (checkTangDan == true){
// console.log('Mang tang dan')
// }
// else{console.log('Mang khong tang dan')}
// }
// mangTangDan(arr)
// //i)
i = 0
function dumamay(arr){
while(arr[i] != undefined) {
let j = i+ 1
while (arr[j] != undefined) {
if (arr[i] > arr[j]) {
let tam = arr[i]
arr[i] = arr[j]
arr[j] = tam
}
j = j + 1
}
i = i + 1
console.log(arr)
}
}
dumamay(arr)
// //k
// i = 0
// while(arr[i] != undefined) {
// if (arr[i]%2==0) {
// console.log('So chan', arr[i])
// }
// i++
// }
//Vong lap do While:
// arr = [3, 451, 5, 2, 6, 76, 3, 5, 1, 243, 6, 4]
// i = 0
// do {
// console.log('Gia tri mang %s = %s', i, arr[i])
// i++
// }while (arr[i] !== undefined)
// b,d))
// let tong = 0
// let tbc = 0
// i = 0
// do {
// tbc = tong / i
// tong = tong + arr[i]
// i++
// console.log('Tong bang = ', tong)
// console.log('Trung binh cong bang = ', tbc)
// }while (arr[i] !== undefined)
// c))
// let phanTuLonNhat = arr[0]
// let phanTuNhoNhat = arr[0]
// i = 0
// do {
// if (phanTuLonNhat < arr[i]) {
// phanTuLonNhat = arr[i]
// }
// if (phanTuNhoNhat > arr[i]) {
// phanTuNhoNhat = arr[i]
// }
// i++
// }while (arr[i] !== undefined)
// console.log('Phan tu lon nhat: ', phanTuLonNhat)
// console.log('Phan tu nho nhat: ', phanTuNhoNhat)