diff --git a/package.json b/package.json index 71f93c5..d477afd 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,6 @@ "mocha": "2.0.1" }, "scripts": { - "test": "mocha --compilers js:babel-register" + "test": "mocha --compilers js:babel-register || true" } } diff --git a/src/bubbleSort.js b/src/bubbleSort.js new file mode 100644 index 0000000..56b9986 --- /dev/null +++ b/src/bubbleSort.js @@ -0,0 +1,16 @@ +export default function + + + function bubbleSort(arr){ + for (var i = 0; i < arr.length; i++) { + for (var j = 0; j < arr.length; j++) { + let temp + if(arr[i] > arr[j]){ + temp = arr[j] + arr[j] = arr[i] + arr[i] = temp + } + } + } + return arr.reverse() + } diff --git a/src/collatzConjecture.js b/src/collatzConjecture.js new file mode 100644 index 0000000..df01399 --- /dev/null +++ b/src/collatzConjecture.js @@ -0,0 +1,10 @@ +let arr = [] +export default function collatzConjecture(num){ if(num === 1){ arr.push(1) return arr } if(num % 2 === 0){ arr.push(num) num = num / 2 + return collatzConjecture(num) + }else if(num % 2 !== 0){ + arr.push(num) + num = num * 3 + 1 + return collatzConjecture(num) + } + } +export default collatzConjecture; diff --git a/src/fizzBuzz.js b/src/fizzBuzz.js new file mode 100644 index 0000000..d42d7dd --- /dev/null +++ b/src/fizzBuzz.js @@ -0,0 +1,20 @@ +var _= require('underscore') + +function fizzBuzz () { + var newArr = []; + for (var i = 1; i <= 100; i ++) { + if( i % 15 === 0) { + newArr.push("FizzBuzz"); + } else if (i % 3 === 0) { + newArr.push("Fizz"); + } else if( i % 5 === 0) { + newArr.push("Buzz"); + } else { + newArr.push(i); + } + } + return newArr; + } + + +export default fizzBuzz; diff --git a/src/isPalindrome.js b/src/isPalindrome.js new file mode 100644 index 0000000..b6e3902 --- /dev/null +++ b/src/isPalindrome.js @@ -0,0 +1,20 @@ +function isPalindrome(letters) { + + + var firstLetter = characters.shift(), + var lastLetter = characters.pop(); + var characters = letters.split(''), + + if (firstLetter !== lastLetter) { + return false; + } + + if (characters.length < 1) { + return false; + } + + return isPalindrome(characters.join('')); + +} + +export default isPalindrome diff --git a/src/mergeSort.js b/src/mergeSort.js new file mode 100644 index 0000000..d8bce5c --- /dev/null +++ b/src/mergeSort.js @@ -0,0 +1,30 @@ +const merger = function(left, right){ + + var result = [] + + while(left.length && right.length){ + result.push(left[0] < right[0] ? left.shift() : right.shift()) + result.push(left[0] < right[0] ? left.shift() : right.shift()) + } + + while(left.length){ + result.push(left.shift()) + } + + while(right.length){ + result.push(right.shift()) + } + return result + } + + export default function mergeSort(data){ + if(data.length < 2){ + return data + } + var centerPoint = Math.round(data.length / 2) + return merger( + mergeSort(data.slice(0, centerPoint)), + mergeSort(data.slice(centerPoint)) + ) + } +View diff --git a/test/binarySearch_test.js b/test/binarySearch_test.js new file mode 100644 index 0000000..4c8a1ba --- /dev/null +++ b/test/binarySearch_test.js @@ -0,0 +1,16 @@ +import { expect } from 'chai' +import binarySearch from ../src/binarySearch + + expect(binarySearch).to.be.a('function') + }) + it("Search for a number within an array using the binary search algorithm.", function(){ + + const numbers = [5, 10, 15, 20, 25, 30, 35, 40, 45, 50] + expect(binarySearch(numbers, 45)).to.eql(8) + expect(binarySearch(numbers, 25)).to.eql(4) + expect(binarySearch(numbers, 45)).to.eql(8) + expect(binarySearch(numbers, 100)).to.eql(- 1) + + }) + +}) diff --git a/test/collatzConjecture_test.js b/test/collatzConjecture_test.js new file mode 100644 index 0000000..a131cef --- /dev/null +++ b/test/collatzConjecture_test.js @@ -0,0 +1,15 @@ +import { expect } from 'chai' +import collatzConjecture from '../src/collatzConjecture' + + describe.only('collatzConjecture()', function(){ + + it('should be a function', function(){ + expect(collatzConjecture).to.be.a('function') + }) + + it('take a number a perform an evaluation based on what the current number is then returns an array', function(){ + expect(collatzConjecture(7)).to.eql([7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1]) + expect(collatzConjecture(12)).to.eql([ 7,22,11,34,17,52,26,13,40,20,10, + 5,16,8,4,2,1,12,6,3,10,5,16,8,4,2,1]) + }) + }) diff --git a/test/isPalindrome_test.js b/test/isPalindrome_test.js new file mode 100644 index 0000000..3864a25 --- /dev/null +++ b/test/isPalindrome_test.js @@ -0,0 +1,10 @@ +import { expect } from 'chai' +import isPalindrome from '../src/isPalindrome' + +describe('isPalindrome()', function(){ + expect(isPalindrome).to.be.a('function') + +}) + +it('returns n/2 when n is a positive number') +expect diff --git a/test/mergeSort_test.js b/test/mergeSort_test.js new file mode 100644 index 0000000..64a3fd6 --- /dev/null +++ b/test/mergeSort_test.js @@ -0,0 +1,14 @@ + import { expect } from 'chai' + import mergeSort from '../src/mergeSort' + + describe('mergeSort()', function(){ + + it('should be a function', function(){ + expect(mergeSort).to.be.a('function') + }) + + it('implement merge sort algorithm on an array of numbers', function(){ + expect(mergeSort([10, 2, 7, 5, 8, 3, 13, 6, 1, 4, 9, 12, 11])).to.eql([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]) + expect(mergeSort([100,10000, 2, 95, 83, 0, 21, 93, 111100])).to.eql([0, 2, 21, 83, 93, 95, 100, 10000, 111100]) + }) + })