From 9a4ed92c71fd160a064cf4f4d604446374cc7b93 Mon Sep 17 00:00:00 2001 From: Obo Agboghidi Date: Tue, 18 Apr 2017 18:03:57 -0700 Subject: [PATCH 1/5] Tuesday --- factorial.js | 9 +++++++++ factorial_test.js | 29 +++++++++++++++++++++++++++++ fizzbuzz.js | 27 +++++++++++++++++++++++++++ fizzbuzz_test.js | 13 +++++++++++++ src/makeChange.js | 2 ++ test/callmenick_test_template.js | 9 +++++++++ 6 files changed, 89 insertions(+) create mode 100644 factorial.js create mode 100644 factorial_test.js create mode 100644 fizzbuzz.js create mode 100644 fizzbuzz_test.js create mode 100644 test/callmenick_test_template.js diff --git a/factorial.js b/factorial.js new file mode 100644 index 0000000..32caa3e --- /dev/null +++ b/factorial.js @@ -0,0 +1,9 @@ +function factorial(n){ + if(n === 0){ + return 1; + } + n=n*factorial(n-1); + return n; + }; + +factorial(2); \ No newline at end of file diff --git a/factorial_test.js b/factorial_test.js new file mode 100644 index 0000000..54ac327 --- /dev/null +++ b/factorial_test.js @@ -0,0 +1,29 @@ +import { expect } from 'chai' +import factorial from '../123/factorial' + +/*describe( 'factorial()' , function(){ + +it(' should be a function ', function(){ +expect ( factorial ).to.be.a('function') + }) +})*/ + + + describe( 'factorial()', function(){ + + it( 'should be a function', function(){ + expect( factorial ).to.be.a( 'function' ) + }) + it( 'returns the factorial of a given number', function(){ + expect( factorial(5)).to.equal( 120 ) + }) + it( 'returns the factorial of a given number', function(){ + expect( factorial(7)).to.equal( 5040 ) + }) + it( 'only operates on a valid input', function(){ + expect( factorial('5')).to.equal( 'Please provide a valid number.' ) + }) + it( 'only operates on a valid input', function(){ + expect( factorial([5])).to.equal( 'Please provide a valid number.' ) + }) + }) \ No newline at end of file diff --git a/fizzbuzz.js b/fizzbuzz.js new file mode 100644 index 0000000..eb5b8c8 --- /dev/null +++ b/fizzbuzz.js @@ -0,0 +1,27 @@ + +function fizzBuzz(){ + let empArray = []; + for(var i = 0; i < 101; i++){ + + empArray.push([i]); + if(i % 3 === 0 && i % 5 === 0){ + //console.log("b"); + empArray.splice([i] , 1, "FizzBuzz"); + //empArray.push([i]); + } + else if(i % 3 === 0){ + empArray.splice([i] , 1, "Fizz"); + } + else if(i % 5 === 0){ + empArray.splice([i] , 1, "Buzz"); + } + } + return empArray; + + } + + +fizzBuzz(); + + +// => [1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', ...] diff --git a/fizzbuzz_test.js b/fizzbuzz_test.js new file mode 100644 index 0000000..786939a --- /dev/null +++ b/fizzbuzz_test.js @@ -0,0 +1,13 @@ +import { expect } from 'chai' +import fizzbuzz from '../123/fizzbuzz' + +describe( 'fizzBuzz()' , function(){ + + + it (' should be a function' , function(){ + + expect( fizzBuzz ).to.be.a('function') + }) +}) + +//it( should have a Array) \ No newline at end of file diff --git a/src/makeChange.js b/src/makeChange.js index 59d89b1..0192b77 100644 --- a/src/makeChange.js +++ b/src/makeChange.js @@ -1,3 +1,5 @@ export default function makeChange({price, amountGiven}) { // your code here } + +module.exports = makeChange \ No newline at end of file diff --git a/test/callmenick_test_template.js b/test/callmenick_test_template.js new file mode 100644 index 0000000..fc24abb --- /dev/null +++ b/test/callmenick_test_template.js @@ -0,0 +1,9 @@ +var expect = chai.expect; +var should = chai.should(); + +describe('Is Even Tests', function() { +}); + +it('Should always return a boolean', function() { + expect(isEven(2)).to.be.a('boolean'); +}); \ No newline at end of file From 8d9f237fa98f2672e97004fb713037bd8f78e127 Mon Sep 17 00:00:00 2001 From: Obo Agboghidi Date: Thu, 20 Apr 2017 17:46:34 -0700 Subject: [PATCH 2/5] Wednesday --- complete_wed/bubbleSort.js | 21 ++++++++++++++++++++ complete_wed/factorial.js | 9 +++++++++ complete_wed/fibonacci.js | 16 +++++++++++++++ complete_wed/mergeSort.js | 27 +++++++++++++++++++++++++ complete_wed/setInsersection.js | 23 ++++++++++++++++++++++ complete_wed/setUnion.js | 35 +++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+) create mode 100644 complete_wed/bubbleSort.js create mode 100644 complete_wed/factorial.js create mode 100644 complete_wed/fibonacci.js create mode 100644 complete_wed/mergeSort.js create mode 100644 complete_wed/setInsersection.js create mode 100644 complete_wed/setUnion.js diff --git a/complete_wed/bubbleSort.js b/complete_wed/bubbleSort.js new file mode 100644 index 0000000..33bcd5a --- /dev/null +++ b/complete_wed/bubbleSort.js @@ -0,0 +1,21 @@ +function bubbleSort(data){ + var len = data.length; //step 1. Determine length of list being sorted tp determine how many passes through the list you will need + + do{ + for(var i = 0; i < len - 1; i++){ //step 2. Iterate our loop from first element to second to last element. + if (data[i] > data[i + 1]){//step 3. Check the current element and the one next to it. If we find an element who next elemt is higher, we run a function to swap their positions + swap(data, i, i + 1); + } + } + }while (len--); + + return data; +} + +function swap(arr, indexA, indexB){ + var temp = arr[indexA];//Step 1. Stores a temp variable to start the copy + arr[indexA] = arr[indexB];//Step 2. changes indexA to index B + arr[indexB] = temp;//Step 3. replaces indexB with information that used to be A. This swaps their positions in the array. +} + +bubbleSort([10, 2, 7, 5, 8, 3, 6, 1, 4, 9]); \ No newline at end of file diff --git a/complete_wed/factorial.js b/complete_wed/factorial.js new file mode 100644 index 0000000..32caa3e --- /dev/null +++ b/complete_wed/factorial.js @@ -0,0 +1,9 @@ +function factorial(n){ + if(n === 0){ + return 1; + } + n=n*factorial(n-1); + return n; + }; + +factorial(2); \ No newline at end of file diff --git a/complete_wed/fibonacci.js b/complete_wed/fibonacci.js new file mode 100644 index 0000000..6b72dff --- /dev/null +++ b/complete_wed/fibonacci.js @@ -0,0 +1,16 @@ +function fibonacci(num){ + var a = 1; + var b = 0; + arr = []; + var temp; + + for(var i = num; num >= 0; i--){ + temp =a; + a = a + b; //we calculate the next number by adding the current number to the old number + b = temp; + arr.push(b);//We push the fibonacci number into a empty variable + num--; + } + return arr; +} +fibonacci(10); \ No newline at end of file diff --git a/complete_wed/mergeSort.js b/complete_wed/mergeSort.js new file mode 100644 index 0000000..285e114 --- /dev/null +++ b/complete_wed/mergeSort.js @@ -0,0 +1,27 @@ +function mergeSort(data){ //If data.length is one, we know it can not be broken down anymore, so we just return the data + if(data.length < 2) { + return data; + } + var midPoint = Math.round(data.length/2); //Step 1. Determind where half of array is. Divide array length by 2, and use math.round to gurantee you get a whole integer + return merge(//Calling a function + mergeSort(data.slice(0, midPoint)),//use slice() on array, to cut in half, and store away from element 0 to where midPoint is + mergeSort(data.slice(midPoint))//slice() again to copy array again. Starting from midPoint to end of array + ); +} + +function merge(left, right){ + var out = []; + while (left.length && right.length){// Step 3. We then loop through both lists being merged + out.push(left[0] < right[0] ? left.shift() : right.shift()); //Step 4. and push the smallest item of the two inputs into the output list. + } + while(left.length){ + out.push(left.shift()); + } + while(right.length){ + out.push(right.shift()); + }// one of the lists is empty we add the remaining items ino the output lists + + return out; +} + +mergeSort([10, 2, 7, 5, 8, 3, 6, 1, 4, 9]); \ No newline at end of file diff --git a/complete_wed/setInsersection.js b/complete_wed/setInsersection.js new file mode 100644 index 0000000..78805a4 --- /dev/null +++ b/complete_wed/setInsersection.js @@ -0,0 +1,23 @@ +//Return the intersection of two sets. This is similar to setunion, except here we are removing many indexes and elements, not storing them + +const a = [1, 2, 3, 4]; +const b = [2, 4, 6, 8]; +var arr = []; + +function setInsersection(){ + while( a.length > 0 && b.length > 0 ){ //run a while loop + if(a[0] < b[0] ){ + a.shift(); //if a[0] is not the same as b[0], remove it out of the array with shift + }else if (a[0] > b[0] ){ + b.shift(); //same with b[0] + }else{ //they're equal here + arr.push(a.shift());//so push one of them into empty array + b.shift();//get rid of the other + } + } + + return arr; +} + +setInsersection(a, b); +// => [2, 4] \ No newline at end of file diff --git a/complete_wed/setUnion.js b/complete_wed/setUnion.js new file mode 100644 index 0000000..c5888f0 --- /dev/null +++ b/complete_wed/setUnion.js @@ -0,0 +1,35 @@ +/* Union algorithm uses a part of the mergesort algorithm. You can push the 2 array elements into one empty array, + and after that is done, run another function to "clear out" he original array, by making a new + empty array and push content onto that. */ + +var a = [1, 2, 3, 4]; +var b = [2, 4, 6, 8]; +var arr = []; + +function setUnion(){ + + while(a.length && b.length){ + arr.push(a[0] < b[0] ? a.shift() : b.shift()); + } + while(a.length){ + arr.push(a.shift()); + } + while(b.length){ + arr.push(b.shift()); + } + //console.log(arr); + return clearOut(arr) +} + +function clearOut(arr){ + var tmp = []; + for(var i = 0; i < arr.length; i++){ + if(tmp.indexOf(arr[i]) == -1){ + tmp.push(arr[i]); + } + } + return tmp; +} +//function clearOut(arr){ +setUnion(a, b); +// => [1, 2, 3, 4, 6, 8] \ No newline at end of file From cfbe5653a7cb4aa0c96521757b3685e1e43a65ee Mon Sep 17 00:00:00 2001 From: Obo Agboghidi Date: Fri, 21 Apr 2017 09:33:25 -0700 Subject: [PATCH 3/5] closetPair --- closetPair.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 closetPair.js diff --git a/closetPair.js b/closetPair.js new file mode 100644 index 0000000..ef97367 --- /dev/null +++ b/closetPair.js @@ -0,0 +1,43 @@ +var points = [ + [2,1], + [4,0], + [-1,0], + [5,3], + [-2,5], + [3,-3], + [-2,0], + [3,4], + [5,-4], + [0,-2] +]; + +function bruteForce(points) { + var point1 = null; + var point2 = null; + var distance = null; +// null is where the thing is known to exist, but it's not known what the value is. + //we will loop through all of the points + for (var i = 0; i < points.length; i = i + 1) { + //compare this point with all of the points ahead of it in the array + for (var j = i + 1; j < points.length; j = j + 1) { + //compute distance using distance formula (Pythagoreom theory) + var curr = Math.sqrt(Math.pow(points[i][0] - points[j][0], 2) + Math.pow(points[i][1] - points[j][1], 2)); + + //compare this with our shortest distance + //or set it if it's the first time we run + if (distance === null || curr < distance) { + distance = curr; + point1 = points[i]; + point2 = points[j]; + } + } + } + //point1 and point2 hold the closest points + //distance is the distance between the two points + return { + "point1": point1, + "point2": point2, + "distance": distance + }; +}; +bruteForce(points); From 43dac271b422449bf2c31405d31795746b66a348 Mon Sep 17 00:00:00 2001 From: Obo Agboghidi Date: Fri, 21 Apr 2017 11:34:07 -0700 Subject: [PATCH 4/5] binarySearch --- binarySearch.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 binarySearch.js diff --git a/binarySearch.js b/binarySearch.js new file mode 100644 index 0000000..4b6c1b8 --- /dev/null +++ b/binarySearch.js @@ -0,0 +1,24 @@ +var array = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]; +function binarySearch(array, key){ + var lo = 0; + var hi = array.length - 1; + var mid; + var element; + + while(lo <= hi){ //run a loop through the array + mid = Math.floor((lo + hi)/2, 10); //calculate the middle of the array + element = array[mid]; //make element the middle of the array + if (element < key){ //if mid of the array is less than key + lo = mid + 1; //cut array in half by moving the low from the first index to a spot 1 index higher than the mid. + + //With this, the loop now runs through less indexes + } else if (element > key) {//if element is kess than key + hi = mid - 1; //the end of the array is moved to one index removed from the mid of the array, cutting it in half. + + }else{//When their are no other places to go, and the array is essentially just one index, return the mid. + return mid; + } + } +} + +binarySearch(array, 45); \ No newline at end of file From c0bb72a6a395af72355e7a9c356dfe947b6006f4 Mon Sep 17 00:00:00 2001 From: oagboghi2 Date: Fri, 21 Apr 2017 11:44:49 -0700 Subject: [PATCH 5/5] Delete binarySearch.js deleted --- binarySearch.js | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 binarySearch.js diff --git a/binarySearch.js b/binarySearch.js deleted file mode 100644 index 4b6c1b8..0000000 --- a/binarySearch.js +++ /dev/null @@ -1,24 +0,0 @@ -var array = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50]; -function binarySearch(array, key){ - var lo = 0; - var hi = array.length - 1; - var mid; - var element; - - while(lo <= hi){ //run a loop through the array - mid = Math.floor((lo + hi)/2, 10); //calculate the middle of the array - element = array[mid]; //make element the middle of the array - if (element < key){ //if mid of the array is less than key - lo = mid + 1; //cut array in half by moving the low from the first index to a spot 1 index higher than the mid. - - //With this, the loop now runs through less indexes - } else if (element > key) {//if element is kess than key - hi = mid - 1; //the end of the array is moved to one index removed from the mid of the array, cutting it in half. - - }else{//When their are no other places to go, and the array is essentially just one index, return the mid. - return mid; - } - } -} - -binarySearch(array, 45); \ No newline at end of file