Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
{
"extends": "airbnb-base",
"plugins": [
"import"
],
"extends": "airbnb-base",
"plugins": ["import"],
"consistent-return": 0,
"rules": {
"no-param-reassign": 0,
"max-len": 0,
"no-plusplus": 0,
"linebreak-style": 0,
"consistent-return": 0,
"rules": {
"no-param-reassign": 0,
"max-len": 0,
"no-plusplus": 0,
"linebreak-style": 0,
"consistent-return": 0,
"no-useless-return": 0,
"no-return-assign": 0,
"arrow-body-style": 0,
"no-unused-vars": 0,
"no-useless-constructor": 0,
"import/no-unresolved": 0
}
"no-useless-return": 0,
"no-return-assign": 0,
"arrow-body-style": 0,
"no-unused-vars": 0,
"no-useless-constructor": 0,
"import/no-unresolved": 0
}
}
20 changes: 10 additions & 10 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

118 changes: 39 additions & 79 deletions src/callbacks.js
Original file line number Diff line number Diff line change
@@ -1,83 +1,43 @@
/* eslint-disable */

/* For a portion of the assignment your job is to write functions
* so that each function invocation below works. You're working backwards.
*
* There are no tests for this file.
*
* Example:
*
* greeting('Hey guys', (message) => {
* console.log(message);
* });
*
* You would then define the greeting function to make the invocation work.
*
*
* const greeting = (str, cb) => {
* cb(str);
* };
*
*/

// Write a function called firstItem that passes the first item of the given array to the callback function
// code here

const foods = ['pineapple', 'mango', 'ribeye', 'curry', 'tacos', 'ribeye', 'mango'];

firstItem(foods, (firstItem) => {
console.log(`The first item is ${firstItem}.`);
});

// Write a function called getLength that passes the length of the array into the callback
// code here

getLength(foods, (length) => {
console.log(`The length of the array is ${length}.`);
});

// Write a function called last which passes the last item of the array into the callback
// code here

last(foods, (lastItem) => {
console.log(`The last item in the array is ${lastItem}.`);
});

// Write a function called sumNums that adds two numbers and passes the result to the callback
// code here

sumNums(5, 10, (sum) => {
console.log(`The sum is ${sum}.`);
});

// Write a function called multiplyNums that multiplies two numbers and passes the result to the callback
// code here

multiplyNums(5, 10, (product) => {
console.log(`The product is ${product}.`);
});

// Write a function called contains that checks if an item is present inside of the given array.
// Pass true to the callback if it is, otherwise pass false
// code here

contains(foods, 'ribeye', (result) => {
console.log(result ? 'ribeye is in the array' : 'ribeye is not in the array');
});

// Write a function called removeDuplicates that removes all duplicate values from the given array.
// Pass the array to the callback function. Do not mutate the original array.
// code here

removeDuplicates(foods, (uniqueFoods) => {
console.log(`foods with duplicates removed: ${uniqueFoods}`);
});

// Write a function called forEach that iterates over the provided array and passes the value and index into the callback.
// code here

forEach(foods, (value, index) => {
console.log(`${value} is at index ${index}.`);
});
const firstItem = (arr, cb) => {
// firstItem passes the first item of the given array to the callback function.
};

const getLength = (arr, cb) => {
// getLength passes the length of the array into the callback.
};

const last = (arr, cb) => {
// last passes the last item of the array into the callback.
};

const sumNums = (x, y, cb) => {
// sumNums adds two numbers (x, y) and passes the result to the callback.
};

const multiplyNums = (x, y, cb) => {
// multiplyNums multiplies two numbers and passes the result to the callback.
};

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.
};
/* STRETCH PROBLEM */
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.
};

/* eslint-enable */
module.exports = {
firstItem,
getLength,
last,
sumNums,
multiplyNums,
contains,
removeDuplicates,
};
114 changes: 114 additions & 0 deletions tests/callbacks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
const callBackMethods = require('../src/callbacks');
/* eslint-disable no-undef */

describe('callback functions', () => {
const listOfFood = [
'cheetoes',
'bananas',
'turkey leg',
'cobbler',
'bananas',
];

describe('firstItem', () => {
it('should pass the first item in the list to the callBack', () => {
let itemToTest;
callBackMethods.firstItem(listOfFood, (first) => {
itemToTest = first;
});
expect(itemToTest).toBe(listOfFood[0]);
});
it('should call the callback with the expected input', () => {
const mock = jest.fn();
callBackMethods.firstItem(listOfFood, mock);
expect(mock).toBeCalledWith('cheetoes');
});
});

describe('getLength', () => {
it('should pass back the length of the array to the callback', () => {
let len = 0;
callBackMethods.getLength(listOfFood, (length) => {
len = length;
});
expect(len).toBe(5);
});
it('should call the callback with the expected input', () => {
const mock = jest.fn();
callBackMethods.getLength(listOfFood, mock);
expect(mock).toBeCalledWith(5);
});
});

describe('last', () => {
it('should pass back the last item in the array to the callback', () => {
let item;
callBackMethods.last(listOfFood, (lastItem) => {
item = lastItem;
});
expect(item).toBe('bananas');
});
it('should call the given callback with the expected input', () => {
const mock = jest.fn();
callBackMethods.last(listOfFood, mock);
expect(mock).toBeCalledWith('bananas');
});
});

describe('sumNums', () => {
it('should pass back the result of the added nums to the callback', () => {
let result = 0;
callBackMethods.sumNums(10, 20, (res) => {
result = res;
});
expect(result).toBe(30);
});
it('should call the given callback with the expected input', () => {
const mock = jest.fn();
callBackMethods.sumNums(11, 22, mock);
expect(mock).toBeCalledWith(33);
});
});

describe('multiplyNums', () => {
it('should pass back the result of the multiplied nums to the callback', () => {
let result = 0;
callBackMethods.multiplyNums(10, 20, (res) => {
result = res;
});
expect(result).toBe(200);
});
it('should call the given callback with the expected input', () => {
const mock = jest.fn();
callBackMethods.multiplyNums(5, 11, mock);
expect(mock).toBeCalledWith(55);
});
});

describe('contains', () => {
it('should pass back true if item is in array', () => {
let flag;
callBackMethods.contains('bananas', listOfFood, (truf) => {
flag = truf;
});
expect(flag).toBe(true);
});
it('should pass back false if item is NOT in array', () => {
let flag;
callBackMethods.contains('boat', listOfFood, (truf) => {
flag = truf;
});
expect(flag).toBe(false);
});
});

describe('removeDuplicates', () => {
it('should remove any duplicate items from the array', () => {
let arrToTest = [];
callBackMethods.removeDuplicates(listOfFood, (arrayFreeOfDups) => {
arrToTest = arrayFreeOfDups;
});
expect(arrToTest.length).toBe(4);
});
});
});