From f0e2e0ea018b6995b65225405136de8656759b94 Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Tue, 4 Jun 2024 14:32:08 -0400 Subject: [PATCH 01/10] Add script tag and create function to check if collection is an array --- index.html | 4 ++-- index.js | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/index.html b/index.html index 04b833e41..3d36e0aec 100644 --- a/index.html +++ b/index.html @@ -6,11 +6,11 @@ fi JS Functional Library - + - + diff --git a/index.js b/index.js index e69de29bb..b08ba5bfd 100644 --- a/index.js +++ b/index.js @@ -0,0 +1,21 @@ +//need to create function that test if collection is an array or object +// Array.isArray(value) + +// if object create an array + +// Object.values(obj) = an array of property values of object + +// if array make a copy so data is not changed +// const collection = {one: 1, two: 2, three: 3} +const collection = [1, 5, 3] + +function makeArrayInput (collection) { + if (Array.isArray(collection) === true) { + return collection + } else { + return (Object.values(collection)) + } +} + + +makeArrayInput(collection) From eb95afcad59283d4068850ebcfce5cc7797c3f2b Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Tue, 4 Jun 2024 15:08:41 -0400 Subject: [PATCH 02/10] Create myEach function --- index.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/index.js b/index.js index b08ba5bfd..47da312e2 100644 --- a/index.js +++ b/index.js @@ -1,14 +1,3 @@ -//need to create function that test if collection is an array or object -// Array.isArray(value) - -// if object create an array - -// Object.values(obj) = an array of property values of object - -// if array make a copy so data is not changed -// const collection = {one: 1, two: 2, three: 3} -const collection = [1, 5, 3] - function makeArrayInput (collection) { if (Array.isArray(collection) === true) { return collection @@ -17,5 +6,20 @@ function makeArrayInput (collection) { } } +// takes two arguments (collection, callback) +//iterates over collection passing each element into callback +// for loop +// let i = 0; i < collection.length; i++ +// returns the the orginal collection +// const collection = [1, 2, 3] +const collection = {one: 1, two: 2, three: 3} + +function myEach(collection, alert) { + const newArray = makeArrayInput(collection) + for (let i = 0; i < newArray.length; i++) { + alert(newArray[i]) + } + return collection +} -makeArrayInput(collection) +// was not have it pass with object need to add array maker From bfe0dfe033319b5796674801e162a2a29a6d50af Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Tue, 4 Jun 2024 16:26:44 -0400 Subject: [PATCH 03/10] Create myMap function --- index.js | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/index.js b/index.js index 47da312e2..04e1ba8b7 100644 --- a/index.js +++ b/index.js @@ -11,15 +11,31 @@ function makeArrayInput (collection) { // for loop // let i = 0; i < collection.length; i++ // returns the the orginal collection -// const collection = [1, 2, 3] -const collection = {one: 1, two: 2, three: 3} -function myEach(collection, alert) { - const newArray = makeArrayInput(collection) - for (let i = 0; i < newArray.length; i++) { - alert(newArray[i]) + +function myEach(collection, anyCallback) { + const newData = makeArrayInput(collection) + for (let i = 0; i < newData.length; i++) { + anyCallback(newData[i]) } return collection } -// was not have it pass with object need to add array maker +// // const collection = [1, 2, 3] +// const collection = {one: 1, two: 2, three: 3} +// takes a collection and callback function +// use push?? +// transforms data and passes into new array +// returns newArray = [] +function myMap (collection, transformCallback) { + const newData = makeArrayInput(collection) + console.log(collection) + console.log(newData) + let newArray = [] + for (let value in newData) { + console.log(value) + newArray.push(transformCallback(newData[value])) + } + return newArray + +} From cf3f5aa7e977db08e1cfa23399524aeddf440e9c Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Wed, 5 Jun 2024 10:02:29 -0400 Subject: [PATCH 04/10] Create myReduce function --- index.js | 49 ++++++++++++++++++++++++++++++++----------------- 1 file changed, 32 insertions(+), 17 deletions(-) diff --git a/index.js b/index.js index 04e1ba8b7..eb4b46014 100644 --- a/index.js +++ b/index.js @@ -1,18 +1,11 @@ function makeArrayInput (collection) { if (Array.isArray(collection) === true) { - return collection + return collection.slice() } else { return (Object.values(collection)) } } -// takes two arguments (collection, callback) -//iterates over collection passing each element into callback -// for loop -// let i = 0; i < collection.length; i++ -// returns the the orginal collection - - function myEach(collection, anyCallback) { const newData = makeArrayInput(collection) for (let i = 0; i < newData.length; i++) { @@ -21,21 +14,43 @@ function myEach(collection, anyCallback) { return collection } -// // const collection = [1, 2, 3] -// const collection = {one: 1, two: 2, three: 3} -// takes a collection and callback function -// use push?? -// transforms data and passes into new array -// returns newArray = [] function myMap (collection, transformCallback) { const newData = makeArrayInput(collection) - console.log(collection) - console.log(newData) let newArray = [] for (let value in newData) { - console.log(value) newArray.push(transformCallback(newData[value])) } return newArray +} + +// let i = 0; i < collection.length; i++ +// // const collection = [1, 2, 3] +// const collection = {one: 1, two: 2, three: 3} +// arguments are collection, callback, starting value +// iterates through array +//accumulator starts at valued passed in as argument +//each pass callback gets a value +// passed the acc, the current element and reference to collection +//each pass update the accumulator +//returns single value + +function myReduce (collection, totalCallback, acc) { + const newData = makeArrayInput(collection) + if(!acc){ + // acc starts at value of first element + acc = newData[0] + // remove first element from array + // copy newData to new obj + // newData = copyNewData + newData.shift() + } + + + newData.forEach((element) => { + acc = totalCallback(acc, element, collection) + + }) + + return acc } From b1cb70d25639552d5406cd615a78c4b9aa629ca4 Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Wed, 5 Jun 2024 10:15:08 -0400 Subject: [PATCH 05/10] Create myFind function --- index.js | 31 +++++++++++-------------------- 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index eb4b46014..12ca7ecdf 100644 --- a/index.js +++ b/index.js @@ -23,34 +23,25 @@ function myMap (collection, transformCallback) { return newArray } -// let i = 0; i < collection.length; i++ -// // const collection = [1, 2, 3] -// const collection = {one: 1, two: 2, three: 3} -// arguments are collection, callback, starting value -// iterates through array -//accumulator starts at valued passed in as argument -//each pass callback gets a value -// passed the acc, the current element and reference to collection -//each pass update the accumulator -//returns single value - function myReduce (collection, totalCallback, acc) { const newData = makeArrayInput(collection) if(!acc){ - // acc starts at value of first element acc = newData[0] - // remove first element from array - // copy newData to new obj - // newData = copyNewData newData.shift() } - - newData.forEach((element) => { acc = totalCallback(acc, element, collection) - }) - - return acc } + +// predicate returns true or false +function myFind (collection, predicate) { + const newData = makeArrayInput(collection) + for (let element of newData) { + if (predicate(element)) { + return element + } + } + return undefined +} From 69d47727177067acf541ef380ce75080840a54d9 Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Wed, 5 Jun 2024 10:22:45 -0400 Subject: [PATCH 06/10] Create myFilter function --- index.js | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 12ca7ecdf..e82059a54 100644 --- a/index.js +++ b/index.js @@ -35,7 +35,6 @@ function myReduce (collection, totalCallback, acc) { return acc } -// predicate returns true or false function myFind (collection, predicate) { const newData = makeArrayInput(collection) for (let element of newData) { @@ -45,3 +44,15 @@ function myFind (collection, predicate) { } return undefined } + +// returns an array of all that match +function myFilter (collection, predicate) { + const newData = makeArrayInput(collection) + const newCollection = [] + for (let element of newData) { + if (predicate(element)) { + newCollection.push(element) + } + } + return newCollection +} From 3992ab2c8a82b789921b5823be32ba5337241b7a Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Wed, 5 Jun 2024 10:26:31 -0400 Subject: [PATCH 07/10] Create mySize function --- index.js | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index e82059a54..9fea56ab2 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -function makeArrayInput (collection) { +function makeArrayInput(collection) { if (Array.isArray(collection) === true) { return collection.slice() } else { @@ -14,7 +14,7 @@ function myEach(collection, anyCallback) { return collection } -function myMap (collection, transformCallback) { +function myMap(collection, transformCallback) { const newData = makeArrayInput(collection) let newArray = [] for (let value in newData) { @@ -23,7 +23,7 @@ function myMap (collection, transformCallback) { return newArray } -function myReduce (collection, totalCallback, acc) { +function myReduce(collection, totalCallback, acc) { const newData = makeArrayInput(collection) if(!acc){ acc = newData[0] @@ -35,7 +35,7 @@ function myReduce (collection, totalCallback, acc) { return acc } -function myFind (collection, predicate) { +function myFind(collection, predicate) { const newData = makeArrayInput(collection) for (let element of newData) { if (predicate(element)) { @@ -45,8 +45,7 @@ function myFind (collection, predicate) { return undefined } -// returns an array of all that match -function myFilter (collection, predicate) { +function myFilter(collection, predicate) { const newData = makeArrayInput(collection) const newCollection = [] for (let element of newData) { @@ -56,3 +55,9 @@ function myFilter (collection, predicate) { } return newCollection } + +function mySize(collection) { + const newData = makeArrayInput(collection) + return newData.length +} + From 11aa284eeb402ae3cdd61024aefd0e76b4b12ada Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Wed, 5 Jun 2024 10:37:54 -0400 Subject: [PATCH 08/10] Create myFirst function --- index.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/index.js b/index.js index 9fea56ab2..b2786c861 100644 --- a/index.js +++ b/index.js @@ -61,3 +61,12 @@ function mySize(collection) { return newData.length } +function myFirst(array, n) { + if(!n){ + return array[0] + } else { + return array.slice(0, n) + } +} + + From e6fcae237fd044af43ae960e0e9ca8e321f3f8f5 Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Wed, 5 Jun 2024 10:47:54 -0400 Subject: [PATCH 09/10] Create myLast function --- index.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/index.js b/index.js index b2786c861..89a5a8db8 100644 --- a/index.js +++ b/index.js @@ -69,4 +69,12 @@ function myFirst(array, n) { } } +function myLast(array, n) { + if(!n) { + return array[array.length -1] + } else { + return array.slice(-n) + } +} + From e930651882af650f8b04d679a3685c7a52bdac42 Mon Sep 17 00:00:00 2001 From: Anthony Phillips Date: Wed, 5 Jun 2024 11:02:39 -0400 Subject: [PATCH 10/10] Create object function for key and value --- index.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/index.js b/index.js index 89a5a8db8..51d36a97f 100644 --- a/index.js +++ b/index.js @@ -77,4 +77,18 @@ function myLast(array, 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 +}