Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions closetPair.js
Original file line number Diff line number Diff line change
@@ -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);
21 changes: 21 additions & 0 deletions complete_wed/bubbleSort.js
Original file line number Diff line number Diff line change
@@ -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]);
9 changes: 9 additions & 0 deletions complete_wed/factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function factorial(n){
if(n === 0){
return 1;
}
n=n*factorial(n-1);
return n;
};

factorial(2);
16 changes: 16 additions & 0 deletions complete_wed/fibonacci.js
Original file line number Diff line number Diff line change
@@ -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);
27 changes: 27 additions & 0 deletions complete_wed/mergeSort.js
Original file line number Diff line number Diff line change
@@ -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]);
23 changes: 23 additions & 0 deletions complete_wed/setInsersection.js
Original file line number Diff line number Diff line change
@@ -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]
35 changes: 35 additions & 0 deletions complete_wed/setUnion.js
Original file line number Diff line number Diff line change
@@ -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]
9 changes: 9 additions & 0 deletions factorial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function factorial(n){
if(n === 0){
return 1;
}
n=n*factorial(n-1);
return n;
};

factorial(2);
29 changes: 29 additions & 0 deletions factorial_test.js
Original file line number Diff line number Diff line change
@@ -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.' )
})
})
27 changes: 27 additions & 0 deletions fizzbuzz.js
Original file line number Diff line number Diff line change
@@ -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', ...]
13 changes: 13 additions & 0 deletions fizzbuzz_test.js
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions src/makeChange.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export default function makeChange({price, amountGiven}) {
// your code here
}

module.exports = makeChange
9 changes: 9 additions & 0 deletions test/callmenick_test_template.js
Original file line number Diff line number Diff line change
@@ -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');
});