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
64 changes: 57 additions & 7 deletions problems/arrayMethods.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
* @returns {number} Sum of all numbers
*/

const sumArray = () => {};
const sumArray = (nums) => {
let sumOfNum = 0;
nums.forEach((num) => {
sumOfNum += num;
});
return sumOfNum;
};

/**
* Takes in an array of numbers and returns the amount of zeros that occur in it.
Expand All @@ -14,7 +20,15 @@ const sumArray = () => {};
* Must use forEach
*/

const zeroCount = () => {};
const zeroCount = (nums) => {
let totalAmount = 0;
nums.forEach((element) => {
if (element < 1) {
totalAmount += 1;
}
});
return totalAmount;
};

/**
* Takes in an array of numbers and returns a new array where each
Expand All @@ -24,7 +38,12 @@ const zeroCount = () => {};
* @returns {number[]} Array with each previous number plus 10.
*/

const plusTen = () => {};
const plusTen = (nums) => {
let newArr = nums.map((element) => {
return (element += 10);
});
return newArr;
};

/**
* Takes in an array and returns a new array where every non-string
Expand All @@ -36,7 +55,16 @@ const plusTen = () => {};
* @returns {string[]} All strings remain, all non strings are now empty strings.
*/

const stringsOnly = () => {};
const stringsOnly = (items) => {
let newArr = items.map((element) => {
if (typeof element !== typeof "cat") {
return "";
} else {
return element;
}
});
return newArr;
};

/**
* Takes in an array and returns a new array with only the even elements.
Expand All @@ -45,7 +73,14 @@ const stringsOnly = () => {};
* @returns {number[]} Only even valued elements
*/

const onlyEvens = () => {};
const onlyEvens = (nums) => {
newArr = nums.filter((element) => {
if (element % 2 === 0) {
return true;
}
});
return newArr;
};

/**
* Takes in an array and returns a new array with only the elements
Expand All @@ -55,7 +90,14 @@ const onlyEvens = () => {};
* @returns {number[]} Only number items should remain.
*/

const numbersOnly = () => {};
const numbersOnly = (items) => {
let newArr = items.filter((element) => {
if (typeof element === typeof 0) {
return true;
}
});
return newArr;
};

/**
* Takes in an array and checks if all elements in the array are the same.
Expand All @@ -64,7 +106,15 @@ const numbersOnly = () => {};
* @returns {boolean} Whether or not all elements are the same.
*/

const isAllSame = () => {};
const isAllSame = (items) => {
let newArr = items.every((element) => {
if (element === element) {
return true;
} else {
return false;
}
});
};

/**
* Takes in an array and returns whether or not all elements in the array are odd.
Expand Down