├── README.md
├── binary-search-tree.js
├── binary-search.js
├── bubble-sort.js
├── games
└── battle-ship.js
├── image-display
├── IMG_0330.jpg
├── IMG_0366.jpg
├── IMG_0873.jpg
├── IMG_1108.jpg
├── IMG_1588.jpg
├── house-front.png
├── index.html
└── index.js
├── invert-binary-tree.js
├── parentheses.js
├── queue.js
├── quick-sort.js
├── random-stuff.js
├── selection-sort.js
├── singly-linked-list.js
├── something.js
├── somethingelse.js
└── stack.js
/README.md:
--------------------------------------------------------------------------------
1 | # js-cs-notes
2 | A list of notes and CS exercises in javascript. meant to improve my own knowledge.
3 |
4 | This is just a fun way to learn more about a great language.
5 |
--------------------------------------------------------------------------------
/binary-search-tree.js:
--------------------------------------------------------------------------------
1 | // Binary search tree
2 | //
3 |
4 | let TreeNode = {
5 | val: null,
6 | left: null,
7 | right: null,
8 | };
9 |
10 | class BinarySearchTree {
11 | constructor() {
12 | this._root = null;
13 | }
14 |
15 | add(val) {
16 | let newNode = Object.create(TreeNode);
17 | newNode.val = val;
18 |
19 | if (this._root == null){
20 | this._root = newNode;
21 | return newNode;
22 | }
23 |
24 | (function walkNodes(reference) {
25 | if (newNode.val >= reference.val) {
26 | if (reference.right){
27 | walkNodes(reference.right);
28 | } else {
29 | reference.right = newNode;
30 | return newNode;
31 | }
32 | } else {
33 | if (reference.left){
34 | walkNodes(reference.left);
35 | } else {
36 | reference.left = newNode;
37 | return newNode;
38 | }
39 | }
40 | })(this._root);
41 | }
42 |
43 | find(val){
44 | let found = null;
45 |
46 | (function findNode(reference) {
47 | if (val === reference.val) {
48 | found = reference;
49 | return;
50 | }
51 |
52 | if (val > reference.val) {
53 | findNode(reference.right);
54 | } else {
55 | findNode(reference.left);
56 | }
57 | })(this._root);
58 |
59 | return found;
60 | }
61 | }
62 |
63 | // let newTree = new BinarySearchTree();
64 | // newTree.add(5);
65 | // newTree.add(10);
66 | // newTree.add(1);
67 | // newTree.add(2);
68 | // let foundNode = newTree.find(2);
69 | // console.log(newTree);
70 | // console.log(foundNode);
71 |
--------------------------------------------------------------------------------
/binary-search.js:
--------------------------------------------------------------------------------
1 | //Binary search should quickly search an array in O(log N) fashion
2 | //
3 |
4 | function binarySearch(array, val){
5 | 'use strict';
6 |
7 | let minimumIndex = 0;
8 | let maxIndex = array.length - 1;
9 | let currentIndex;
10 |
11 | (function search(){
12 | currentIndex = Math.floor((minimumIndex + maxIndex) / 2);
13 |
14 | console.log(`curently looking between ${array[minimumIndex]} and ${array[maxIndex]}`);
15 |
16 | if (array[currentIndex] === val ){
17 | console.log(`FOUND IT! ${val} == ${array[currentIndex]}`);
18 | return;
19 | }
20 |
21 | if (val > array[currentIndex]){
22 | minimumIndex = currentIndex + 1;
23 | }
24 |
25 | if (val < array[currentIndex]){
26 | maxIndex = currentIndex -1;
27 | }
28 |
29 | search();
30 | })();
31 | }
32 |
33 | let arr = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97];
34 | binarySearch(arr, 5);
35 | binarySearch(arr, 3);
36 | binarySearch(arr, 59);
37 | binarySearch(arr, 61);
38 | binarySearch(arr, 7);
39 |
--------------------------------------------------------------------------------
/bubble-sort.js:
--------------------------------------------------------------------------------
1 | //Bubble sort to sort an array. O(n) complexity at best. O(n^2) at worst
2 | //
3 |
4 | function bubbleSort(arr){
5 | 'use strict';
6 | for(let i = arr.length-1; i >= 0; i--){
7 | console.log(`PASSES LEFT ${i}`);
8 | for (let j = 0; j<=i; j++){
9 | console.log(arr);
10 | if (arr[j+1] < arr[j]){
11 | let temp = arr[j+1];
12 | arr[j+1] = arr[j];
13 | arr[j] = temp;
14 | }
15 | }
16 | }
17 |
18 | return arr;
19 | }
20 |
21 | console.log(bubbleSort([1,3,6,5,2,7,8]));
22 | console.log(bubbleSort([1,4,6,5,20,19,3]));
23 |
--------------------------------------------------------------------------------
/games/battle-ship.js:
--------------------------------------------------------------------------------
1 | // fun little game for someone to play by themselves when they are lonely
2 | //
3 |
4 | class Ship {
5 | constructor(size, coordinates){
6 | this.size = size;
7 | this.coordinates = coordinates;
8 | this.hitCount = 0;
9 | }
10 |
11 | hit(coords){
12 | for (let i = 0; i < this.coordinates.length; i++){
13 | if (this.coordinates[i].join() === coords.join()){
14 | console.log(`You hit me at ${coords}!!`);
15 | this.hitCount++;
16 | this.checkIfDead();
17 | }
18 | }
19 | }
20 |
21 | checkIfDead(){
22 | if (this.isDead()){
23 | console.log("YOU GOT ME, I'M SUNK!!!");
24 | }
25 | }
26 |
27 | isDead(){
28 | return this.hitCount === this.size ? true : false;
29 | }
30 | }
31 |
32 | class Game {
33 | constructor(ships){
34 | this.ships = ships;
35 | this.hits = [];
36 | }
37 |
38 | hit(coords){
39 | this.errorIfAlreadyHit(coords);
40 | this.ships.forEach(function(ship){
41 | ship.hit(coords);
42 | });
43 | this.hits.push(coords);
44 | this.endGameIfAllShipsAreDead();
45 | }
46 |
47 | endGameIfAllShipsAreDead(){
48 | this.ships.forEach(function(ship){
49 | if (!ship.isDead()){
50 | return true;
51 | }
52 |
53 | throw new Error("All ships are dead, GG");
54 | });
55 | }
56 |
57 | errorIfAlreadyHit(coords){
58 | for (let i = 0; i < this.hits.length; i++){
59 | if (this.hits[i].join() === coords.join()){
60 | throw new Error("You already struck that, go again");
61 | }
62 | }
63 | }
64 | }
65 |
66 | //Console commands after loading this in
67 | let ship = new Ship(2, [[1,1],[1,2]]);
68 | let game = new Game([ship]);
69 |
70 | game.hit([1,1]); //Hits the ship once
71 | game.hit([1,0]); //Hits nothing
72 | game.hit([1,2]); //Hits the ship one last time. sinking it and ending game. GG
73 |
--------------------------------------------------------------------------------
/image-display/IMG_0330.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cpow/js-cs-notes/5c05057fa0115b0c537067de64b952c2b545181a/image-display/IMG_0330.jpg
--------------------------------------------------------------------------------
/image-display/IMG_0366.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cpow/js-cs-notes/5c05057fa0115b0c537067de64b952c2b545181a/image-display/IMG_0366.jpg
--------------------------------------------------------------------------------
/image-display/IMG_0873.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cpow/js-cs-notes/5c05057fa0115b0c537067de64b952c2b545181a/image-display/IMG_0873.jpg
--------------------------------------------------------------------------------
/image-display/IMG_1108.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cpow/js-cs-notes/5c05057fa0115b0c537067de64b952c2b545181a/image-display/IMG_1108.jpg
--------------------------------------------------------------------------------
/image-display/IMG_1588.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cpow/js-cs-notes/5c05057fa0115b0c537067de64b952c2b545181a/image-display/IMG_1588.jpg
--------------------------------------------------------------------------------
/image-display/house-front.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cpow/js-cs-notes/5c05057fa0115b0c537067de64b952c2b545181a/image-display/house-front.png
--------------------------------------------------------------------------------
/image-display/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 | SUP
10 |
11 |
![]()
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/image-display/index.js:
--------------------------------------------------------------------------------
1 | // This is an example of a hadmade image slideshow viewer-thing
2 | // used circular doubly linked-list to make it customizable
3 | // (user can add images into the structure as they please)
4 | // whole structure is loaded on window load. makes going next & previous super
5 | // simple.
6 | (function(){
7 | 'use strict';
8 |
9 | var IMAGES = [
10 | 'IMG_0330.jpg',
11 | 'IMG_0366.jpg',
12 | 'IMG_0873.jpg',
13 | 'IMG_1108.jpg',
14 | 'IMG_1588.jpg',
15 | 'house-front.png',
16 | ];
17 |
18 | var List = function(){
19 | this._root = null;
20 | this.currentImage = null;
21 | };
22 |
23 | List.prototype.add = function(url){
24 | if (this._root === null){
25 | this._root = new Object({
26 | url: url,
27 | nextImage: null,
28 | previousImage: null,
29 | });
30 |
31 | this._root.nextImage = this._root;
32 | this._root.previousImage = this._root;
33 | return this._root;
34 | }
35 |
36 | var currentRoot = this._root;
37 |
38 | (function walkNodes(node){
39 | if (node.nextImage === currentRoot){
40 | var newNode = new Object({
41 | url: url,
42 | nextImage: currentRoot,
43 | previousImage: node,
44 | });
45 |
46 | node.nextImage = newNode;
47 | currentRoot.previousImage = newNode;
48 | } else {
49 | walkNodes(node.nextImage);
50 | }
51 | })(this._root);
52 | };
53 |
54 |
55 | var list = new List();
56 |
57 | IMAGES.forEach(function(url){
58 | list.add(url);
59 | });
60 |
61 | window.onload = function(){
62 | var element = document.getElementById("current-image");
63 | var nextButton = document.getElementById("next-btn");
64 | var prevButton = document.getElementById("prev-btn");
65 |
66 | var currentNode = list._root;
67 | element.src = currentNode.url;
68 |
69 | nextButton.addEventListener('click', function(){
70 | currentNode = currentNode.nextImage;
71 | element.src = currentNode.url;
72 | });
73 |
74 | prevButton.addEventListener('click', function(){
75 | currentNode = currentNode.previousImage;
76 | element.src = currentNode.url;
77 | });
78 | };
79 | })();
80 |
--------------------------------------------------------------------------------
/invert-binary-tree.js:
--------------------------------------------------------------------------------
1 | //inverting a binary tree... because why not
2 | //
3 |
4 | function invert(currentRoot){
5 | if (currentRoot == null){
6 | return;
7 | }
8 |
9 | this.invert(currentRoot.right);
10 | this.invert(currentRoot.left);
11 |
12 | if (currentRoot.left && currentRoot.right){
13 | let temp = currentRoot.left;
14 | currentRoot.left = currentRoot.right;
15 | currentRoot.right = temp;
16 | }
17 |
18 | return currentRoot;
19 | }
20 |
21 | let tree = {
22 | val: 1,
23 | right: {
24 | val: 3,
25 | right: {
26 | val: 7
27 | },
28 | left: {
29 | val: 6,
30 | }
31 | },
32 | left: {
33 | val: 2,
34 | right: {
35 | val: 5,
36 | },
37 | left: {
38 | val: 4,
39 | }
40 | }
41 | };
42 |
43 | // console.log(tree);
44 | console.log(invert(tree));
45 |
--------------------------------------------------------------------------------
/parentheses.js:
--------------------------------------------------------------------------------
1 | //Quick program to check if parentheses are balanced in a string
2 | //
3 |
4 | function parenthesesChecker(string) {
5 | let types = {
6 | '[': {position: 'open', type: 'brace'},
7 | ']': {position: 'closed', type: 'brace'},
8 | '{': {position: 'open', type: 'curly'},
9 | '}': {position: 'closed', type: 'curly'},
10 | '(': {position: 'open', type: 'parentheses'},
11 | ')': {position: 'closed', type: 'parentheses'},
12 | };
13 |
14 | let currentStack = [];
15 |
16 | for(let i = 0; i < string.length; i++){
17 | let currentBrace = string[i];
18 | let braceInfo = types[currentBrace];
19 |
20 | if (braceInfo.position === 'closed'){
21 | let lastBrace = currentStack.pop();
22 |
23 | if (!(lastBrace)){
24 | return false;
25 | }
26 | if (!(lastBrace.position === 'open' && lastBrace.type === braceInfo.type)){
27 | return false;
28 | }
29 |
30 | } else {
31 | currentStack.push(braceInfo);
32 | }
33 | }
34 |
35 | return !currentStack.length;
36 | }
37 |
38 | console.log(parenthesesChecker("{}()"));
39 | console.log(parenthesesChecker("{})"));
40 | console.log(parenthesesChecker("[]{}()"));
41 | console.log(parenthesesChecker("[{}]()"));
42 |
--------------------------------------------------------------------------------
/queue.js:
--------------------------------------------------------------------------------
1 | // Queue is a linear data structure like a Stack that only deletes the oldest
2 | // added data
3 |
4 | var Queue = function() {
5 | var self = {
6 | oldestIndex: 1,
7 | newestIndex: 1,
8 | storage: {},
9 | size: function(){
10 | return self.newestIndex - self.oldestIndex;
11 | },
12 | enqueue: function(data){
13 | self.storage[self.newestIndex] = data;
14 | self.newestIndex++;
15 | },
16 | dequeue: function(){
17 | if (self.size() > 0){
18 | console.log(self.storage[self.oldestIndex]);
19 | delete self.storage[self.oldestIndex];
20 | ++self.oldestIndex;
21 | } else {
22 | return;
23 | }
24 | },
25 | inspect: function(){
26 | return {
27 | oldestIndex: self.oldestIndex,
28 | newestIndex: self.newestIndex,
29 | storage: self.storage
30 | };
31 | }
32 | };
33 |
34 | return self;
35 | };
36 |
37 | // Queue.prototype.size = function(){
38 | // return this.newestIndex - this.oldestIndex;
39 | // };
40 | //
41 | // Queue.prototype.enqueue = function(data){
42 | // this.storage[this.newestIndex] = data;
43 | // this.newestIndex++;
44 | // };
45 | //
46 | // Queue.prototype.dequeue = function(){
47 | // if (this.size() > 0){
48 | // console.log(this.storage[this.oldestIndex]);
49 | // delete this.storage[this.oldestIndex];
50 | // ++this.oldestIndex;
51 | // } else {
52 | // return;
53 | // }
54 | // };
55 |
56 | var something = Object.create(new Queue());
57 | something.enqueue(4);
58 | something.enqueue(5);
59 | something.dequeue();
60 | console.log(something.inspect());
61 | console.log(something.size());
62 |
63 |
64 | something.dequeue();
65 | console.log(something.size());
66 |
67 | something.dequeue();
68 | console.log(something);
69 |
--------------------------------------------------------------------------------
/quick-sort.js:
--------------------------------------------------------------------------------
1 | // sorting algorithm with O(nlogn) complexity
2 |
3 | function uniques(arr){
4 | let uniqueElements = {};
5 |
6 | for (let i = 0; i < arr.length; i++){
7 | }
8 | };
9 |
--------------------------------------------------------------------------------
/random-stuff.js:
--------------------------------------------------------------------------------
1 | const something = "blah";
2 | console.log(something[0]);
3 |
--------------------------------------------------------------------------------
/selection-sort.js:
--------------------------------------------------------------------------------
1 | //Selection sort. sorting algorithm with O(n^2) complexity always
2 |
3 | function selectionSort(arr){
4 | for (let i = 0; i < arr.length; i++){
5 | let lowestIndex = i;
6 | for (let j = i+1; j < arr.length; j++){
7 | if (arr[j] < arr[lowestIndex]){
8 | lowestIndex = j;
9 | }
10 | }
11 |
12 | if (arr[i] > arr[lowestIndex]){
13 | let temp = arr[i];
14 | arr[i] = arr[lowestIndex];
15 | arr[lowestIndex] = temp;
16 | }
17 | }
18 |
19 | return arr;
20 | }
21 |
22 | console.log(selectionSort([4,8,7,3,5,2,1]));
23 |
--------------------------------------------------------------------------------
/singly-linked-list.js:
--------------------------------------------------------------------------------
1 | //Singly linked list
2 |
3 | let Node = function(data){
4 | let self = {
5 | data: data,
6 | next: null,
7 | };
8 |
9 | return self;
10 | };
11 |
12 | var List = function(){
13 | let self = {
14 | _length: 0,
15 | head: null,
16 | add: function(data){
17 | let node = new Node(data);
18 | let currentNode = self.head;
19 |
20 | if (!currentNode){
21 | self.head = node;
22 | self._length++;
23 |
24 | return node;
25 | }
26 |
27 | while (currentNode.next){
28 | currentNode = currentNode.next;
29 | }
30 |
31 | currentNode.next = node;
32 | self._length++;
33 |
34 | return node;
35 | },
36 |
37 | findAtPosition: function(position){
38 | self.checkValidPosition(position);
39 |
40 | let count = 0;
41 | let currentNode = self.head;
42 |
43 | while ( count < position ){
44 | currentNode = currentNode.next;
45 | count++;
46 | }
47 |
48 | return currentNode;
49 | },
50 |
51 | remove: function(position){
52 | self.checkValidPosition(position);
53 |
54 | //if its the first position
55 | if (position === 0){
56 | let deletedNode = self.head;
57 | self.head = self.head.next;
58 | self._length--;
59 |
60 | return deletedNode;
61 | }
62 |
63 | let beforeNode = self.findAtPosition(position - 1);
64 | let deletedNode = beforeNode.next;
65 |
66 | if (deletedNode == null){
67 | beforeNode.next = null;
68 | } else {
69 | beforeNode.next = deletedNode.next;
70 | }
71 |
72 | self._length--;
73 | return deletedNode;
74 | },
75 |
76 | checkValidPosition: function(position){
77 | if (position > self._length || self._length === 0 || position < 0){
78 | throw new Error("Fuck you!");
79 | }
80 | }
81 | };
82 |
83 | return self;
84 | };
85 |
86 | let newList = new List();
87 | newList.add("fuck!");
88 | newList.add("snakes!");
89 | newList.add("hi");
90 | newList.add("sup");
91 |
92 | console.log(newList.head);
93 | console.log(newList._length);
94 | newList.remove(2);
95 | console.log(newList.head);
96 | console.log(newList._length);
97 |
--------------------------------------------------------------------------------
/something.js:
--------------------------------------------------------------------------------
1 | class Stack {
2 | constructor(){
3 | this.currentSize = 0;
4 | this.storage = {};
5 | }
6 |
7 | push(data){
8 | let index = ++this.currentSize;
9 | this.storage[index] = data;
10 | }
11 |
12 | pop(){
13 | let retrieved = this.storage[this.currentSize];
14 | delete this.storage[this.currentSize];
15 | --this.currentSize;
16 | return retrieved;
17 | }
18 | }
19 |
20 |
21 | let stack = new Stack();
22 | stack.push("fuck");
23 | console.log(stack);
24 |
25 | let something = stack.pop();
26 | console.log(something);
27 | console.log(stack);
28 |
29 |
30 | class Queue {
31 | constructor(){
32 | this.minimumIndex = 0;
33 | this.maximumIndex = null;
34 | this.storage = {};
35 | }
36 |
37 | enqueue(data){
38 | if (this.maximumIndex === null){
39 | this.maximumIndex = 0;
40 | this.storage[this.maximumIndex] = data;
41 | } else {
42 | ++this.maximumIndex;
43 | this.storage[this.maximumIndex] = data;
44 | }
45 | }
46 |
47 | dequeue(){
48 | this.checkEmpty();
49 | // now you know its not empty. moving on
50 | let retrieved = this.storage[this.minimumIndex];
51 | delete this.storage[this.minimumIndex];
52 | ++this.minimumIndex;
53 | return retrieved;
54 | }
55 |
56 | checkEmpty(){
57 | if (this.minimumIndex === this.maximumIndex){
58 | throw new Error("empty");
59 | }
60 | }
61 | }
62 |
63 | let queue = new Queue();
64 | queue.enqueue("hiyo");
65 | queue.enqueue("something");
66 | queue.enqueue("snakes");
67 | console.log(queue);
68 |
69 | console.log(queue.dequeue());
70 |
71 | console.log(queue);
72 |
--------------------------------------------------------------------------------
/somethingelse.js:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/cpow/js-cs-notes/5c05057fa0115b0c537067de64b952c2b545181a/somethingelse.js
--------------------------------------------------------------------------------
/stack.js:
--------------------------------------------------------------------------------
1 | //Stack is a linear data structure that can add data and remove only the last
2 | //added data.
3 | var Stack = function(){
4 | this.currentSize = 0;
5 | this.storage = {};
6 | };
7 |
8 | Stack.prototype.push = function(data){
9 | var index = this.currentSize++;
10 | this.storage[index] = data;
11 | };
12 |
13 | Stack.prototype.pop = function(){
14 | if (this.currentSize > 0){
15 | --this.currentSize;
16 | var recentData = this.storage[this.currentSize];
17 | delete this.storage[this.currentSize];
18 | console.log(recentData);
19 | return true;
20 | } else {
21 | return;
22 | }
23 | };
24 |
--------------------------------------------------------------------------------