From 775527155c0fc3042c0fed557bc0f04a428ae885 Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 15:07:23 -0600 Subject: [PATCH 1/8] Solved callbacks problem and started on arrays --- .vscode/settings.json | 3 +++ src/arrays.js | 8 ++++++++ src/callbacks.js | 8 +++++++- 3 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..3b664107 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "git.ignoreLimitWarning": true +} \ No newline at end of file diff --git a/src/arrays.js b/src/arrays.js index 968a168b..470f152b 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -14,12 +14,20 @@ const each = (elements, cb) => { // This only needs to work with arrays. // You should also pass the index into `cb` as the second argument // based off http://underscorejs.org/#each + for (let i = 0; i < elements.length; i++) { + cb(elements[i], i); + } }; const map = (elements, cb) => { // Do NOT use .map, to complete this function. // Produces a new array of values by mapping each value in list through a transformation function (iteratee). // Return the new array. + const newArr = []; + for (let i = 0; i < elements.length; i++) { + newArr.push(cb(elements[i])); + } + return newArr; }; const reduce = (elements, cb, startingValue) => { diff --git a/src/callbacks.js b/src/callbacks.js index 53917475..79d7b6d6 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -1,26 +1,32 @@ const firstItem = (arr, cb) => { // firstItem passes the first item of the given array to the callback function. + cb(arr[0]); }; const getLength = (arr, cb) => { // getLength passes the length of the array into the callback. + cb(arr.length); }; const last = (arr, cb) => { // last passes the last item of the array into the callback. + cb(arr[arr.length - 1]); }; const sumNums = (x, y, cb) => { - // sumNums adds two numbers (x, y) and passes the result to the callback. +// // sumNums adds two numbers (x, y) and passes the result to the callback. + cb(x + y); }; const multiplyNums = (x, y, cb) => { // multiplyNums multiplies two numbers and passes the result to the callback. + cb(x * y); }; const contains = (item, list, cb) => { // contains checks if an item is present inside of the given array/list. // Pass true to the callback if it is, otherwise pass false. + cb(list.includes(item)); }; /* STRETCH PROBLEM */ From 3c896eaef5bca1a7cf736b6d5c31e9902435830e Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 15:53:39 -0600 Subject: [PATCH 2/8] Finished reduce function --- .vscode/launch.json | 14 ++++++++++++++ src/arrays.js | 7 +++++++ 2 files changed, 21 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..73b7a5cd --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,14 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Launch Program", + "program": "${workspaceFolder}/index.js" + } + ] +} \ No newline at end of file diff --git a/src/arrays.js b/src/arrays.js index 470f152b..8d74f835 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -36,6 +36,13 @@ const reduce = (elements, cb, startingValue) => { // Elements will be passed one by one into `cb` along with the `startingValue`. // `startingValue` should be the first argument passed to `cb` and the array element should be the second argument. // `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value. + if (startingValue === undefined) { + startingValue = elements.shift(); + } + for (let i = 0; i < elements.length; i++) { + startingValue = cb(startingValue, elements[i]); + } + return startingValue; }; const find = (elements, cb) => { From 8a6ad6b113051ca4c70321e83498a75a0ed5452b Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 15:54:46 -0600 Subject: [PATCH 3/8] Solved find --- src/arrays.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 8d74f835..57ea6ac7 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -50,6 +50,11 @@ const find = (elements, cb) => { // Look through each value in `elements` and pass each element to `cb`. // If `cb` returns `true` then return that element. // Return `undefined` if no elements pass the truth test. + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) { + return elements[i]; + } + } }; const filter = (elements, cb) => { From 292029a3d474078aa32882919d8cb881fab3dadc Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 15:56:37 -0600 Subject: [PATCH 4/8] Solved filter --- src/arrays.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index 57ea6ac7..a38017bc 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -61,6 +61,13 @@ const filter = (elements, cb) => { // Do NOT use .filter, to complete this function. // Similar to `find` but you will return an array of all elements that passed the truth test // Return an empty array if no elements pass the truth test + const output = []; + for (let i = 0; i < elements.length; i++) { + if (cb(elements[i])) { + output.push(elements[i]); + } + } + return output; }; /* STRETCH PROBLEM */ From c843231ed41ecd4779c02176588f483a2242979a Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 16:35:12 -0600 Subject: [PATCH 5/8] Did flatten --- src/arrays.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/arrays.js b/src/arrays.js index a38017bc..28b82246 100644 --- a/src/arrays.js +++ b/src/arrays.js @@ -75,6 +75,15 @@ const filter = (elements, cb) => { const flatten = (elements) => { // Flattens a nested array (the nesting can be to any depth). // Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4]; + let output = []; + for (let i = 0; i < elements.length; i++) { + if (Array.isArray(elements[i])) { + output = output.concat(flatten(elements[i])); + } else { + output.push(elements[i]); + } + } + return output; }; /* eslint-enable no-unused-vars, max-len */ From 19cf77f5a416790dbfe017aae42dac1ae4f3db01 Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 16:53:05 -0600 Subject: [PATCH 6/8] Finished removeDuplicates --- src/callbacks.js | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/callbacks.js b/src/callbacks.js index 79d7b6d6..108b60f4 100644 --- a/src/callbacks.js +++ b/src/callbacks.js @@ -14,7 +14,7 @@ const last = (arr, cb) => { }; const sumNums = (x, y, cb) => { -// // sumNums adds two numbers (x, y) and passes the result to the callback. + // // sumNums adds two numbers (x, y) and passes the result to the callback. cb(x + y); }; @@ -35,6 +35,13 @@ const removeDuplicates = (array, cb) => { // removeDuplicates removes all duplicate values from the given array. // Pass the duplicate free array to the callback function. // Do not mutate the original array. + const output = []; + for (let i = 0; i < array.length; i++) { + if (!output.includes(array[i])) { + output.push(array[i]); + } + } + cb(output); }; /* eslint-enable */ From eb76cc23ff5457be287caf841d40b6775fd79b44 Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 17:47:53 -0600 Subject: [PATCH 7/8] Finished objects project --- src/objects.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/objects.js b/src/objects.js index 2898d4d4..f4219e61 100644 --- a/src/objects.js +++ b/src/objects.js @@ -5,22 +5,30 @@ const keys = (obj) => { // Retrieve all the names of the object's properties. // Return the keys as strings in an array. // Based on http://underscorejs.org/#keys + return Object.keys(obj); }; const values = (obj) => { // Return all of the values of the object's own properties. // Ignore functions // http://underscorejs.org/#values + return Object.values(obj); }; const mapObject = (obj, cb) => { // Like map for arrays, but for objects. Transform the value of each property in turn. // http://underscorejs.org/#mapObject + const arrKey = keys(obj); + for (let i = 0; i < arrKey.length; i++) { + obj[arrKey[i]] = cb(obj[arrKey[i]]); + } + return obj; }; const pairs = (obj) => { // Convert an object into a list of [key, value] pairs. // http://underscorejs.org/#pairs + return Object.entries(obj); }; /* STRETCH PROBLEMS */ @@ -29,12 +37,25 @@ const invert = (obj) => { // Returns a copy of the object where the keys have become the values and the values the keys. // Assume that all of the object's values will be unique and string serializable. // http://underscorejs.org/#invert + const keysArr = keys(obj); + const dupe = {}; + for (let i = 0; i < keysArr.length; i++) { + dupe[obj[keysArr[i]]] = keysArr[i]; + } + return dupe; }; const defaults = (obj, defaultProps) => { // Fill in undefined properties that match properties on the `defaultProps` parameter object. // Return `obj`. // http://underscorejs.org/#defaults + const keysArr = keys(defaultProps); + for (let i = 0; i < keysArr.length; i++) { + if (obj[keysArr[i]] === undefined) { + obj[keysArr[i]] = defaultProps[keysArr[i]]; + } + } + return obj; }; /* eslint-enable no-unused-vars */ From b5e7c3593aa3a73d3804481e5e366d8bf4e00c9d Mon Sep 17 00:00:00 2001 From: kait-schorr Date: Tue, 20 Feb 2018 18:02:16 -0600 Subject: [PATCH 8/8] Finished objects --- src/objects.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/objects.js b/src/objects.js index f4219e61..2856f5bd 100644 --- a/src/objects.js +++ b/src/objects.js @@ -37,7 +37,7 @@ const invert = (obj) => { // Returns a copy of the object where the keys have become the values and the values the keys. // Assume that all of the object's values will be unique and string serializable. // http://underscorejs.org/#invert - const keysArr = keys(obj); + const keysArr = keys(obj); //Object.keys(obj) const dupe = {}; for (let i = 0; i < keysArr.length; i++) { dupe[obj[keysArr[i]]] = keysArr[i];