Skip to content

Commit fd34665

Browse files
Ala S. AyaadAla S. Ayaad
authored andcommitted
HM1
1 parent d95404a commit fd34665

4 files changed

Lines changed: 56 additions & 49 deletions

File tree

src/arrays.js

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,45 @@
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) => {
8-
// Iterates over a list of elements, yielding each in turn to the `cb` function.
9-
// This only needs to work with arrays.
10-
// You should also pass the index into `cb` as the second argument
11-
// based off http://underscorejs.org/#each
2+
for (let i = 0; i < elements.length; i++) {
3+
cb(elements[i], i);
4+
}
125
};
136

147
const map = (elements, cb) => {
15-
// Produces a new array of values by mapping each value in list through a transformation function (iteratee).
16-
// Return the new array.
8+
const newArray = [];
9+
for (let i = 0; i < elements.length; i++) {
10+
newArray.push(cb(elements[i]));
11+
}
12+
return newArray;
1713
};
1814

1915
const reduce = (elements, cb, startingValue) => {
20-
// Combine all elements into a single value going from left to right.
21-
// Elements will be passed one by one into `cb` along with the `startingValue`.
22-
// `startingValue` should be the first argument passed to `cb` and the array element should be the second argument.
23-
// `startingValue` is the starting value. If `startingValue` is undefined then make `elements[0]` the initial value.
16+
const newElements = elements.slice();
17+
if (startingValue === undefined) {
18+
startingValue = newElements.shift();
19+
}
20+
let memo = startingValue;
21+
each(newElements, (el) => {
22+
memo = cb(memo, el);
23+
});
24+
return memo;
2425
};
2526

2627
const find = (elements, cb) => {
27-
// Look through each value in `elements` and pass each element to `cb`.
28-
// If `cb` returns `true` then return that element.
29-
// Return `undefined` if no elements pass the truth test.
28+
for (let i = 0; i < elements.length; i++) {
29+
if (cb(elements[i])) {
30+
return elements[i];
31+
}
32+
}
3033
};
3134

3235
const filter = (elements, cb) => {
33-
// Similar to `find` but you will return an array of all elements that passed the truth test
34-
// Return an empty array if no elements pass the truth test
36+
const newArr = [];
37+
for (let i = 0; i < elements.length; i++) {
38+
if (cb(elements[i])) {
39+
newArr.push(elements[i]);
40+
}
41+
}
42+
return newArr;
3543
};
3644

3745
/* STRETCH PROBLEM */

src/callbacks.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
1-
/* eslint-disable */
2-
31
const firstItem = (arr, cb) => {
4-
// firstItem passes the first item of the given array to the callback function.
52
cb(arr[0]);
63
};
74

85
const getLength = (arr, cb) => {
9-
// getLength passes the length of the array into the callback.
10-
cb(arr.length); // do i have ur code ryan ? ahh
6+
cb(arr.length);
117
};
128

139
const last = (arr, cb) => {
14-
// last passes the last item of the array into the callback.
10+
cb(arr[arr.length - 1]);
1511
};
1612

1713
const sumNums = (x, y, cb) => {
18-
// sumNums adds two numbers (x, y) and passes the result to the callback.
14+
cb(x + y);
1915
};
2016

2117
const multiplyNums = (x, y, cb) => {
22-
// multiplyNums multiplies two numbers and passes the result to the callback.
18+
cb(x * y);
2319
};
2420

2521
const contains = (item, list, cb) => {
26-
// contains checks if an item is present inside of the given array/list.
27-
// Pass true to the callback if it is, otherwise pass false.
22+
for (let i = 0; i <= list.length; i++) {
23+
if (list[i] === item) {
24+
return cb(true);
25+
}
26+
}
27+
return cb(false);
2828
};
2929

3030
/* STRETCH PROBLEM */

src/closure.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
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
4+
let count = 0;
5+
return () => ++count;
86
};
97

108
const counterFactory = () => {
11-
// Return an object that has two methods called `increment` and `decrement`.
12-
// `increment` should increment a counter variable in closure scope and return it.
13-
// `decrement` should decrement the counter variable and return it.
9+
// let counts = 0;
10+
// return {
11+
// increment: () => ++counts,
12+
// decrement: () => --counts,
1413
};
1514

1615
const limitFunctionCallCount = (cb, n) => {
17-
// Should return a function that invokes `cb`.
18-
// The returned function should only allow `cb` to be invoked `n` times.
19-
};
16+
let count = 0;
17+
return (...args) => {
18+
// watch for n. -> "Base Case"
19+
if (n === count) return null; // once cb is called n amount of times, return null
20+
count++; // else, invoke cb() and increment counter
21+
return cb(...args);
22+
};
23+
}; // The returned function should only allow `cb` to be invoked `n` times.
2024

2125
/* STRETCH PROBLEM */
2226

src/objects.js

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22
// Reference http://underscorejs.org/ for examples.
33

44
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
5+
return Object.keys(obj);
86
};
97

108
const values = (obj) => {
11-
// Return all of the values of the object's own properties.
12-
// Ignore functions
13-
// http://underscorejs.org/#values
9+
return Object.values(obj);
1410
};
1511

1612
const mapObject = (obj, cb) => {
@@ -19,8 +15,7 @@ const mapObject = (obj, cb) => {
1915
};
2016

2117
const pairs = (obj) => {
22-
// Convert an object into a list of [key, value] pairs.
23-
// http://underscorejs.org/#pairs
18+
return Object.entries(obj);
2419
};
2520

2621
/* STRETCH PROBLEMS */

0 commit comments

Comments
 (0)