-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_structures_tutorial.js
More file actions
122 lines (99 loc) · 2.78 KB
/
data_structures_tutorial.js
File metadata and controls
122 lines (99 loc) · 2.78 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
// Basic Data Structures
let firstArr = ['first', 'second', 'third'];
firstArr.unshift('zero');
firstArr.push('fourth');
console.log(firstArr); // zero, first, second, third, fourth
firstArr.pop();
console.log(firstArr); // zero, first, second, third
let shifted = firstArr.shift();
console.log(shifted); // zero
console.log(firstArr); // first, second, third
let spliceArr = ['This', 'array', 'has', 'some', 'extra', 'words'];
let removedWord = spliceArr.splice(4, 1);
console.log(spliceArr); // This, array, has, some, words
console.log(removedWord); // extra
function sumRemaining(arr) {
arr.splice(2,2);
return arr.reduce((a, b) => a + b);
}
// indexOf
let fruits = ['apples', 'pears', 'oranges', 'peaches', 'pears'];
fruits.indexOf('dates') // -1, does not exist in array
fruits.indexOf('oranges') // 2
fruits.indexOf('pears') // 1, the first index at which the element exists
function quickCheck(arr, elem) {
return arr.indexOf(elem);
}
// const quickCheck = (arr, elem) => arr.indexOf(elem);
console.log(quickCheck(['squash', 'onions', 'shallots'], 'mushrooms'));
// combine arrays with spread operator
let thisArray = ['sage', 'rosemary', 'parsley', 'thyme'];
let thatArray = ['basil', 'cilantro', ...thisArray, 'coriander'];
console.log(thatArray); // ['basil', 'cilantro', 'sage', 'rosemary', 'parsley', 'thyme', 'coriander']
function spreadOut() {
let fragment = ['to', 'code'];
let sentence = ['learning', 'how', ...fragment, 'is', 'fun'];
return sentence;
}
console.log(spreadOut()); // learning,how,to,code,is,fun
let users = {
Alan: {
age: 27,
online: false
},
Jeff: {
age: 32,
online: true
},
Sarah: {
age: 48,
online: false
},
Ryan: {
age: 19,
online: true
}
};
for (let user in users) {
console.log(user);
};
// Alan, Jeff, Sarah, Ryan
function whoIsOnline(obj) {
let onlineUsers = 0;
for(let item in obj) {
if(obj[item].online == true)
onlineUsers++;
}
return onlineUsers;
}
// Linked List
function LinkedList() {
this.head = null;
this.tail = null;
}
function Node(value, next, previous) {
this.value = value;
this.next = next;
this.previous = previous;
}
LinkedList.prototype.addToHead = function() {
var newNode = new Node(value, this.head, null);
this.head != null ? this.head.prev = newNode : this.tail = newNode;
this.head = newNode;
};
LinkedList.prototype.addToTail = function() {
var newNode = new Node(value, null, this.tail);
this.tail != null ? this.tail.next = newNode : this.head = newNode;
this.tail = newNode;
}
var ll = new LinkedList();
ll.addToHead(50);
console.log(ll);
// { head: { value: 100, next: null, prev: null},
// tail: { value: 100, next: null, prev: null} }
ll.addToHead(5);
ll.addToHead(200);
ll.addToHead(150);
ll.addToTail(25);
ll.addToTail(10);
console.log(ll);