-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrayexercise.js
More file actions
63 lines (47 loc) · 1.3 KB
/
Copy patharrayexercise.js
File metadata and controls
63 lines (47 loc) · 1.3 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
// ex1//////
// const numbers = arFrRange(-1, 4);
// console.log(numbers);
// function arFrRange(min, max) {
// const output = [];
// for (let i = min; i <= max; i++)
// output.push(i);
// return output;
// }
// // ex2/////
// const numbs = [1, 2, 3, 4];
// // console.log(numbs.includes(2));
// function includes(array, searchIndex) {
// for (let key of array) {
// if (array[key] === searchIndex)
// return true;
// return false;
// }
// }
// console.log(includes(numbs, 5));
// ex3////////
// const num = [1, 2, 3, 4, 1, 1, 1];
// const output = except(num, [1]);
// console.log(output);
// function except(array, excluded) {
// let output = [];
// for (let element of array)
// if (!excluded.includes(element))
// output.push(element);
// return output;
// }
// ex4/////
const num = [1, 2, 3, 4];
const output = move(num, 1, 2);
console.log(output);
function move(array, index, offset) {
// swap
const position = index + offset;
if (position >= array.length || position < 0) {
console.error('invalid')
return; // none of code aage will run
}
let output = [...array];
const element = output.splice(index, 1)[0];
output.splice(index + offset, 0, element);
return output;
}