Repository navigation
Expand file tree
/
Copy patharrays.js
More file actions
246 lines (188 loc) · 7.35 KB
/
Copy patharrays.js
File metadata and controls
246 lines (188 loc) · 7.35 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// // Перебір масиву
// const array = ["Hello world", null, true, 15];
// // Цикл for
// for (let i = 0; i < array.length; i+=1){
// const item = array[i]
// console.log(item);
// }
// const c = [4, 7];
// const d = [5];
// const e = c; // константа e буде мати посилання на тей самий масив, що і константа c
// console.log(c === d); // false
// console.log(c[0] === d[0]); // true -> 5 === 5 -> true
// console.log(c === e); // true
// e[0] = 55;
// console.log(e, c);
// const arrCopy = c.slice(0);
// arrCopy[0] = 100;
// console.log(arrCopy, c);
// const numbers = [1, 2, 3];
// numbers.push(4, 5, 6, 7);
// console.log(numbers);
// numbers.pop();
// console.log(numbers);
// numbers.push(4, 5, 6, 7);
// console.log(numbers);
// numbers.pop();
// console.log(numbers);
// unshift(додає один або кілька елементів на початок масиву) shift(видаляє один елемент з початку масиву)
// const numbers = [1, 2, 3];
// console.log(numbers.unshift(-3, -2, -1, 0));
// console.log(numbers);
// console.log(numbers.shift());
// console.log(numbers)
// console.log(numbers.unshift(-3, -2, -1, 0));
// console.log(numbers);
// console.log(numbers.shift())
// slice(start, end) - повертає новий масив, з скопійованими елементами починаючи з індексу start і до індексу end(не включно)
// const fruits = ["apple", "pineapple", "cherry", "banana"];
// const newFruits = fruits.slice(1, 3);
// const lastTwoFruits = fruits.slice(-2);
// console.log(newFruits);
// console.log(lastTwoFruits);
// const newFruits = fruits.slice(1, 3);
// const lastTwoFruits = fruits.slice(-2);
// console.log(newFruits);
// console.log(lastTwoFruits);
//splice(start, deleteCount, ...insert) - мутує масив, вирізаючи з нього deleteCount елементів починаючи з індексу start. Також, можна вставити якість нові елементи замість видалених в insert
// const fruits = ["apple", "pineapple", "cherry", "banana"];
// fruits.splice(2, 1, "kavun");
// console.log(fruits)
// fruits.splice(2, 1, "orange", "lemon")
// console.log(fruits)
// includes(item) - перевіряє чи є item всередині масиву, якщо так - повертає true, якщо ні - повертає false
// const fruits = ["apple", "pineapple", "cherry", "banana"];
// console.log(fruits.includes("banana"))
// console.log(fruits.includes("orange"))
// const fruits = ["apple", "pineapple", "cherry", "banana"];
// const index = fruits.indexOf("cherry")
// console.log(index)
// fruits.splice(index, 2)
// console.log(fruits)
// console.log(fruits.indexOf("pineapple"))
// concat(...arrays) - повертає новий масив, у якому будуть всі елементи масивів arrays
// const fruits = ["apple", "pineapple"];
// const berries = ["cherry", "watermelon"];
// const allItems = fruits.concat(berries);
// console.log(allItems);
// Task-1
// Напиши скрипт який буде перебирати масив та видаляти з нього (мутувати) всі елементи що не є типом даних Number.
// const arr = [3, "Hello", null, 42, false];
// for (let i = 0; i < arr.length; i += 1){
// if (typeof arr[i] !== "number") {
// arr.splice(i, 1);
// i -= 1;
// }
// }
// console.log(arr)
// Task-2
// Потрібно створити функцію яка буде приймати 1 параметр
// Функція повина відібрати з масиву тільки ті елементи що дублюються в ньому та повернути їх в вигляді нового масиву як результат виконання функції
// function getCommonElements(arr) {
// const duplicates = [];
// for (let i = 0; i < arr.length; i += 1){
// if (arr.includes(arr[i], i += 1)) {
// duplicates.push(arr[i]);
// }
// }
// return duplicates;
// }
// console.log(getCommonElements([1, 2, 3, 2, 17, 1, 17, 2, 19]));
// function slugify(title) {
// // Change code below this line
// let words = title.toLowerCase;
// let titleWords = words.split(" ");
// let slug = titleWords.join("-")
// // Change code above this line
// }
// function makeArray(firstArray, secondArray, maxLength) {
// let result
// const newArray = firstArray.concat(secondArray);
// if (newArray.length > maxLength) {
// result = newArray.slice(0, maxLength);
// }
// else {
// return newArray;
// }
// return result;
// }
// let finish = makeArray(["Mango", "Poly"], ["Ajax", "Chelsea"], 3);
// console.log(finish)
// finish = makeArray(["Mango", "Poly", "Houston"], ["Ajax", "Chelsea"], 4);
// console.log(finish)
// finish = makeArray(["Mango"], ["Ajax", "Chelsea", "Poly", "Houston"], 3);
// console.log(finish)
// finish = makeArray(["Earth", "Jupiter"], ["Neptune", "Uranus"], 2);
// console.log(finish)
// finish = makeArray(["Earth", "Jupiter"], ["Neptune", "Uranus"], 4);
// console.log(finish)
// finish = makeArray(["Earth", "Jupiter"], ["Neptune", "Uranus", "Venus"], 0);
// console.log(finish)
// for (let i = 0; i <= 20; i += 5) {
// console.log(i);
// }
// function calculateTotal(number) {
// let totalSum = 0;
// for (i = 1; i <= number; i += 1) {
// const newSum = i + totalSum;
// totalSum = newSum;
// }
// return totalSum;
// }
// const planets = ["Earth", "Mars", "Venus"];
// for (let i = 0; i < planets.length; i += 1) {
// console.log(planets[i]);
// }
// function calculateTotalPrice(order) {
// let total = 0;
// for (let i = 0; i < order.length; i += 1) {
// const newTotal = order[i] + total;
// total = newTotal;
// }
// return total;
// }
// let result = calculateTotalPrice([12, 85, 37, 4]);
// console.log(result)
// function findLongestWord(string) {
// let newArray = string.split(" ");
// let wordLength = 0;
// let longestWord;
// console.log(newArray);
// for (let i = 0; i < newArray.length; i += 1) {
// wordLength = newArray[i]
// if (newArray[i].length > wordLength) {
// longestWord = newArray[i];
// return longestWord;
// }
// }
// }
//Пошук найдовшого слова!!!!!
// function findLongestWord(string) {
// // Change code below this line
// let arrStr = string.split(' ');
// let wordLength = 0;
// let longestWord;
// for (let i = 0; i < arrStr.length; i += 1) {
// wordLength = arrStr[1].length;
// if (arrStr[i].length > wordLength) {
// longestWord = arrStr[i];
// return longestWord;
// }
// }
// }
// let result = findLongestWord("The quick brown fox jumped over the lazy dog");
// console.log(result);
// result = findLongestWord("Google do a roll");
// console.log(result);
// result = findLongestWord("May the force be with you");
// console.log(result);
// result = findLongestWord("ffhfhfhfh jf joro");
// console.log(result);
// const clients = ["Mango", "Ajax", "Poly"];
// for (const client of clients) {
// console.log(client);
// }
// const string = "javascript";
// for (const character of string) {
// console.log(character);
// }