diff --git a/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js b/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js index 230c8030..61abb553 100644 --- a/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js +++ b/01-week-1--starter-algorithms/03-day-4--selection-sort/javascript/selection_sort.js @@ -1,6 +1,20 @@ function selectionSort(arr) { - // type your code here -} + newArr = [] + + while (arr.length !== 0) { + minny = Math.min(...arr) + index = arr.indexOf(minny) + + newArr.push(minny) + arr.splice(index, 1) + console.log(newArr) + + } + + } + + + if (require.main === module) { // add your own tests in here @@ -20,4 +34,13 @@ if (require.main === module) { module.exports = selectionSort; // Please add your pseudocode to this file + +//create new array to push things into +//create a while loop that goes until arr is 0 +//find smallest value in array +//find index of smallest value +//push min value to new array +//remove that value using slice +//some elm = Math.min, somewhere? + // And a written explanation of your solution diff --git a/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js b/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js index d995b9c7..d2fc4fd3 100644 --- a/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js +++ b/02-week-2--recursion/01-day-1--recursive-counting/javascript/recursive_count.js @@ -1,5 +1,10 @@ function recursiveCount(num = 0) { - // type your code here + if (num <= 9) { + console.log(num) + + + recursiveCount(num + 1) + } } if (require.main === module) { @@ -11,3 +16,7 @@ module.exports = recursiveCount; // OPTIONAL // Please add your pseudocode to this file // And a written explanation of your solution + +//I make an if statement to establish the base count. +//I then console.log(num) to print num as it returns up the stack +//I make the recursive call of recursiveCount(num + 1) diff --git a/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js b/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js index 7ff60062..04238d31 100644 --- a/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js +++ b/02-week-2--recursion/02-day-2--recursive-search/javascript/recursive_search.js @@ -1,5 +1,15 @@ function recursiveSearch(arr, target) { - // type your code here + if (arr.length === 0) { + return false + } + + if (arr[0] === target) { + return true + } + + return recursiveSearch(arr.slice(1), target) + + } if (require.main === module) { @@ -15,5 +25,22 @@ if (require.main === module) { module.exports = recursiveSearch; + + + + + + + + // Please add your pseudocode to this file + +//if array.length = 0 +//return false +//if element[i] in array = target +//return true +//someVariable = array.slice +//recursiveSearch(someVariable, target) + + // And a written explanation of your solution diff --git a/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js b/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js index 7d118da6..016d817f 100644 --- a/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js +++ b/02-week-2--recursion/03-day-3--recursive-fibonacci-series/javascript/fibonacci_recursive.js @@ -1,5 +1,10 @@ function fibonacci(n) { - // type your code here + if (n < 2) { + return n + } + + return fibonacci(n - 1) + fibonacci(n - 2) + } if (require.main === module) { diff --git a/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js b/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js index 92c47e89..ed87ed75 100644 --- a/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js +++ b/02-week-2--recursion/04-day-4--recursive-find-shortest-string/javascript/find_shortest_string_recursive.js @@ -1,5 +1,12 @@ function findShortestStringRecursive(arr) { - // type your code here + if (arr.length === 1) { + return arr[0] + } + + const result = findShortestStringRecursive(arr.slice(1)) + + return arr[0] <= result.length ? arr[0] : result + } if (require.main === module) { diff --git a/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js b/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js index bf1c20b6..de7540ae 100644 --- a/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js +++ b/02-week-2--recursion/05-day-5--recursive-selection-sort/javascript/selection_sort_recursive.js @@ -1,5 +1,15 @@ function selectionSortRecursive(arr) { - // type your code here +if (arr.length < 1) { + return [] +} + + const smallestNum = Math.min(...arr) + const index = arr.indexOf(smallestNum) + arr.splice(index, 1) + const result = selectionSortRecursive(arr) + result.unshift(smallestNum) + return result + } if (require.main === module) { diff --git a/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js b/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js index 62df33d9..89154862 100644 --- a/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js +++ b/06-week-5--big-o-continued/00-days-1-to-2--implement-a-stack-class/javascript/stack.js @@ -9,46 +9,58 @@ class Stack { // add item to top of stack if not full // if full throw error push(item) { - + if (!this.isFull()) { + this.stack.push(item) + } else { + throw new Error("Stack is full!") + } } // remove item from top of stack and return it pop() { - + return this.stack.pop() } // return item at top of stack without removing it peek() { - + return this.stack[this.size() -1] } // return true if stack is empty, otherwise false isEmpty() { - + return this.size() === 0 } // return true if stack is full, otherwise false isFull() { - + return this.size() === this.limit } // return number of items in stack size() { - + return this.stack.length } // return -1 if item not in stack, otherwise integer representing // how far it is from the top search(target) { + for (let i = -1; i >= -this.size(); --i) { + if (this.stack[this.size() + i] === target) { + return Math.abs(i) - 1; + } + } + return -1; } + // print contents of stack: do not return the stack itself! print() { - + console.log(this.stack.join(' <- ')) } } + if (require.main === module) { // add your own tests in here } diff --git a/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js b/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js index 06654e80..583e9202 100644 --- a/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js +++ b/06-week-5--big-o-continued/01-days-3-to-4--implement-a-queue-class/javascript/queue.js @@ -9,43 +9,59 @@ class Queue { // add item to rear of queue if not full // if full throw error enqueue(item) { + if (!this.isFull()) { + this.queue.push(item) + } else { + throw new Error("Queue is full!") + } } + // remove item from front of queue and return it dequeue() { - + return this.queue.shift() } + // return item at front of queue without removing it peek() { - + return this.queue[0] } // return true if queue is empty, otherwise false isEmpty() { - + if (this.size() === 0) { + return true + } else { + return false + } } // return true if queue is full, otherwise false isFull() { - + return this.size() === this.limit } // return number of items in queue size() { - + return this.queue.length } // return -1 if item not in queue, otherwise integer representing // how far it is from the front search(target) { - + if( this.queue.indexOf(target) > -1) { + return this.queue.indexOf(target) + } else { + return -1 + } + } // print contents of queue: do not return the queue itself! print() { - + console.log(this.queue.join(' <- ')) } } diff --git a/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js b/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js index 21032e5e..f6f30905 100644 --- a/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js +++ b/06-week-5--big-o-continued/02-day-5--implement-a-set/javascript/my_set.js @@ -3,38 +3,61 @@ class MySet { // if an iterable is provided only its unique values should be in data // strings and arrays will need to be broken down by their elements/characters constructor(iterable) { + if (!(iterable === undefined || + Array.isArray(iterable) || + typeof iterable === "string")) { + throw new Error("MySet only accepts iterable or nothing on initialization") + } + + + this.data = {}; + + if (iterable) { + for (const e of iterable) { + this.data[e] = true + } + } + + + } // return number of elements in MySet size() { - + return this.entries().length } // add an item to MySet as is // don't worry about arrays here! // return the MySet instance add(item) { - + this.data[item] = true + return this } // delete an item from MySet // don't worry about arrays here! // return true if successful, otherwise false delete(item) { + if (this.has(item)) { + delete this.data[item] + return true + } else { + return false + } } - // return true if in MySet, otherwise false // don't worry about arrays here! has(item) { - + return !!this.data[item] } // return data as an array // don't worry about arrays here! entries() { - + return Object.keys(this.data) } } diff --git a/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js b/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js index 14dd9232..826fdbfe 100644 --- a/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js +++ b/10-week-8--searching/01-day-4--manual-binary-tree/javascript/binary_tree.js @@ -1,23 +1,40 @@ class Node { - constructor() { - // add your Node class code + constructor(value, left = null, right = null) { + this.value = value; + this.left = left; + this.right = right; } } // list = [1, 4, 7] function oneToSeven() { - // manually create the BST - // then return the root node + const left = new Node(1); + const right = new Node(7); + + return new Node(4, left, right); } // list = [10, 40, 45, 46, 50] function tenToFifty() { - + const tenNode = new Node(10); + const fortyNode = new Node(40, tenNode); + const fortySixNode = new Node(46); + const fiftyNode = new Node(50, fortySixNode); + + return new Node(45, fortyNode, fiftyNode); } // list = [-20, -19, -17, -15, 0, 1, 2, 10] function negativeToPositive() { - + const nTwenty = new Node(-20); + const nSeventeen = new Node(-17); + const nNineteen = new Node(-19, nTwenty, nSeventeen); + const one = new Node(1); + const zero = new Node(0, null, one); + const ten = new Node(10); + const two = new Node(2, zero, ten); + + return new Node(-15, nNineteen, two); } if (require.main === module) {