From d50c718c1a598eee3758780a8b3d5b8fd880859c Mon Sep 17 00:00:00 2001 From: Ivan Castillo Date: Wed, 13 Jan 2021 10:17:14 -0500 Subject: [PATCH 1/2] First commit --- problems/arrayMethods.js | 129 ++++++++++++++++++++++++++++++++++++--- 1 file changed, 119 insertions(+), 10 deletions(-) diff --git a/problems/arrayMethods.js b/problems/arrayMethods.js index 74bdd40..eec8d62 100644 --- a/problems/arrayMethods.js +++ b/problems/arrayMethods.js @@ -5,7 +5,21 @@ * @returns {number} Sum of all numbers */ -const sumArray = () => {}; +const sumArray = (nums) => { + let sum = 0 + nums.forEach((num) => { + sum = sum + num + }) + return sum +}; + +console.log(sumArray([13, 2, 5, 5])) + + // let sum = 0 + // nums.forEach((num) => { + // sum = sum + num + // }) + // return sum /** * Takes in an array of numbers and returns the amount of zeros that occur in it. @@ -14,7 +28,17 @@ const sumArray = () => {}; * Must use forEach */ -const zeroCount = () => {}; +const zeroCount = (nums) => { + let zeros = 0 + nums.forEach((num) => { + if (num === 0) { + zeros += 1 + } + }) + return zeros +}; + +console.log(zeroCount([13, 0, 5, 0])) /** * Takes in an array of numbers and returns a new array where each @@ -24,7 +48,14 @@ const zeroCount = () => {}; * @returns {number[]} Array with each previous number plus 10. */ -const plusTen = () => {}; +const plusTen = (nums) => { + let addTen = nums.map((num) => { + return num + 10 + }) + return addTen +}; + +console.log(plusTen([13, 0, 5, 0])) /** * Takes in an array and returns a new array where every non-string @@ -36,7 +67,19 @@ const plusTen = () => {}; * @returns {string[]} All strings remain, all non strings are now empty strings. */ -const stringsOnly = () => {}; +const stringsOnly = (items) => { + let newArray = items.map((item) => { + if (typeof item === "string") { + return item + } + else { + return "" + } + }) + return newArray +}; + +console.log(stringsOnly(["a", 123, "b", "c", {name: "cat"}])) /** * Takes in an array and returns a new array with only the even elements. @@ -45,7 +88,20 @@ const stringsOnly = () => {}; * @returns {number[]} Only even valued elements */ -const onlyEvens = () => {}; +const onlyEvens = (nums) => { + // let evenNumber = nums.filter(num => + // num % 2 === 0 + // ) + // return evenNumber + let evenNumber = nums.filter((num) => { + if (num % 2 === 0) { + return true + } + }) + return evenNumber +}; + +console.log(onlyEvens([1, 2, 3, 4, 5])) /** * Takes in an array and returns a new array with only the elements @@ -55,7 +111,17 @@ const onlyEvens = () => {}; * @returns {number[]} Only number items should remain. */ -const numbersOnly = () => {}; +const numbersOnly = (items) => { + let onlyNumbers = items.filter((item) => { + if (typeof item === "number") { + return true + } + }) + return onlyNumbers +}; + +console.log(numbersOnly(["cat", "dog"])) +console.log(numbersOnly([1, 2, 3])) /** * Takes in an array and checks if all elements in the array are the same. @@ -64,7 +130,22 @@ const numbersOnly = () => {}; * @returns {boolean} Whether or not all elements are the same. */ -const isAllSame = () => {}; +const isAllSame = (items) => { + let same = items.every((item) => { + if (items[0] === item) { + return true + } + }) + return same +}; + +console.log(isAllSame(["cat", "cat", "cat", "cat"])) +console.log(isAllSame([1, 2, 3])) +console.log(isAllSame(["cat", "dog"])) +console.log(isAllSame([1, 1, 1])) + + + /** * Takes in an array and returns whether or not all elements in the array are odd. @@ -72,7 +153,20 @@ const isAllSame = () => {}; * @returns {boolean} All odd? */ -const isAllOdd = () => {}; +const isAllOdd = (nums) => { + let odds = nums.every((num) => { + if (num % 2 === 0) { + return false + } + else { + return true + } + }) + return odds +}; + +console.log(isAllOdd([1, 3, 5, 7])) +console.log(isAllOdd([0, 1, 3, 2])) /** * Takes in an array of numbers and a target. @@ -82,7 +176,18 @@ const isAllOdd = () => {}; * @returns {number} Number of target occurrences */ -const targetCount = () => {}; +const targetCount = (nums, target) => { + let targetNumbers = 0 + nums.forEach((num) => { + if (target === num) { + targetNumbers++ + } + }) + return targetNumbers +}; + +console.log(targetCount([13, 0, 5, 5, 0], 0)) +console.log(targetCount([], [2])) /** * Takes in an array of elements and returns an Object that contains @@ -91,7 +196,11 @@ const targetCount = () => {}; * @returns {Object} A mapping of elements to their occurrence count. */ -const counterObject = () => {}; +const counterObject = (items) => { + +}; + +console.log(counterObject([1, 1, 2, 1, 3])) /** * Takes in an array of numbers and returns a new array with each element doubled. From f91842b79f9946dadc5b1aac521551458a7da227 Mon Sep 17 00:00:00 2001 From: Ivan Castillo Date: Thu, 28 Jan 2021 02:09:27 -0500 Subject: [PATCH 2/2] Final commit. Finished the last lab in Module 1 --- problems/arrayMethods.js | 86 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 7 deletions(-) diff --git a/problems/arrayMethods.js b/problems/arrayMethods.js index eec8d62..ed999cf 100644 --- a/problems/arrayMethods.js +++ b/problems/arrayMethods.js @@ -197,7 +197,23 @@ console.log(targetCount([], [2])) */ const counterObject = (items) => { - + // take in a group of items (array) + // we want to know how many of each item there are + // create an empty object + let newObject = {} + // iterate through the array of items + // we store these items and amounts in key/value pairs inside the object + // first iteration stores each key into the object with a value of 0 + items.forEach((item) => { + if (newObject[item] === undefined) { + newObject[item] = 1 + } //{1:3, 2:1, 3:1} + else { + newObject[item] ++ + } + }) + + return newObject }; console.log(counterObject([1, 1, 2, 1, 3])) @@ -208,8 +224,22 @@ console.log(counterObject([1, 1, 2, 1, 3])) * @returns {number[]} Each element has been doubled */ -const doubled = () => {}; +const doubled = (nums) => { + // function multiplyByTwo(num) { //this block of code creates a seperate function that doubles the + // return num * 2 //item plugged into the parameter. + // } //The code then uses this function to plug into nums.map. This strategy + //might be used to apply the double function to different maps? + // let doubleNum = nums.map(multiplyByTwo) + + // return doubleNum + let doubleNum = nums.map((num) => { //this block of code creates one function with map, and applies + return num * 2 //the function to every item in nums + }) + return doubleNum +}; + +console.log(doubled([1, 2, 3, 4, 5])) /** * Takes in an array and returns whether every number is less than 20 even after * being tripled. @@ -217,7 +247,15 @@ const doubled = () => {}; * @returns {boolean} Each number times 3 less than 20 ? */ -const tripledAndLessThan20 = () => {}; +const tripledAndLessThan20 = (nums) => { + let tripleLess20 = nums.every((num) => { + return (num * 3) < 20 + }) + return tripleLess20 +}; + +console.log(tripledAndLessThan20([1, 10, 5, 8, 6, 3, 19])) +console.log(tripledAndLessThan20([1, 5, 3, 6])) /** * Takes in a divisor and an array of numbers. @@ -228,7 +266,15 @@ const tripledAndLessThan20 = () => {}; * @returns {number[]} Numbers evenly divided by divisor. */ -const divisibleBy = () => {}; +const divisibleBy = (divisor, nums) => { + let divide = nums.filter((num) => { + return num % divisor === 0 + }) + return divide +}; + +console.log(divisibleBy(2, [1, 2, 3, 4, 5])) +console.log(divisibleBy(2, [2, 4, 6, 8, 10])) /** * Takes in an array and returns a new array where each element @@ -239,7 +285,13 @@ const divisibleBy = () => {}; * @returns {number[]} Numbers times their index */ -const numberTimesIdx = () => {}; +const numberTimesIdx = (nums) => { + let index = 0; + + return nums.map((num) => num * index++) // Is there another way of doing this? +}; // (besides traditional function definition) + +console.log(numberTimesIdx([6,7,8,9])) /** * Takes in an array of numbers and returns whether or not every value @@ -254,7 +306,17 @@ const numberTimesIdx = () => {}; * @returns {boolean} Are all numbers positive, even, and not ending in 0. */ -const arePositiveEvenAndNonZeroEnding = () => {}; +const arePositiveEvenAndNonZeroEnding = (nums) => { + return nums.every((num) => { + return num % 10 !== 0 && num % 2 === 0 && num >= 0 // is there another way to determine if all positive + }) + +}; + +console.log(arePositiveEvenAndNonZeroEnding([10])) +console.log(arePositiveEvenAndNonZeroEnding([2, 4, 6, 8])) +console.log(arePositiveEvenAndNonZeroEnding([2, 4, -6, 6, 8])) +console.log(arePositiveEvenAndNonZeroEnding([2, 4, 6, 8, 10])) /** * Takes in an array of elements. @@ -265,7 +327,17 @@ const arePositiveEvenAndNonZeroEnding = () => {}; * @returns {string[]} All strings uppercase. */ -const stringsAndCaps = () => {}; +const stringsAndCaps = (items) => { + let newArr = items.filter((item) => { + return typeof(item) === typeof("string") + }) + let capsArr = newArr.map((item) => { + return item.toUpperCase() + }) + return capsArr +}; + +console.log(stringsAndCaps([{}, 1, "cat", 3, ["hi"], {name: "dog"}, "dog", "bear"])) module.exports = { sumArray,