Skip to content
Open
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
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
* `mergeSort` - Create a function which stores an array. Return a closure which accepts a number as an argument. The closure should insert the number into the array in descending order and return the highest number from array
* `total` - Create a function which stores a `total` number. Return a closure which accepts an array of numbers. The closure should add all the numbers in array, add them to `total` and return the new `total`.
* `gibberish` - Create a function which stores an empty string. Return a closure which can accept either a string or an array. If it's a string, add it to existing string separated by a space `' '`. If it's an array of strings join them using a space, append to existing string separated by a space and add a fullstop `.` at the end.The closure should return the new resulting string.
* `calculator` - Create a function which stores a `total` number. It should return an object with 2 methods
* `calculator` - Create a function which stores a `total` number.
* `calculate` should accept 3 parameters: 2 numbers and a string representing an operator `+`, `-`, `*` or `/`. It should perform the operation indicated by the operator add the result to `total`. Return the new `total`

* `trainstation` - Create a function which stores an array of people. Each person should have a `name` and an `amount` of money, a random integer between 0 and 20. Return an object which has 2 methods
* `arrive` should accept a person, who has a random amount of money \[0-20\] and add it to array of people
* `getPeople` should return the array of people
* `giveMoney` should allocate an random amount of money to a random person. You may want to generate the random amount and index of random person first to make function testable
* `trainArrives` everyone who now has £20 or more can buy a ticket and get on a train. You should remove people with 20 or more from array and return then in a new array.


* `dogHome` - Create a function which stores an object. This object will have keys representing locations and an array as values. Return an object with 2 methods
* `houseDog` - It will receive a dog object as an argument. The dogs object should have a `name`, `breed`, `colour` and `location`. Store the dog in the object of parent scope by location
* `getDogsByLocation` - It will accept a `location` as an argument and return all dogs for that location


* `shop` - Create a function which stores a `storage` object and a number `revenue`. Return an object with 3 methods
* `addStock` - It will receive an array of items. Each item will have a `name`, `quantity` and `price`. If `storage` already has an item that is being added, update the quantity to reflect the new stock levels. If the price of new stock is different, calculate a new average price for old and new stock and set new price to be the average.
* `sellStock` - Will receive an `order` array. Each item in `order` will have a `name` and a `quantity`. You cannot sell more items than you have in stock. Reduce the quantity of items in stock by amount sold. Add the revenue from each order to the total total revenue. Return an array which contains items sold. Each item should have a `name`, `quantity` and `price`.
Expand Down
209 changes: 209 additions & 0 deletions index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
const { double,
increase,
mergeSort,
total,
gibberish,
calculator,
trainstation,
dogHouse,
shop
} = require('./src/index.js');

// describe('name of func',()=>{
// it('what it does',()=>{
// const expected = "what you expect here"
// const result = 'get your result here'
// expect(result).toBe(exptected)
// })
// })

describe('double function', () => {
it('double the number', () => {
const expected = 8;
const aFunctionThatDoubles4 = double(4);
const result = aFunctionThatDoubles4();
expect(result).toBe(expected)
})
})

describe('increase function', () => {
it('double the number', () => {
const expected = 7
const addToBase4 = increase(4);
const result = addToBase4(3);
expect(result).toBe(expected);
})
})

describe('mergeSort', () => {
it('highest num from an array', () => {
const expected = 11;
const mergeSorting = mergeSort();
const result = mergeSorting(11);
expect(result).toBe(expected);
const result2 = mergeSorting(2);
expect(result2).toBe(11);
const result3 = mergeSorting(14)
expect(result3).toBe(14);
})
})

describe('total function', () => {
it('should a new function that gets number array and add to the total', () => {
const expected = 6;
const addToTotal = total();
const result = addToTotal([1, 4, 1]);
expect(result).toBe(expected);
const expected2 = 27
const result2 = addToTotal([7, 5, 9]);
expect(result2).toBe(expected2);
})
})

describe('gibberish function', () => {
it('adds the string received to a parent string with space', () => {
const expected = ' str1';
const addToStr = gibberish();
const result = addToStr('str1');
expect(result).toBe(expected);
})
})
describe('calculator', () => {
it('should complete the + operation and return the total', () => {
const expected = 5;
const calculate = calculator();
const result = calculate(2, 3, "+");
expect(result).toBe(expected);
})
it('should complete the - operation and return the total', () => {
const expected = 8;
const calculate = calculator();
const result = calculate(11, 3, "-");
expect(result).toBe(expected);
})
it('should complete the * operation and return the total', () => {
const expected = 6;
const calculate = calculator();
const result = calculate(2, 3, "*");
expect(result).toBe(expected);
})
it('should complete the / operation and return the total', () => {
const expected = 4;
const calculate = calculator();
const result = calculate(20, 5, "/");
expect(result).toBe(expected);
})
})

describe('trainstation', () => {
it('return an object with four methods', () => {
const result = trainstation();
expect(result).toEqual({ "arrive": expect.any(Function), "getPeople": expect.any(Function), "giveMoney": expect.any(Function), "trainArrives": expect.any(Function) });
})

describe('arrive ,get people, giveMoney, trainArrives', () => {
// defined in upper scope to create persisting connection to same scope
const { arrive, getPeople, giveMoney, trainArrives } = trainstation();
describe('arrive and getPeople', () => {
it('should accept a person, who has a random amount of money \[0-20\] and add it to array of people ', () => {
arrive({ name: "Josh", amount: 10 });
arrive({ name: "Debra", amount: 0 });
const expected = [{ name: "Josh", amount: 10 }, { name: "Debra", amount: 0 }];
const result = getPeople();
expect(result).toEqual(expected);
})
})

describe('giveMoney', () => {
it('giving money to a random person', function () {
const expected = [{ name: "Josh", amount: 10 }, { name: "Debra", amount: 20 }];
giveMoney(1, 20);
const result = getPeople();
expect(result).toEqual(expected);
})
})

describe('trainArrives', () => {
it('remove people with 20 pounds and return remaining', function () {
const expected = [{ name: "Josh", amount: 10 }];
const result = trainArrives();
expect(result).toEqual(expected);
})
})


})

})

describe('dogHouse',()=>{
const {houseDog,getDogsByLocation} = dogHouse();
describe('houseDog and getDogsByLocation',()=>{
it('it will add the dog received to the appropriate location array in dogs object',()=>{
const dogToBeAdded1={
name: "Pancho",
breed: "Pappilion",
location: "Poland"
}
const dogToBeAdded2={
name: "Ginger",
breed: "ChowChow",
location: "Poland"
}
const dogToBeAdded3={
name: "Bun",
breed: "Sausage Dog",
location: "London"
}
houseDog(dogToBeAdded1);
houseDog(dogToBeAdded2);
houseDog(dogToBeAdded3);
const result = getDogsByLocation(dogToBeAdded3.location);
expect(result).toContain(dogToBeAdded3);
const result2 = getDogsByLocation(dogToBeAdded1.location);
expect(result2).toContain(dogToBeAdded1);
expect(result2).toContain(dogToBeAdded2);
})
})
})

describe('shop storage', ()=>{
it('return an object with three methods', () => {
const result = shop();
expect(result).toEqual({ "addStock": expect.any(Function), "sellStock": expect.any(Function), "getRevenue": expect.any(Function) });
})
const { addStock, sellStock, getRevenue} = shop();

describe('get revenue function', ()=>{
it('returns the total revenue',()=>{
const result = getRevenue();
expect(result).toBe(0);
})
})
describe('Add, Sell, Check revenue', ()=>{
it('adds stocks, sell stocks return sold items, get total revenue',()=>{
const stocksToAdd = [{name:"apple", quantity:10, price:30},{name:"google", quantity:5, price:120}]
const extraStocksToAdd = [{name:"google", quantity:5, price:20}]
addStock(stocksToAdd);
addStock(extraStocksToAdd);


const stockToSell = [{name:"google", quantity:20, price:80},{name:"not another google", quantity:7, price:50}];
const result = sellStock(stockToSell);
expect(result).toEqual([{name:"google", quantity:10, price:80}]);

const totalRevenue = getRevenue();
expect(totalRevenue).toBe(100);
})
})





})





Loading