Skip to content

Implement Custom Collection Processing Functions #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
81 changes: 81 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
const myEach = (collection, callback) => {
Object.values(collection).forEach(callback)
return collection
}

const myMap = (collection, callback) => {
const returnArr = [];
Object.values(collection).forEach((value) => {
returnArr.push(callback(value))
})
return returnArr
}

const myReduce = (collection, callback, accum) => {
let accumulator = accum;
Object.values(collection).forEach((value, index) => {
if (index === 0 && accumulator === undefined) {
accumulator = value;
} else {
accumulator = callback(accumulator, value, Object.values(collection))
}
})
return accumulator
}

const myFind = (collection, predicate) => {
const values = Object.values(collection);
for (let i = 0; i < values.length; i++) {
if (predicate(values[i])) {
return values[i];
}
}
return undefined;
}

const myFilter = (collection, predicate) => {
const values = Object.values(collection);
const returnValues = [];
for (let i = 0; i < values.length; i++) {
if (predicate(values[i])) {
returnValues.push(values[i])
}
}
return returnValues;
}

const mySize = (collection) => {
let count = 0;
Object.values(collection).forEach((_) => count += 1)
return count;
}

const myFirst = (collection, n = 1) => {
const values = Object.values(collection);
const returnValues = [];
for (let i = 0; i < n; i++) {
returnValues.push(values[i])
}
return returnValues.length > 1 ? returnValues : returnValues[0];
}

const myLast = (collection, n = 1) => {
debugger
let result = myFirst(Object.values(collection).reverse(), n)
if (typeof result !== 'number') result = result.reverse()
return result
}

const myKeys = (object) => {
const keys = [];
for (let key in object) {
keys.push(key);
}
return keys;
}

const myValues = (object) => {
const values = [];
myKeys(object).forEach(value => values.push(object[value]))
return values
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "JavaScript Variables Lab for Learn.co",
"main": "index.js",
"scripts": {
"test": "mocha --timeout 5000 -R mocha-multi --reporter-options spec=-,json=.results.json"
"test": "mocha --timeout 5000 -b -R mocha-multi --reporter-options spec=-,json=.results.json"
},
"repository": {
"type": "git",
Expand Down