Skip to content
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

Create Functions Collection of Functions #9

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<title>fi JS Functional Library</title>
<meta name="description" content="Functional JavaScript Lab in Learn.co Curriculum">
<meta name="author" content="Flatiron School">

<script src = "index.js"></script>
<link rel="stylesheet" href="node_modules/mocha/mocha.css">
</head>

<body>

</body>
</html>
94 changes: 94 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
function makeArrayInput(collection) {
if (Array.isArray(collection) === true) {
return collection.slice()
} else {
return (Object.values(collection))
}
}

function myEach(collection, anyCallback) {
const newData = makeArrayInput(collection)
for (let i = 0; i < newData.length; i++) {
anyCallback(newData[i])
}
return collection
}

function myMap(collection, transformCallback) {
const newData = makeArrayInput(collection)
let newArray = []
for (let value in newData) {
newArray.push(transformCallback(newData[value]))
}
return newArray
}

function myReduce(collection, totalCallback, acc) {
const newData = makeArrayInput(collection)
if(!acc){
acc = newData[0]
newData.shift()
}
newData.forEach((element) => {
acc = totalCallback(acc, element, collection)
})
return acc
}

function myFind(collection, predicate) {
const newData = makeArrayInput(collection)
for (let element of newData) {
if (predicate(element)) {
return element
}
}
return undefined
}

function myFilter(collection, predicate) {
const newData = makeArrayInput(collection)
const newCollection = []
for (let element of newData) {
if (predicate(element)) {
newCollection.push(element)
}
}
return newCollection
}

function mySize(collection) {
const newData = makeArrayInput(collection)
return newData.length
}

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

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

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

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