-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask1.js
More file actions
147 lines (120 loc) · 3.25 KB
/
Copy pathtask1.js
File metadata and controls
147 lines (120 loc) · 3.25 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
/**
* Returns the rectagle object with width and height parameters and getArea() method
*
* @param {number} width
* @param {number} height
* @return {Object}
*
* @example
* var r = new Rectangle(10,20);
* console.log(r.width); // => 10
* console.log(r.height); // => 20
* console.log(r.getArea()); // => 200
*/
function Rectangle(width, height) {
this.width = width;
this.height = height;
Rectangle.prototype.getArea = function () {
return this.width * this.height;
};
}
const rect1 = new Rectangle(5, 10);
console.log(rect1.getArea());
console.log(rect1.width);
console.log(rect1.height);
/**
* Returns the n last items of the specified arrays
*
* @param {array} arr
* @param {number} n
*
* @example
* [ 1, 3, 4, 5 ], 2 => [ 4, 5 ]
* [ 'a', 'b', 'c', 'd'], 3 => [ 'b', 'c', 'd' ]
*/
function getTail(arr, n) {
return arr.slice(-n);
}
console.log(getTail([1, 2, 3, 4, 5, 6, 8, 9], 3));
console.log(getTail([1, 3, 4, 5], 2));
/**
* Inserts the item into specified array at specified index
*
* @param {array} arr
* @param {any} item
* @param {number} index
*
* @example
* [ 1, 3, 4, 5 ], 2, 1 => [ 1, 2, 3, 4, 5 ]
* [ 1, 'b', 'c'], 0, 'x' => [ 'x', 1, 'b', 'c' ]
*/
function insertItem(arr, item, index) {
arr.splice(index, 0, item);
return arr;
}
console.log(insertItem([1, 3, 4, 5], 2, 1));
console.log(insertItem([1, 'b', 'c'], 'x', 0));
/**
* Returns an array of positive numbers from the specified array in original order
*
* @param {array} arr
* @return {array}
*
* @example
* [ 0, 1, 2, 3, 4, 5 ] => [ 1, 2, 3, 4, 5 ]
* [-1, 2, -5, -4, 0] => [ 2 ]
* [] => []
*/
function getArrayOfPositives(arr) {
let arrPositive = arr.filter(function(item){
return item > 0;
});
console.log(arrPositive);
}
getArrayOfPositives([0, 1, 2, -3, 4, 5]);
getArrayOfPositives([-1, 2, -5, -4, 0]);
/**
* Returns array containing only unique values from the specified array.
*
* @param {array} arr
* @return {array}
*
* @example
* [ 1, 2, 3, 3, 2, 1 ] => [ 1, 2, 3 ]
* [ 'a', 'a', 'a', 'a' ] => [ 'a' ]
* [ 1, 1, 2, 2, 3, 3, 4, 4] => [ 1, 2, 3, 4]
*/
function distinct(arr) {
// let arrDistinct = arr.filter(item => !arrDistinct.includes(item));
// let arrDistinct = arr.filter(function(item){
// return !arrDistinct.includes(item);
// });
// console.log(arrDistinct);
let arrDistinct = [];
arr.forEach(function (item) {
if (arrDistinct.length === 0) {
arrDistinct.push(item);
};
if (!arrDistinct.includes(item)) {
arrDistinct.push(item);
};
});
return arrDistinct;
}
distinct(['a', 'a', 'a', 'a']);
distinct([1, 1, 2, 2, 3, 3, 4, 4]);
const div = document.body.appendChild(document.createElement('div'));
const but1 = div.appendChild(document.createElement('button'));
but1.innerText = "+";
const div2 = div.appendChild(document.createElement('div'));
div2.innerText = 0;
const but2 = div.appendChild(document.createElement('button'));
but2.innerText = "-";
but1.addEventListener('click', plus);
but2.addEventListener('click', minus);
function plus() {
div2.innerText = +div2.innerText + 1;
}
function minus() {
div2.innerText = +div2.innerText - 1;
}