diff --git a/algorithms.md b/algorithms.md index 93d5faa..68d86c5 100644 --- a/algorithms.md +++ b/algorithms.md @@ -61,7 +61,7 @@ Return the factorial of a number. factorial(5) // => 120 ``` - +fac #### fibonacci Return an array of Fibonacci numbers to the _nth_ position. diff --git a/src/collatzConjecture.js b/src/collatzConjecture.js new file mode 100644 index 0000000..629590a --- /dev/null +++ b/src/collatzConjecture.js @@ -0,0 +1,23 @@ +function assembleCollatz(num) { + let collatzArray = [num]; + + while ( collatzArray[collatzArray.length-1] !== 1) { + let lastNumber = collatzArray[collatzArray.length-1]; + + if (lastNumber % 2 === 0) { + collatzArray.push(lastNumber / 2) + } else { + collatzArray.push((3 * lastNumber) + 1) + } + } + return collatzArray; +} +console.log(assembleCollatz(7)); + +//initalize function (num) +// create empty arraySequence + // while (the last number of arraySequence is not 1) + // if num is even + // push num / 2 to the array + // else if push 3num + 1 to arraySequence + // return sequence diff --git a/src/factorial.js b/src/factorial.js new file mode 100644 index 0000000..7447422 --- /dev/null +++ b/src/factorial.js @@ -0,0 +1,8 @@ +function factorialize(num) { + let inputValue = num; + let factorial = 1; + for (let x = num; x >= 1; x--) { + factorial *= x; + } + return factorial; +} diff --git a/src/fibonacci.js b/src/fibonacci.js new file mode 100644 index 0000000..dd500eb --- /dev/null +++ b/src/fibonacci.js @@ -0,0 +1,12 @@ +function assembleFibonacci(nth) { + let a = 0, b = 1, f = 1; + let fibonacciArray = [a,b]; + for (let i = 2; i < nth; i++) { + f = a + b; + a = b; + b = f; + fibonacciArray.push(f); + } + return fibonacciArray; +} +console.log(assembleFibonacci(10)); diff --git a/src/fizzBuzz.js b/src/fizzBuzz.js new file mode 100644 index 0000000..0e820cb --- /dev/null +++ b/src/fizzBuzz.js @@ -0,0 +1,14 @@ +let list = new Array(100); + + for (let i = 0; i < 100; i++) { + list[i] = i + 1; // $$c populates the array. +1 is necessary because arrays are 0 index based and you want to store 1 - 100 in it, NOT 0-99.{ + + //list[i] is equal to an index in the array + if (list[i] % 5 === 0 && list[i] % 3 === 0) { // populate multiples of both five && three with "FIZZBUZZ" + list[i] = "FizzBuzz" + } else if (list[i] % 5 === 0) { // populate multiples of five with "BUZZ" + list[i] = "Buzz" + } else if (list[i] % 3 === 0) { // populate multiples of three with "FIZZ" + list[i] = "Fizz" + } + } diff --git a/src/isPalindrome.js b/src/isPalindrome.js new file mode 100644 index 0000000..9cd205e --- /dev/null +++ b/src/isPalindrome.js @@ -0,0 +1,23 @@ + +function isPalindrome(str) { + + str = str.replace(/[^0-9a-z]/gi, "").toLowerCase(); + let palindrome = str.split("").reverse().join(""); + if (str === palindrome) { + return true; + } else { + return false; + } +} + +/*console.log( + + isPalindrome('radar') + => true + + isPalindrome('bananas') + => false + + isPalindrome('A man, a plan, a canal: Panama') + => true + isPalindrome("A man, a plan, a canal: Panama")); */ diff --git a/src/makeChange.js b/src/makeChange.js index 59d89b1..77afa24 100644 --- a/src/makeChange.js +++ b/src/makeChange.js @@ -1,3 +1,18 @@ export default function makeChange({price, amountGiven}) { - // your code here + // your code here +let coins = {quarters: 25, dimes: 10, nickels: 5, pennies: 1 } +let changeDue = amountGiven - price + + for(let key in coins) { + let numberOfCoins = Math.floor(changeDue / coins[key]) + /*takes the changeDue divides it by coin section of quarters (25) resulting in a + decimal of 1.64, then Math.floor round down to the nearest integer which is + 1 (<= the given number) and assigns it to the variable numberOfCoins. + */ + changeDue -= coins[key] * numberOfCoins + + coins[key] = numberOfCoins + + } + return coins; } diff --git a/src/setUnion.js b/src/setUnion.js new file mode 100644 index 0000000..7dd6ba0 --- /dev/null +++ b/src/setUnion.js @@ -0,0 +1,26 @@ +function returnUnion(a,b) { + const a = [1, 2, 3, 4] + const b = [2, 4, 6, 8] + let unionValues = []; + + + // create index counter to loop through array + // while checking for matches/equality/less of index + // push proper values to array + + + + + + + +} + +// initalize function (a,b) +// declare constants and set empty array to hold union values +// determine the first values of the two arrays + + // if one is less than the other push the lesser value to array + // if equal push the value to array + // else return union values +// return unionVAluesm diff --git a/test/factorial_test.js b/test/factorial_test.js new file mode 100644 index 0000000..69e0fa8 --- /dev/null +++ b/test/factorial_test.js @@ -0,0 +1,16 @@ +import { expect } from 'chai' +import factorialize from '../src/factorial' + +describe('factorial()', function(){ + + it('should be a function', function(){ + expect(factorial).to.be.a('function') + }) + + it('returns the outout of the factorial)', function(){ + expect(factorialize(5)).to.equal(120) + expect(factorialize(12)).to.equal(479001600) + expect(factorialize(8)).to.equal(40320) + + }) +}) diff --git a/test/fizzBuzzTests.js b/test/fizzBuzzTests.js new file mode 100644 index 0000000..e69de29 diff --git a/test/isPalindrome_test.js b/test/isPalindrome_test.js new file mode 100644 index 0000000..a4f30c2 --- /dev/null +++ b/test/isPalindrome_test.js @@ -0,0 +1,20 @@ +import { expect } from 'chai' +import isPalindrome from '../src/isPalindrome' + +describe('isPalindrome()', function(){ + + it('should be a function', function(){ + expect(isPalindrome).to.be.a('function') + }) + + it('returns true if the input is a Palindrome', function(){ + expect(isPalindrome('racecar')).to.equal(true) + }) + it('returns true if the input is a Palindrome', function(){ + expect(isPalindrome('trevor')).to.equal(false) + }) + it('returns true if the input is a Palindrome', function(){ + expect(isPalindrome('A man, a plan, a canal: Panama')).to.equal(true) + }) + +})