Skip to content

Commit 8f16acc

Browse files
committed
more draft content - short hand methods, stacks
1 parent 6be2d8e commit 8f16acc

15 files changed

+609
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* For
3+
*/
4+
const fruits = ['mango', 'peach', 'banana'];
5+
for (let fruit of fruits)
6+
console.log(fruits)
7+
8+
/**
9+
* For IN
10+
*/
11+
const obj = {continent: 'Africa', country: 'Kenya', city: 'Nairobi'}
12+
for (let key in obj)
13+
console.log(key) // output: continent, country, city
14+
15+
16+
/**
17+
* ForEach
18+
*/
19+
const logArrayElements = (element, index, array) => console.log("a[" + index + "] = " + element);
20+
[2, 5, 9].forEach(logArrayElements);
21+

00-Drafts/00-Iterables/readme.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
---
2+
theme : "night"
3+
transition: "slide"
4+
highlightTheme: "monokai"
5+
logoImg: "logo.png"
6+
slideNumber: false
7+
title: "VSCode Reveal intro"
8+
---
9+
10+
::: block
11+
*JavaScript Iterables* {style=background:red;width:500px}
12+
:::
13+
14+
15+
---
16+
17+
### ToDo List
18+
19+
- [ ] Recurvive Functions
20+
- [ ] While VS Do While Loop
21+
- [ ] For Loop
22+
- [ ] For Each
23+
- [ ] Arrays - Map, reduce, etc
24+
- [ ] For of VS For of
25+
- [ ] For Await
26+
27+
---
28+
29+
### Recurvive Functions
30+
31+
- [ ] Recurvive Functions
32+
- [ ] While VS Do While Loop
33+
- [ ] For Loop
34+
- [ ] For Each
35+
- [ ] Arrays
36+
- [ ] For of VS For of
37+
- [ ] For Await
38+
39+
---
40+
41+
### While VS Do While Loop
42+
43+
- [ ] Recurvive Functions
44+
- [ ] While VS Do While Loop
45+
- [ ] For Loop
46+
- [ ] For Each
47+
- [ ] Arrays
48+
- [ ] For of VS For of
49+
- [ ] For Await
50+
51+
---
52+
53+
### For Loop
54+
- [ ] i
55+
- [ ] sematic naming
56+
- [ ] nested loops
57+
58+
---
59+
60+
61+
### Arrays - ForEach, Map, reduce, etc
62+
63+
- [ ] Callbacks
64+
65+
---
66+
67+
### Arrays - For of VS For of
68+
69+
- [ ] For of VS For of
70+
- [ ] For Await
71+
72+
73+
---
74+
75+
### For Await
76+
77+
- [ ] Generators
78+
- [ ] For Await

00-Drafts/00-ShortHand/Loop.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* For
3+
*/
4+
const fruits = ['mango', 'peach', 'banana'];
5+
for (let fruit of fruits)
6+
console.log(fruits)
7+
8+
/**
9+
* For IN
10+
*/
11+
const obj = {continent: 'Africa', country: 'Kenya', city: 'Nairobi'}
12+
for (let key in obj)
13+
console.log(key) // output: continent, country, city
14+
15+
16+
/**
17+
* ForEach
18+
*/
19+
const logArrayElements = (element, index, array) => console.log("a[" + index + "] = " + element);
20+
[2, 5, 9].forEach(logArrayElements);
21+

00-Drafts/00-Stacks/BigO.js

Whitespace-only changes.
17.1 KB
Loading
35.1 KB
Loading
55.1 KB
Loading
62 KB
Loading

00-Drafts/00-Stacks/linkedList.js

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
/**
2+
* Articles
3+
* 1. Good Medium citation
4+
* https://codeburst.io/js-data-structures-linked-list-3ed4d63e6571
5+
*/
6+
7+
8+
function LinkedList() {
9+
10+
let Node = function(element){
11+
this.element = element;
12+
this.next = null;
13+
};
14+
15+
let length = 0;
16+
let head = null;
17+
18+
this.append = function(element){
19+
20+
let node = new Node(element),
21+
current;
22+
23+
if (head === null){ //first node on list
24+
head = node;
25+
} else {
26+
27+
current = head;
28+
29+
//loop the list until find last item
30+
while(current.next){
31+
current = current.next;
32+
}
33+
34+
//get last item and assign next to added item to make the link
35+
current.next = node;
36+
}
37+
38+
length++; //update size of list
39+
};
40+
41+
this.insert = function(position, element){
42+
43+
//check for out-of-bounds values
44+
if (position >= 0 && position <= length){
45+
46+
let node = new Node(element),
47+
current = head,
48+
previous,
49+
index = 0;
50+
51+
if (position === 0){ //add on first position
52+
53+
node.next = current;
54+
head = node;
55+
56+
} else {
57+
while (index++ < position){
58+
previous = current;
59+
current = current.next;
60+
}
61+
node.next = current;
62+
previous.next = node;
63+
}
64+
65+
length++; //update size of list
66+
67+
return true;
68+
69+
} else {
70+
return false;
71+
}
72+
};
73+
74+
this.removeAt = function(position){
75+
76+
//check for out-of-bounds values
77+
if (position > -1 && position < length){
78+
79+
let current = head,
80+
previous,
81+
index = 0;
82+
83+
//removing first item
84+
if (position === 0){
85+
head = current.next;
86+
} else {
87+
88+
while (index++ < position){
89+
90+
previous = current;
91+
current = current.next;
92+
}
93+
94+
//link previous with current's next - skip it to remove
95+
previous.next = current.next;
96+
}
97+
98+
length--;
99+
100+
return current.element;
101+
102+
} else {
103+
return null;
104+
}
105+
};
106+
107+
this.remove = function(element){
108+
109+
let index = this.indexOf(element);
110+
return this.removeAt(index);
111+
};
112+
113+
this.indexOf = function(element){
114+
115+
let current = head,
116+
index = 0;
117+
118+
while (current) {
119+
if (element === current.element) {
120+
return index;
121+
}
122+
index++;
123+
current = current.next;
124+
}
125+
126+
return -1;
127+
};
128+
129+
this.isEmpty = ()=> length === 0;
130+
131+
this.size = ()=> length;
132+
133+
this.getHead = ()=> head;
134+
135+
this.toString = function(){
136+
137+
let current = head,
138+
string = '';
139+
140+
while (current) {
141+
string += current.element + (current.next ? ', ' : '');
142+
current = current.next;
143+
}
144+
return string;
145+
146+
};
147+
148+
this.print = function(){
149+
console.log(this.toString());
150+
// console.table(this);
151+
let [...table] = this.toString()
152+
console.table(table);
153+
// console.table(Node());
154+
};
155+
}
156+
157+
let list = new LinkedList();
158+
list.append(15);
159+
list.print();
160+
console.log(list.indexOf(15));
161+
list.append(10);
162+
list.print();
163+
console.log(list.indexOf(10));
164+
list.append(13);
165+
list.print();
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
let Stack3 = (function () {
2+
3+
const items = new WeakMap();
4+
5+
class Stack3 {
6+
7+
constructor () {
8+
items.set(this, []);
9+
}
10+
11+
push(element){
12+
let s = items.get(this);
13+
s.push(element);
14+
}
15+
16+
pop(){
17+
let s = items.get(this);
18+
let r = s.pop();
19+
return r;
20+
}
21+
22+
peek(){
23+
let s = items.get(this);
24+
return s[s.length-1];
25+
}
26+
27+
isEmpty(){
28+
return items.get(this).length == 0;
29+
}
30+
31+
size(){
32+
let s = items.get(this);
33+
return s.length;
34+
}
35+
36+
clear(){
37+
items.set(this, []);
38+
}
39+
40+
print(){
41+
console.log(this.toString());
42+
}
43+
44+
toString(){
45+
return items.get(this).toString();
46+
}
47+
}
48+
49+
return Stack3;
50+
})();
51+

0 commit comments

Comments
 (0)