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); 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 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