This assignment covers the first week of the Intermediate Bootcamp. This repository contains 3 test files, each covering a single task. The order of the tasks does not matter. You can complete them in any order. Try to complete as many of them as you can.
First run yarn install to install the dependencies.
Run the tests with the command yarn test. Read the instructions in this document to know what is expected of you. The tests are meant to help you know when you've completed a task. You can and should read the tests to see exactly what it's expecting, especially if you're getting errors. However, do not change the tests.
Note: we'll use the results of this homework assignment for a formal evaluation and as such you should write the code individually. Plagiarism is a violation of the Academy contract and makes nobody happy. Do not discuss this assignment in slack
Note: You can implement some of them out of order!
Send your homework to teachers@codaisseur.com before Sunday 20:00
Clean up your code:
- Fix the formatting/indentation
- Remove unnecessary code.
The app should be runnable simply by cloning the repository and running yarn test
- You must create a module named
ShoppingCart. The test imports it withconst ShoppingCart = require("./ShoppingCart")
- The module must export a
class, so we can runconst cart = new ShoppingCart() - Right after constructing a new object, calling
cart.getItems()should return an empty array. - We should be able to call
cart.addItem(itemName, quantity, price), which adds a new item to an internal array. Subsequent calls tocart.getItems()should return the added items. Items in the array should be in this format:{ name: "Name of the item", quantity: 1, pricePerUnit: 99.99 }
- Calling
cart.clear()should remove all items from the items array. - Calling
cart.clone()should return a new ShoppingCart object that contains all the same items. However, the items array and all the items inside should be copied so that any changes to one of the carts does not affect the other.
- You must create a module named
pathFind. The test imports it withNote: This is a named exportconst { pathFind } = require("./pathFind")
- The exported
pathFindshould be a function. Declare this function with thefunctionkeyword (otherwise the tests cannot check if your function is recursive later) - The function will be called with two parameters:
pathFind(path, object). The path is an array of strings and the object is an object. The array of strings refer to a sequence of properties on the object. Here are some examples of how it should work:pathFind(["foo"], { foo: "bar" }) // === "bar" pathFind(["name"], { name: "Dave" }) // === "Dave" pathFind(["author", "name"], { author: { name: "Stephen" } }) // === "Stephen" pathFind(["book", "author", "name"], { book: { author: { name: "Yuval" } } }) // === "Yuval"
- The function should be a pure function, neither of the inputs should be mutated.
- The function should be recursive. If you don't manage making it recursive, it's more important that it returns the expected outputs.
-
You must create a module named
async-functions. The test imports it withconst { giveItBackLater, addSomePromises, promiseToGiveItBackLater } = require("./async-functions")
Note: There should be 3 named exports that should all be functions
-
The function
giveItBackLaterwill be called with two parameters:giveItBackLater(value, callback)
The
callbackfunction should be called and given thevalueas a parameter. It should NOT do this right away, but later (asynchronously) usingsetTimeout. -
The function
promiseToGiveItBackLater(value)should return a promise that will resolve with thevaluelater. It should use the same function you defined ingiveItBackLater. That means you will wrap your callback-style function with a promise-style version. -
When the code
const outputPromise = addSomePromises(somePromise)is executed, your function should return a new promise that has both a fulfillment handler and a rejection handler.- When
somePromiseresolves with a string"foo", theoutputPromiseshould resolve with a string"foofoo". - When
somePromiseis rejected with the value"bar", theoutputPromiseshould resolve with"barbarbar".
So, your fulfillment handler should double the string and the rejection handler should triple the string.
- When