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
213 changes: 197 additions & 16 deletions problems/arrayMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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.
Expand All @@ -64,15 +130,43 @@ 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.
* @param {number[]} nums
* @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.
Expand All @@ -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
Expand All @@ -91,24 +196,66 @@ const targetCount = () => {};
* @returns {Object} A mapping of elements to their occurrence count.
*/

const counterObject = () => {};
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]))

/**
* Takes in an array of numbers and returns a new array with each element doubled.
* @param {number[]} nums
* @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.
* @param {number[]} nums
* @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.
Expand All @@ -119,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
Expand All @@ -130,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
Expand All @@ -145,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.
Expand All @@ -156,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,
Expand Down