Skip to content

Commit b4314ef

Browse files
committed
Re factored callbacks to fit the testing suite. started tests
1 parent 05928e0 commit b4314ef

3 files changed

Lines changed: 65 additions & 83 deletions

File tree

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: 33 additions & 73 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-
*/
3+
const firstItem = (arr, cb) => {
4+
// firstItem passes the first item of the given array to the callback function.
5+
};
226

23-
// Write a function called firstItem that passes the first item of the given array to the callback function
24-
// code here
7+
const getLength = (arr, cb) => {
8+
// getLength passes the length of the array into the callback.
9+
};
2510

26-
const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'];
11+
const last = (arr, cb) => {
12+
// last passes the last item of the array into the callback.
13+
};
2714

28-
firstItem(foods, (firstItem) => {
29-
console.log(`The first item is ${firstItem}.`);
30-
});
15+
const sumNums = (x, y, cb) => {
16+
// sumNums adds two numbers (x, y) and passes the result to the callback.
17+
};
3118

32-
// Write a function called getLength that passes the length of the array into the callback
33-
// code here
19+
const multiplyNums = (x, y, cb) => {
20+
// multiplyNums multiplies two numbers and passes the result to the callback.
21+
};
3422

35-
getLength(foods, (length) => {
36-
console.log(`The length of the array is ${length}.`);
37-
});
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+
};
3827

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-
});
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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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 callBack', () => {
15+
let itemToTest;
16+
callBackMethods.firstItem(listOfFood, (first) => {
17+
itemToTest = first;
18+
});
19+
expect(itemToTest).toBe(listOfFood[0]);
20+
});
21+
});
22+
});

0 commit comments

Comments
 (0)