forked from loiane/javascript-datastructures-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path03-AddingRemovingElements.js
executable file
·105 lines (80 loc) · 2.8 KB
/
03-AddingRemovingElements.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
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
// @ts-check
/* eslint-disable */
function logArray(message = '', array) {
const output = `${message} => (${array.length}) [${array.join(', ')}]`;
document.writeln(`<p>${output}</p>`);
console.log(message, array);
}
let numbers = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
console.log('Original array', numbers);
function printArray(myArray) {
for (let i = 0; i < myArray.length; i++) {
console.log(myArray[i]);
}
}
/****************************** Inserting an element in the end of the Array */
// add a new element to the numbers array
numbers[numbers.length] = 10;
console.log('Add 10 to the end', numbers);
numbers.push(11);
console.log('Add 11 with push', numbers);
numbers.push(12, 13);
console.log('Add 12 and 13 with push', numbers);
// insert first position manually
Array.prototype.insertFirstPosition = function(value) {
for (let i = this.length; i >= 0; i--) {
this[i] = this[i - 1];
}
this[0] = value;
};
numbers.insertFirstPosition(-1);
console.log('Add -1 with insertFirstPosition', numbers);
// printArray(numbers);
// using method unshift
numbers.unshift(-2);
console.log('Add -2 with unshift', numbers);
//printArray(numbers);
numbers.unshift(-4, -3);
console.log('Add -4 and -3 with unshift', numbers);
// printArray(numbers);
// ************** Removing elements
numbers.pop();
console.log('Removed last value with pop', numbers);
for (let i = 0; i < numbers.length; i++) {
numbers[i] = numbers[i + 1];
}
console.log('Removed first value manually', numbers);
console.log('Lenght after value removed manually', numbers.length);
//reset array
numbers = [-4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
Array.prototype.reIndex = function(myArray) {
const newArray = [];
for(let i = 0; i < myArray.length; i++ ) {
if (myArray[i] !== undefined) {
// console.log(myArray[i]);
newArray.push(myArray[i]);
}
}
return newArray;
}
// remove first position manually and reIndex
Array.prototype.removeFirstPosition = function() {
for (let i = 0; i < this.length; i++) {
this[i] = this[i + 1];
}
return this.reIndex(this);
};
numbers = numbers.removeFirstPosition();
console.log('Removed first with removeFirstPosition + reIndex', numbers);
// using method shift
numbers.shift();
console.log('Removed first with shift', numbers);
console.log('Lenght after removed first with shift', numbers.length);
//* *** Removing and Adding elements from the middle of the array or specific position
// splice method - parameter (index, howManyPositionsToBeRemoved, item1...itemX)
numbers.splice(5, 3);
console.log('Removing 3 elements (3, 4, 5) starting index 5', numbers);
numbers.splice(5, 0, 2, 3, 4);
console.log('Adding 3 elements (2, 3, 4) starting index 5', numbers);
numbers.splice(5, 3, 2, 3, 4);
console.log('Removing 3 elements starting index 5 and adding (2, 3, 4)', numbers);