Skip to content

Completed JavaScript Functional Library Project #7

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 3 commits into
base: master
Choose a base branch
from
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
92 changes: 92 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const myEach = function(collection, callback) {
for(const item of Object.values(collection)) {
callback(item)
}
return collection
}

const myMap = function (collection, callback) {
let newCollection = []
for(const item of Object.values(collection)) {
newCollection.push(callback(item))
}
return newCollection
}

const myReduce = function(collection, callback, acc) {
let singleValue
let startIndex

if (acc === undefined) {
const values = Object.values(collection)
singleValue = values[0]
startIndex = 1
} else {
singleValue = acc
startIndex = 0
}

const values = Object.values(collection)
for (let i = startIndex; i < values.length; i++) {
singleValue = callback(singleValue, values[i], collection)
}

return singleValue
}

const myFind = function(collection, predicate) {
for( const item of Object.values(collection)) {
if (predicate(item)) {
return item
}
}
}

const myFilter = function(collection, predicate) {
let newCollection = []
for( const item of Object.values(collection)) {
if (predicate(item)) {
newCollection.push(item)
}
} return newCollection
}

const mySize = function(collection) {
let numOfItems = 0
for(let i = 0; i < Object.values(collection).length; i++){
numOfItems += 1
}
return numOfItems
}

const myFirst = function(array, n) {
if (n === undefined) {
return array[0]
} else {
return array.slice(0, n)
}
}

const myLast = function(array, n) {
if (n === undefined) {
return array[array.length - 1]
} else {
return array.slice(-n)
}
}

const myKeys = function(object) {
let keys = []
for(const item in object) {
keys.push(item)
}
return keys
}

const myValues = function(object) {
let values = []
for(const item in object) {
values.push(object[item])
}
return values
}