-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtask_3.js
59 lines (47 loc) · 1.08 KB
/
task_3.js
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
'use strict'
// forEach function
const array = [1, 2, 3, 4, 5, 6, 7]
function myForEach (arr, fn) {
for (let i = 0; i < arr.length; i++) {
fn(arr[i], i, arr)
}
}
function adder (item, index, array) {
item += item
console.log(item, index, array)
}
myForEach(array, adder)
// map function
const words = ['one', 'two', 'three', 'four', 'five']
function myMap (arr, fn) {
const newArray = []
for (let i = 0; i < arr.length; i++) {
const item = fn(arr[i], i, arr)
newArray.push(item)
}
console.log(newArray)
}
function itemLength (i) {
return i.length
}
myMap(words, itemLength)
// sort function
function mySort (array) {
let done = false
while (!done) {
done = true
for (let i = 1; i < array.length; i += 1) {
if (array[i - 1] > array[i]) {
done = false
const tmp = array[i - 1]
array[i - 1] = array[i]
array[i] = tmp
}
}
}
return array
}
const numbers = [12, 2, 10, 15, 11, 14, 13, 16]
const materials = ['tree', 'steel', 'plastic', 'wood', 'glass']
console.log(mySort(numbers))
console.log(mySort(words))