Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 84 additions & 9 deletions problems/arrayMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -64,15 +116,27 @@ 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.
* @param {number[]} nums
* @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.
Expand All @@ -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
Expand Down