From a6b32f8c52bcf696770650173c079f0e2463e279 Mon Sep 17 00:00:00 2001 From: kmu973 Date: Wed, 5 Oct 2022 10:28:17 -0400 Subject: [PATCH] Min assignment --- exercises/part1-fizzbuzz/index.js | 7 +- exercises/part2-basic-functions/index.js | 76 ++++++++++---- exercises/part3-iterators/index.js | 97 ++++++++++++++--- exercises/part4-functions-as-values/index.js | 104 ++++++++++++++++++- 4 files changed, 245 insertions(+), 39 deletions(-) diff --git a/exercises/part1-fizzbuzz/index.js b/exercises/part1-fizzbuzz/index.js index a4302f9..127a53b 100644 --- a/exercises/part1-fizzbuzz/index.js +++ b/exercises/part1-fizzbuzz/index.js @@ -25,7 +25,12 @@ Start code ===================== */ - +for (let i = 1; i <= 100; i ++){ + if (i%3 === 0 && i%5 === 0) {console.log("FizzBuzz"); +} else if (i%3 === 0) {console.log("Fizz"); +} else if (i%5 === 0) {console.log("Buzz"); +} else {console.log(i);} +} /* ===================== diff --git a/exercises/part2-basic-functions/index.js b/exercises/part2-basic-functions/index.js index ee2138b..40cd703 100644 --- a/exercises/part2-basic-functions/index.js +++ b/exercises/part2-basic-functions/index.js @@ -13,71 +13,96 @@ in years. (Let's just assume this person was born January 1 at 12:01 AM) Example: "age(2000) should return 22" ===================== */ -let age = (birthYear) => {}; +let age = (birthYear) => { + return 2022 - birthYear +}; -console.log('age success:', age(1971) === 51); +console.log('age:', age(1991)) + +/*console.log('age success:', age(1971) === 51); /* ===================== Instructions: Write a function that adds one to the number provided Example: "plusOne(2) should return 3" ===================== */ -let plusOne = () => {}; +let plusOne = (x) => { + return x+1; +}; +console.log(plusOne(2)) -console.log('plusOne success:', plusOne(99) === 100); +/*console.log('plusOne success:', plusOne(99) === 100); /* ===================== Instructions: Write a function that multiplies the number provided by three Example: "timesThree(2) should return 6" ===================== */ -let timesThree = () => {}; +let timesThree = (x) => { + return x*3; +}; +console.log(timesThree(2)) -console.log('timesThree success:', timesThree(33) === 99); +/*console.log('timesThree success:', timesThree(33) === 99); /* ===================== Instructions: Write a function that adds two given numbers Example: "add(2, 3) should return 5" ===================== */ -let add = () => {}; +let add = (x, y) => { + return x+y +}; +console.log(add(2,3)) -console.log('add success:', add(4, 6) === 10); +/*console.log('add success:', add(4, 6) === 10); /* ===================== Instructions: Write a function that multiplies two given numbers Example: "multiply(2, 3) should return 6" ===================== */ -let multiply = () => {}; +let multiply = (x, y) => { + return x*y; +}; +console.log(multiply(2,3)); -console.log('multiply success:', multiply(4, 6) === 24); +/*console.log('multiply success:', multiply(4, 6) === 24); /* ===================== Instructions: "Write a function that returns the value of an array at a specified index" Example: "valueAtIndex(['Mercury', 'Venus', 'Earth', 'Mars'], 2) should return 'Earth'" ===================== */ -let valueAtIndex = () => {}; - -console.log('valueAtIndex success:', valueAtIndex([9, 8, 7, 6, 5], 2) === 7); - +let valueAtIndex = (arr, i) => { + return arr[i] +}; +console.log(valueAtIndex(['Mercury', 'Venus', 'Earth', 'Mars'], 2)) + /* ===================== Instructions: "Write a function that returns the value of an object at a specified key" Example: "valueAtKey({'name': 'Nathan'}, 'name') should return 'Nathan'" ===================== */ -let valueAtKey = () => {}; - -console.log('valueAtKey success:', valueAtKey({ 'foo': 'bar' }, 'foo') === 'bar'); +let valueAtKey = (object, key) => { + return object[key] +}; +console.log(valueAtKey({ 'foo': 'bar' }, 'foo')); /* ===================== Instructions: Write a function that returns true if a number is even. ===================== */ -let isEven = () => {}; +let isEven = (x) => { + if(x%2 === 0){console.log("true") +} else { + console.log("false") +} +} -console.log('isEven success:', isEven(2) === true && isEven(3) === false); +console.log(isEven(2)) +console.log(isEven(3)) +/*console.log('isEven success:', isEven(2) === true && isEven(3) === false); /* ===================== Instructions: Write a function that returns true if a number is odd. @@ -85,4 +110,15 @@ Instructions: Write a function that returns true if a number is odd. let isOdd = () => {}; -console.log('isOdd success:', isOdd(4) === false && isOdd(5) === true); +let isOdd = (x) => { + if(x%2 != 0){console.log("true") +} else { + console.log("false") +} +} + +console.log(isOdd(2)) +console.log(isOdd(3)) + + +/*console.log('isOdd success:', isOdd(4) === false && isOdd(5) === true); diff --git a/exercises/part3-iterators/index.js b/exercises/part3-iterators/index.js index ff4696f..876926a 100644 --- a/exercises/part3-iterators/index.js +++ b/exercises/part3-iterators/index.js @@ -16,7 +16,15 @@ Instructions: Write a function which counts the number of times a value occurs i Example: countItem(['a', 'b', 'a'], 'a') should return 2 ===================== */ -let countItem = (arr, item) => {}; +let countItem = (arr, item) => { + let count = 0; + for (let i = 0; i < arr.length; i++) { + if (arr[i] === item){ + count++; + } + } + return count; +}; console.log('countItem success:', countItem([1, 2, 3, 4, 5, 4, 4], 4) === 3); @@ -27,8 +35,23 @@ Instructions: Write a function which takes an array and returns a new array with original array. ===================== */ -let isEven = (n) => {}; -let filterEven = (arr) => {}; +let isEven = (x) => { + if (x%2 === 0){ + return (x%2 === 0); + } else { + return ("NA"); + } +}; + +let filterEven = (arr) => { + let evenArr = []; + for (i = 0; i < arr.length; i++){ + if (isEven(arr[i]) === true ){ + evenArr.push(arr[i]) + } + } + return evenArr +}; console.log('filterEven success:', _(filterEven([1, 2, 3, 4, 5, 4, 4])).isEqual([2, 4, 4, 4])); @@ -38,8 +61,23 @@ Instructions: Write a function which takes an array and returns a new array with function MUST use the isOdd function and MUST NOT change the original array. ===================== */ -let isOdd = (n) => {}; -let filterOdd = (arr) => {}; +let isOdd = (x) => { + if (x%2 != 0){ + return (x%2 !=0) + } else { + return ("NA") + } +}; + +let filterOdd = (arr) => { + let oddArr =[]; + for (i=0; i {}; -let mapIncrement = (arr) => {}; +let plusOne = (x) => { + return x+1 +}; +let mapIncrement = (arr) => { + let plusArr = []; + for (i=0; i {}; -let mapTriple = (arr) => {}; +let timesThree = (x) => { + return x*3 +}; +let mapTriple = (arr) => { + let tripleArr = []; + for (i=0; i {}; -let reduceSum = (arr) => {}; +let add = (x, y) => { + return x+y +}; + +let reduceSum = (arr) => { + let totalSum = 0; + for (i=0; i {}; -let reduceProduct = (arr) => {}; +let multiply = (x, y) => { + return x*y +}; +let reduceProduct = (arr) => { + mtpl = 1; + for (i=0; i {}; +let isOdd = (x) => { + if (x%2 != 0){ + return (x%2 !=0) + } else { + return ("NA") + } +}; + +let isEven = (x) => { + if (x%2 === 0){ + return (x%2 === 0); + } else { + return ("NA"); + } +}; + +let filter = (arr, pred) => { + let newArr = []; + for (let i = 0; i < arr.length; i++) { + if (pred(arr[i])) { + newArr.push(arr[i]); + } + } + return newArr; +}; + console.log('filter success #1:', _(filter([1, 2, 3, 4, 5, 4, 4], isEven)).isEqual([2, 4, 4, 4])); console.log('filter success #2:', _(filter([1, 2, 3, 4, 5, 4, 4], isOdd)).isEqual([1, 3, 5])); @@ -27,7 +52,35 @@ Instructions: Write a function which takes an array and returns a new array, where each item has a function applied to it. ===================== */ -let map = (arr, func) => {}; +let plusOne = (x) => { + return x+1 +}; +let mapIncrement = (arr) => { + let plusArr = []; + for (i=0; i { + return x*3 +}; +let mapTriple = (arr) => { + let tripleArr = []; + for (i=0; i { + let newArr = []; + for (let i = 0; i < arr.length; i++) { + newArr.push(func(arr[i])); + } + return newArr; +}; console.log('map success #1:', _(map([1, 2, 3, 4, 5, 4, 4], plusOne)).isEqual([2, 3, 4, 5, 6, 5, 5])); console.log('map success #2:', _(map([1, 2, 3, 4, 5, 4, 4], timesThree)).isEqual([3, 6, 9, 12, 15, 12, 12])); @@ -51,12 +104,29 @@ Instructions: Write a function which takes an array and returns the value of add( 9 , 7 ); // 16 <-- final reduced value ===================== */ +let add = (x, y) => { + return x + y +} + +let multiply = (x, y) => { + return x * y +} + +let reduce = (arr, func, initial) => { + let final = arr[initial]; + for (i = 0; i < arr.length; i++) { + if (i != initial) { + final = func (arr[i], final) + } + } + return final +}; + -let reduce = (arr, func, initial) => {}; console.log('reduce success #1:', reduce([1, 2, 3, 4, 5, 4, 4], add, 0) === 23); console.log('reduce success #2:', reduce([1, 2, 3, 4, 5, 4, 4], multiply, 1) === 1920); -console.log('reduce success #3:', _(reduce([1, 2, 3, 4, 5, 4, 4], (x, y) => [y, ...x], [])).isEqual([4, 4, 5, 4, 3, 2, 1])); +console.log('reduce success #3:', (reduce([1, 2, 3, 4, 5, 4, 4], (x, y) => [y, ...x], [])).isEqual([4, 4, 5, 4, 3, 2, 1])); /* ===================== Bonus: Create a function called sumSquares that takes an array and returns @@ -65,6 +135,30 @@ Bonus: Create a function called sumSquares that takes an array and returns `multiply` functions that you developed before). ===================== */ -let sumSquares = (arr) => {}; +let add = (x, y) => { + return x + y +} + +let multiply = (x, y) => { + return x * y +} + +let reduce = (arr, func, initial) => { + let final = arr[initial]; + for (i = 0; i < arr.length; i++) { + if (i != initial) { + final = func (arr[i], final) + } + } + return final +}; + +let sumSquares = (arr) => { + let result = []; + for (i = 0; i < arr.length; i++){ + result.push(multiply(arr[i],arr[i])) + } + return reduce(result, add, 0) +}; console.log('sumSquares success:', sumSquares([1, 2, 3, 4]) === 30);