Skip to content

Commit b155e6d

Browse files
author
Ryan Hamblin
authored
Merge pull request bloominstituteoftechnology#46 from LambdaSchool/callbacks-refactor
Callbacks refactor
2 parents 05928e0 + 7b755c8 commit b155e6d

File tree

4 files changed

+178
-106
lines changed

4 files changed

+178
-106
lines changed

.eslintrc.json

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
{
2-
"extends": "airbnb-base",
3-
"plugins": [
4-
"import"
5-
],
2+
"extends": "airbnb-base",
3+
"plugins": ["import"],
4+
"consistent-return": 0,
5+
"rules": {
6+
"no-param-reassign": 0,
7+
"max-len": 0,
8+
"no-plusplus": 0,
9+
"linebreak-style": 0,
610
"consistent-return": 0,
7-
"rules": {
8-
"no-param-reassign": 0,
9-
"max-len": 0,
10-
"no-plusplus": 0,
11-
"linebreak-style": 0,
12-
"consistent-return": 0,
13-
"no-useless-return": 0,
14-
"no-return-assign": 0,
15-
"arrow-body-style": 0,
16-
"no-unused-vars": 0,
17-
"no-useless-constructor": 0,
18-
"import/no-unresolved": 0
19-
}
11+
"no-useless-return": 0,
12+
"no-return-assign": 0,
13+
"arrow-body-style": 0,
14+
"no-unused-vars": 0,
15+
"no-useless-constructor": 0,
16+
"import/no-unresolved": 0
17+
}
2018
}

package-lock.json

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/callbacks.js

Lines changed: 39 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,43 @@
11
/* eslint-disable */
22

3-
/* For a portion of the assignment your job is to write functions
4-
* so that each function invocation below works. You're working backwards.
5-
*
6-
* There are no tests for this file.
7-
*
8-
* Example:
9-
*
10-
* greeting('Hey guys', (message) => {
11-
* console.log(message);
12-
* });
13-
*
14-
* You would then define the greeting function to make the invocation work.
15-
*
16-
*
17-
* const greeting = (str, cb) => {
18-
* cb(str);
19-
* };
20-
*
21-
*/
22-
23-
// Write a function called firstItem that passes the first item of the given array to the callback function
24-
// code here
25-
26-
const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'];
27-
28-
firstItem(foods, (firstItem) => {
29-
console.log(`The first item is ${firstItem}.`);
30-
});
31-
32-
// Write a function called getLength that passes the length of the array into the callback
33-
// code here
34-
35-
getLength(foods, (length) => {
36-
console.log(`The length of the array is ${length}.`);
37-
});
38-
39-
// Write a function called last which passes the last item of the array into the callback
40-
// code here
41-
42-
last(foods, (lastItem) => {
43-
console.log(`The last item in the array is ${lastItem}.`);
44-
});
45-
46-
// Write a function called sumNums that adds two numbers and passes the result to the callback
47-
// code here
48-
49-
sumNums(5, 10, (sum) => {
50-
console.log(`The sum is ${sum}.`);
51-
});
52-
53-
// Write a function called multiplyNums that multiplies two numbers and passes the result to the callback
54-
// code here
55-
56-
multiplyNums(5, 10, (product) => {
57-
console.log(`The product is ${product}.`);
58-
});
59-
60-
// Write a function called contains that checks if an item is present inside of the given array.
61-
// Pass true to the callback if it is, otherwise pass false
62-
// code here
63-
64-
contains(foods, 'ribeye', (result) => {
65-
console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array');
66-
});
67-
68-
// Write a function called removeDuplicates that removes all duplicate values from the given array.
69-
// Pass the array to the callback function. Do not mutate the original array.
70-
// code here
71-
72-
removeDuplicates(foods, (uniqueFoods) => {
73-
console.log(`foods with duplicates removed: ${uniqueFoods}`);
74-
});
75-
76-
// Write a function called forEach that iterates over the provided array and passes the value and index into the callback.
77-
// code here
78-
79-
forEach(foods, (value, index) => {
80-
console.log(`${value} is at index ${index}.`);
81-
});
3+
const firstItem = (arr, cb) => {
4+
// firstItem passes the first item of the given array to the callback function.
5+
};
6+
7+
const getLength = (arr, cb) => {
8+
// getLength passes the length of the array into the callback.
9+
};
10+
11+
const last = (arr, cb) => {
12+
// last passes the last item of the array into the callback.
13+
};
14+
15+
const sumNums = (x, y, cb) => {
16+
// sumNums adds two numbers (x, y) and passes the result to the callback.
17+
};
18+
19+
const multiplyNums = (x, y, cb) => {
20+
// multiplyNums multiplies two numbers and passes the result to the callback.
21+
};
22+
23+
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.
26+
};
27+
/* STRETCH PROBLEM */
28+
const removeDuplicates = (array, cb) => {
29+
// removeDuplicates removes all duplicate values from the given array.
30+
// Pass the duplicate free array to the callback function.
31+
// Do not mutate the original array.
32+
};
8233

8334
/* eslint-enable */
35+
module.exports = {
36+
firstItem,
37+
getLength,
38+
last,
39+
sumNums,
40+
multiplyNums,
41+
contains,
42+
removeDuplicates,
43+
};

tests/callbacks.test.js

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
const callBackMethods = require('../src/callbacks');
2+
/* eslint-disable no-undef */
3+
4+
describe('callback functions', () => {
5+
const listOfFood = [
6+
'cheetoes',
7+
'bananas',
8+
'turkey leg',
9+
'cobbler',
10+
'bananas',
11+
];
12+
13+
describe('firstItem', () => {
14+
it('should pass the first item in the list to the callBack', () => {
15+
let itemToTest;
16+
callBackMethods.firstItem(listOfFood, (first) => {
17+
itemToTest = first;
18+
});
19+
expect(itemToTest).toBe(listOfFood[0]);
20+
});
21+
it('should call the callback with the expected input', () => {
22+
const mock = jest.fn();
23+
callBackMethods.firstItem(listOfFood, mock);
24+
expect(mock).toBeCalledWith('cheetoes');
25+
});
26+
});
27+
28+
describe('getLength', () => {
29+
it('should pass back the length of the array to the callback', () => {
30+
let len = 0;
31+
callBackMethods.getLength(listOfFood, (length) => {
32+
len = length;
33+
});
34+
expect(len).toBe(5);
35+
});
36+
it('should call the callback with the expected input', () => {
37+
const mock = jest.fn();
38+
callBackMethods.getLength(listOfFood, mock);
39+
expect(mock).toBeCalledWith(5);
40+
});
41+
});
42+
43+
describe('last', () => {
44+
it('should pass back the last item in the array to the callback', () => {
45+
let item;
46+
callBackMethods.last(listOfFood, (lastItem) => {
47+
item = lastItem;
48+
});
49+
expect(item).toBe('bananas');
50+
});
51+
it('should call the given callback with the expected input', () => {
52+
const mock = jest.fn();
53+
callBackMethods.last(listOfFood, mock);
54+
expect(mock).toBeCalledWith('bananas');
55+
});
56+
});
57+
58+
describe('sumNums', () => {
59+
it('should pass back the result of the added nums to the callback', () => {
60+
let result = 0;
61+
callBackMethods.sumNums(10, 20, (res) => {
62+
result = res;
63+
});
64+
expect(result).toBe(30);
65+
});
66+
it('should call the given callback with the expected input', () => {
67+
const mock = jest.fn();
68+
callBackMethods.sumNums(11, 22, mock);
69+
expect(mock).toBeCalledWith(33);
70+
});
71+
});
72+
73+
describe('multiplyNums', () => {
74+
it('should pass back the result of the multiplied nums to the callback', () => {
75+
let result = 0;
76+
callBackMethods.multiplyNums(10, 20, (res) => {
77+
result = res;
78+
});
79+
expect(result).toBe(200);
80+
});
81+
it('should call the given callback with the expected input', () => {
82+
const mock = jest.fn();
83+
callBackMethods.multiplyNums(5, 11, mock);
84+
expect(mock).toBeCalledWith(55);
85+
});
86+
});
87+
88+
describe('contains', () => {
89+
it('should pass back true if item is in array', () => {
90+
let flag;
91+
callBackMethods.contains('bananas', listOfFood, (truf) => {
92+
flag = truf;
93+
});
94+
expect(flag).toBe(true);
95+
});
96+
it('should pass back false if item is NOT in array', () => {
97+
let flag;
98+
callBackMethods.contains('boat', listOfFood, (truf) => {
99+
flag = truf;
100+
});
101+
expect(flag).toBe(false);
102+
});
103+
});
104+
105+
describe('removeDuplicates', () => {
106+
it('should remove any duplicate items from the array', () => {
107+
let arrToTest = [];
108+
callBackMethods.removeDuplicates(listOfFood, (arrayFreeOfDups) => {
109+
arrToTest = arrayFreeOfDups;
110+
});
111+
expect(arrToTest.length).toBe(4);
112+
});
113+
});
114+
});

0 commit comments

Comments
 (0)