You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/TASKS.md
+87-60Lines changed: 87 additions & 60 deletions
Original file line number
Diff line number
Diff line change
@@ -52,115 +52,142 @@ const catalogue = [
52
52
53
53
Update **every** book in the array and save the file.
54
54
55
-
## 3) Implement the catalogue functions
55
+
## 3) Run the tests for the `countBooksByAuthor` function
56
56
57
-
Time to implement the 5 catalogue functions. You have already implemented these earlier in the week, in [programming_fundamentals_002](https://github.com/techreturners/programming_fundamentals_002), but now you need to make sure all the functions work with an _array of objects_ instead of an array of strings.
57
+
The tests for `countBooksByAuthor` are already written.
58
58
59
-
Tests are already written for the `checkBook` function but you will need to write your own tests for the other functions.
59
+
Run `npm test` in the terminal to run them. See that they fail.
60
60
61
-
This is likely to take you some time.
61
+
In these tests, we have written **three expectations** in the same test, as 3 examples of the same behaviour. All 3 expectations must pass for the test to pass:
62
62
63
-
Feel free to refer to your previous work for help, and to the [Tasks for programming_fundamentals_002](https://github.com/techreturners/programming_fundamentals_002/blob/master/docs/TASKS.md) if you need a reminder of how the Catalogue Service functions should behave.
{title:"Between the Assassinations", author:"Aravind Adiga", quantity:9},
74
-
...
75
-
];
76
-
```
77
+
For example, there are 5 books by Hilary Mantel and 1 by Celeste Ng.
77
78
78
-
However, we might want to create a Class of `Book` to make it easier to create new books that follow a common blueprint, and to encapsulate common behaviour.
79
+
Go to the `app/catalogue_service.js` file and implement the function so that the tests pass.
79
80
80
-
Open the file `Book.js` in the `/app` directory and take a look at the class defined in there.
81
+
## 5) Write a test for the `checkBookByTitle` function
81
82
82
-
Now open `test/Book.test.js`and look at the tests which have been written for the book class.
83
+
This function receives a title as an argument and should return `true` if a book with this title exists in the array, and `false` otherwise.
83
84
84
-
The first test should be passing, but all the other tests are disabled, with a little `x` at the beginning of the `describe` block:
85
+
E.g.
85
86
86
87
```javascript
87
-
xdescribe("addStock()", () => {
88
-
// tests here
89
-
});
90
-
88
+
checkBookByTitle("The Origin of Species"); // true
89
+
checkBookByTitle("The Chronicles of Narnia"); // false
91
90
```
92
91
93
-
Remove the `x` from the beginning of the `addStock()` tests and run `npm test` in the terminal to run the next set of tests too.
Go to the `app/catalogue_service.js` file and implement the function so that the tests you have just written pass.
108
119
109
-
Create an `addStock` method inside the `Book` class.
120
+
## 7) The other case
110
121
111
-
This method should take an argument of a number, and increase the number of books in stock by this number.
122
+
Write another test that ensures the function `checkBookByTitle` returns false when the book does not exists.
112
123
113
-
Run the tests again to ensure that they all pass.
124
+
Run this test and see whether it passes or fails.
114
125
115
-
## 6) Un-x the next test
126
+
If it fails, you'll need to update your function to ensure it returns `false` if the book is not found.
116
127
117
-
Now un-x the next test which tests the `removeStock()` method.
128
+
## 8) The `countBooksByFirstLetter` function
118
129
119
-
Run the tests and see that they fail.
130
+
This function should receive a letter as an argument (e.g. "H") and return the total number of books that begin with this letter. Note that the quantity is not relevant here. For example:
120
131
121
-
## 7) Create a removeStock method for a book
132
+
```javascript
133
+
countBooksByFirstLetter("W"); // returns 2
134
+
```
122
135
123
-
Create a `removeStock` method inside the `Book` class.
136
+
This returns 2 because Wolf Hall and Why Be Happy When You Could Be Normal? both begin with W.
124
137
125
-
This method should take an argument of a number, and decrease the number of books in stock by this number.
138
+
Begin by writing a test, as always, before implementing the function.
126
139
127
-
Note that the number of books in stock cannot fall below 0.
140
+
## 9) A lowercase letter
128
141
129
-
Run the tests again to ensure that they all pass.
142
+
What if the `countBooksByFirstLetter` function is given a lowercase letter, e.g. `countBooksByFirstLetter("h")`?
130
143
131
-
## 8) An array of Book Objects
144
+
It should still count the books beginning with H, ignoring the case of the letter.
132
145
133
-
Now we should be able to use our `Book` class to create the book objects that are populating our `catalogue` array.
146
+
Write a test for this functionality, and if the test doesn't pass, update you function so that it passes.
134
147
135
-
First, we need to **require** the Book class into the file where we're going to use it.
148
+
## 10) The `getQuantity` function
136
149
137
-
Add this line to the top of the `catalogue_service.js` file:
150
+
This function should receive a title as an argument (e.g. "The Origin of Species") and return the quantity of this item which is in stock. For example:
138
151
139
152
```javascript
140
-
constBook=require("./Book");
153
+
getQuantity("A Place of Greater Safety"); // returns 11
141
154
```
142
155
143
-
Then, update each of the objects in the `catalogue` array to be new Book instances:
156
+
Begin by writing a test, as always, before implementing the function.
157
+
158
+
## 11) The `getBooksByAuthor` function
159
+
160
+
This function should receive an author as an argument (e.g. "Robert Bolaño") and return an array of books. For example:
144
161
145
162
```javascript
146
-
constcatalogue= [
147
-
newBook("The Catcher in the Rye", "J.D. Salinger", 10),
148
-
newBook("Dracula", "Bram Stoker", 0),
149
-
newBook("Between the Assassinations", "Aravind Adiga", 9),
{ title:"By Night In Chile", author:"Robert Bolaño", quantity:8 }
151
169
];
152
170
```
153
171
154
-
Update **all** of your book objects so they look like the above.
172
+
Begin by writing a test, as always, before implementing the function.
155
173
156
-
Your tests should all still pass at this point.
174
+
Remember that when testing an array return value you should use `toEqual` and not `toBe` (see yesterday's work for a reminder)
175
+
176
+
## 12) The `checkQuantity` function
177
+
178
+
This function should receive a title and a quantity as an argument (e.g. "By Night In Chile" and 4) and return `true` if there are at least as many books in stock as the given quantity, and false otherwise. For example:
179
+
180
+
```javascript
181
+
checkQuantity("By Night In Chile", 4); // true
182
+
checkQuantity("By Night In Chile", 100); // false
183
+
```
184
+
185
+
Begin by writing a test, as always, before implementing the function.
157
186
158
187
## Written Questions
159
188
160
189
1. If a book is an object, what is its encapsulated data?
161
190
162
-
2. Why do we create a new Book for every single test in `Book.test.js`?
163
-
164
-
3. What data, of a book's encapsulated data, is changeable?
191
+
2. What data, of a book's encapsulated data, might be changeable?
165
192
166
-
4. Why is representing a book as an object a better idea than representing it as a string?
193
+
3. Why is representing a book as an object a better idea than representing it as a string?
0 commit comments