Skip to content

Commit d74454b

Browse files
committed
problems not solved?
closure cacheFunction and object defualt creation
1 parent c96819c commit d74454b

4 files changed

Lines changed: 5 additions & 52 deletions

File tree

src/arrays.js

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
// Complete the following functions.
2-
// These functions only need to work with arrays.
3-
// Do NOT use the built in array methods to solve these. forEach, map, reduce, filter, includes, etc.
4-
// You CAN use concat, push, pop, etc. but do not use the exact method that you are replicating
5-
// You can use the functions that you have already written to help solve the other problems
6-
71
const each = (elements, cb) => {
82
for (let i = 0; i < elements.length; i++) {
93
cb(elements[i], i);
@@ -19,21 +13,18 @@ const map = (elements, cb) => {
1913
};
2014

2115
const reduce = (elements, cb, startingValue) => {
22-
const newElements = elements.slice(); // creating a new array
23-
if (!startingValue) { // if its not pointing on the 1st element
24-
startingValue = newElements.shift(); // shift return the 1st el
16+
const newElements = elements.slice();
17+
if (!startingValue) {
18+
startingValue = newElements.shift();
2519
}
26-
let memo = startingValue; // this is memoazation for the staringValue
27-
each(newElements, (el) => { // run a forEach on el and a callback where memo refrence the cb
20+
let memo = startingValue;
21+
each(newElements, (el) => {
2822
memo = cb(memo, el);
2923
});
3024
return memo;
3125
};
3226

3327
const find = (elements, cb) => {
34-
// Look through each value in `elements` and pass each element to `cb`.
35-
// If `cb` returns `true` then return that element.
36-
// Return `undefined` if no elements pass the truth test.
3728
for (let i = 0; i < elements.length; i++) {
3829
if (cb(elements[i])) {
3930
return elements[i];
@@ -42,8 +33,6 @@ const find = (elements, cb) => {
4233
};
4334

4435
const filter = (elements, cb) => {
45-
// Similar to `find` but you will return an array of all elements that passed the truth test
46-
// Return an empty array if no elements pass the truth test
4736
const newArr = [];
4837
for (let i = 0; i < elements.length; i++) {
4938
if (cb(elements[i])) {
@@ -56,10 +45,6 @@ const filter = (elements, cb) => {
5645
/* STRETCH PROBLEM */
5746

5847
const flatten = (elements) => {
59-
// Flattens a nested array (the nesting can be to any depth).
60-
// Example: flatten([1, [2], [3, [[4]]]]); => [1, 2, 3, 4];
61-
// const reducedArr = elements.reduce((a, n) => a.concat(n), []);
62-
// return reducedArr;
6348
const newArr = [];
6449
Object.values(elements).forEach((e) => {
6550
if (typeof e === 'number') {

src/callbacks.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ const multiplyNums = (x, y, cb) => {
2121
};
2222

2323
const contains = (item, list, cb) => {
24-
// contains checks if an item is present inside of the given array/list.
25-
// Pass true to the callback if it is, otherwise pass false.
2624
for (let i = 0; i < list.length; i++ ) {
2725
if (item === list[i]) return cb(true);
2826
}
@@ -32,9 +30,6 @@ const contains = (item, list, cb) => {
3230
/* STRETCH PROBLEM */
3331

3432
const removeDuplicates = (array, cb) => {
35-
// removeDuplicates removes all duplicate values from the given array.
36-
// Pass the duplicate free array to the callback function.
37-
// Do not mutate the original array.
3833
const newObj = {};
3934
Object.keys(array).map(key => newObj[array[key]] = array[key]);
4035
const newArr = Object.keys(newObj);

src/closure.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,11 @@
11
// Complete the following functions.
22

33
const counter = () => {
4-
// Return a function that when invoked increments and returns a counter variable.
5-
// Example: const newCounter = counter();
6-
// newCounter(); // 1
7-
// newCounter(); // 2
84
let count = 0;
95
return () => ++count;
106
};
117

128
const counterFactory = () => {
13-
// Return an object that has two methods called `increment` and `decrement`.
14-
// `increment` should increment a counter variable in closure scope and return it.
15-
// `decrement` should decrement the counter variable and return it.
169
let fCounter = 0;
1710
return {
1811
increment() {
@@ -25,8 +18,6 @@ const counterFactory = () => {
2518
};
2619

2720
const limitFunctionCallCount = (cb, n) => {
28-
// Should return a function that invokes `cb`.
29-
// The returned function should only allow `cb` to be invoked `n` times.
3021
return () => {
3122
for (let i = 0; i <= n; i++) {
3223
return cb();

src/objects.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,19 @@
1-
// Complete the following underscore functions.
2-
// Reference http://underscorejs.org/ for examples.
3-
41
const keys = (obj) => {
5-
// Retrieve all the names of the object's properties.
6-
// Return the keys as strings in an array.
7-
// Based on http://underscorejs.org/#keys
8-
// array like object
92
return Object.keys(obj);
103
};
114

125
const values = (obj) => {
13-
// Return all of the values of the object's own properties.
14-
// Ignore functions
15-
// http://underscorejs.org/#values
16-
176
// return Object.values(obj) best solution!
187
return Object.keys(obj).map(value => obj[value]);
198
};
209

2110
const mapObject = (obj, cb) => {
22-
// Like map for arrays, but for objects. Transform the value of each property in turn.
23-
// http://underscorejs.org/#mapObject
2411
const newObj = {};
2512
Object.keys(obj).map(key => newObj[key] = obj[key]);
2613
return cb(newObj);
2714
};
2815

2916
const pairs = (obj) => {
30-
// Convert an object into a list of [key, value] pairs.
31-
// http://underscorejs.org/#pairs
3217
const newArr = [];
3318
Object.keys(obj).map((key, i, arr) => newArr.push([arr[i], obj[key]]));
3419
return newArr;
@@ -37,9 +22,6 @@ const pairs = (obj) => {
3722
/* STRETCH PROBLEMS */
3823

3924
const invert = (obj) => {
40-
// Returns a copy of the object where the keys have become the values and the values the keys.
41-
// Assume that all of the object's values will be unique and string serializable.
42-
// http://underscorejs.org/#invert
4325
const newObj = {};
4426
Object.keys(obj).map((key, i, arr) => newObj[obj[key]] = arr[i]);
4527
return newObj;

0 commit comments

Comments
 (0)