diff --git a/problems/arrayMethods.js b/problems/arrayMethods.js index 74bdd40..88a0bc1 100644 --- a/problems/arrayMethods.js +++ b/problems/arrayMethods.js @@ -5,7 +5,17 @@ * @returns {number} Sum of all numbers */ -const sumArray = () => {}; +const sumArray = (nums) => { + let sum = 0 + nums.forEach((nums) => { + sum += nums + + }) + return sum + +}; + +// test: console.log(sumArray([1, 1, 1, 1])) /** * Takes in an array of numbers and returns the amount of zeros that occur in it. @@ -14,7 +24,12 @@ const sumArray = () => {}; * Must use forEach */ -const zeroCount = () => {}; +const zeroCount = (nums) => { + let numOfZero = 0 + nums.forEach(num => num === 0 ? numOfZero += 1 : false) + return numOfZero + +}; /** * Takes in an array of numbers and returns a new array where each @@ -24,7 +39,13 @@ const zeroCount = () => {}; * @returns {number[]} Array with each previous number plus 10. */ -const plusTen = () => {}; +const plusTen = (nums) => { + return nums.map(num => { + return num += 10 + + }) +} + /** * Takes in an array and returns a new array where every non-string @@ -36,7 +57,21 @@ const plusTen = () => {}; * @returns {string[]} All strings remain, all non strings are now empty strings. */ -const stringsOnly = () => {}; +const stringsOnly = (items) => { + + return items.map(item => { + if(typeof(item) !== 'string'){ + return "" + } + else{ + return item + } + + + }) +} + + /** * Takes in an array and returns a new array with only the even elements. @@ -45,7 +80,15 @@ const stringsOnly = () => {}; * @returns {number[]} Only even valued elements */ -const onlyEvens = () => {}; +const onlyEvens = (nums) => { + + return nums.filter(num => { + return num % 2 === 0 + + + }) + +} /** * Takes in an array and returns a new array with only the elements @@ -55,7 +98,16 @@ const onlyEvens = () => {}; * @returns {number[]} Only number items should remain. */ -const numbersOnly = () => {}; +const numbersOnly = (items) => { + return items.filter(item =>{ + if (typeof(item) === 'number'){ + return true + } + }) +} + +// test: console.log(numbersOnly(["cat", 0, { hi: "corey" }, [], 2, -3])) + /** * Takes in an array and checks if all elements in the array are the same. @@ -64,7 +116,16 @@ const numbersOnly = () => {}; * @returns {boolean} Whether or not all elements are the same. */ -const isAllSame = () => {}; +const isAllSame = (items) => { + return items.every(item =>{ + + return items[0] === item + + + }) + + } + /** * Takes in an array and returns whether or not all elements in the array are odd. @@ -72,7 +133,10 @@ const isAllSame = () => {}; * @returns {boolean} All odd? */ -const isAllOdd = () => {}; +const isAllOdd = (nums) => { + return nums.every(num => num % 2 === 1) + +}; /** * Takes in an array of numbers and a target. @@ -82,7 +146,18 @@ const isAllOdd = () => {}; * @returns {number} Number of target occurrences */ -const targetCount = () => {}; +const targetCount = (nums, target) => { +let start = nums[0] +nums.forEach(()=>{}) + + + +} + + + + +}; /** * Takes in an array of elements and returns an Object that contains