Today is our first day using NodeJS, so we will practice doing some basic operations. Ready to start?
- Fork this repo
- Clone this repo
-
Upon completion, run the following commands:
git add . git commit -m "done" git push origin master
-
Create Pull Request so your TAs can check up your work.
This exercise will be completed through a series of tests. You should be familiar with testing from previous lessons and exercises, but just in case, here is a short recap of Intro to testing. π
Software testing is a process of executing an application to validate and verify that it meets the business and technical requirements and works as expected.
Testing is a process, not a single activity. As early as we start developing and conducting tests, the better are chances that we can prevent deficiencies in the code or product design.
In this exercise, we have created all the tests you need to create the solution, and you have to execute them all and create the code to accomplish all the requirements.
Tests prove that your code actually works in every situation in which itβs designed to work. Even when you are improving the design or creating new features, you can change your current code without breaking what already works.
Mocha is a feature-rich JavaScript test framework running on Node.js and in the browser, making asynchronous testing simple and fun.
We have already included Mocha in the project you just forked, so let's see how to use it to implement our code.
To start using mocha, you have to install it in your current working folder. Remember that we should always use the Node Package Manager (NPM) to manage all the packages in our projects.
Since it is listed in the dependencies in the package.json
, we will do so by running the following command inside the starter-code
folder, where the package.json
is located:
$ npm install
Before starting to code, we will explain the provided project structure:
starter-code/
βββ test
β βββ test.js
ββ package.json
ββ index.js
We are going to be working with the index.js
file. Here we will write the implementation of our SortedList
class.
The test
folder contains all the tests we will execute with Mocha.
Running the tests with Mocha is super easy, you just have to run npm test
. Before you run this command, make sure you navigate to the starter-code
.
$ npm test
> lab-intro-node@1.0.0 test ~~/lab-intro-node/starter-code
> mocha
SortedList
Constructor
1) should create an empty SortedList
#add(x)
2) should add a single value to SortedList
3) should add a third value to SortedList
#get(i)
4) should return an OutOfBounds exception if there is no element in that position
5) should return the element in that position
#add(x) and get(i)
6) should add a second value to SortedList, sorted
7) should add a third value to SortedList, sorted
#max()
8) should return an EmptySortedList exception if there is no element in the list
9) should return the max element in the list
#min()
10) should return an EmptySortedList exception if there is no element in the list
11) should return the min element in the list
#sum()
12) should return 0 for an empty sorted list
13) should add(sum) all elements of the array if there are elements in the list
#average()
14) should return an EmptySortedList exception if there are no elements
15) should return the average of elements in the array
0 passing (19ms)
15 failing
Don't worry if none of these tests are passing... your goal is to make them ALL PASS!! π
The task here is to create a class that maintains a sorted list of numbers in ascending order.
The class will have the following methods:
new SortedList
should create a new object from the SortedList
class.
The object should have a items
and length
property.
items
should be an array.length
should be the number of elements in the array.
The add(x)
method will add x
to the items array.
The method will get the nth
value in the list.
You should also provide a length property that gives the length of the list. Make sure you throw an error OutOfBounds if a user tries to get an element in the unexisting position (ex. if the array has 5 elements and we are trying to get element on the position 12).
The max()
method should return the highest value of the array.
In the case you have an empty SortedList
, you must throw an error with the message "EmptySortedList". For this, you can use:
throw new Error("EmptySortedList")
If you want more information about try...catch
, you can go on the Mozilla documentation.
The min()
method should return the lowest value of the array.
In the case you have an empty SortedList
, you must throw an error with the message "EmptySortedList".
The sum()
method should return the sum value of the array. At this point, we will not tell you anything else, so go ahead and check the corresponding test and see if there's anything else to be added. You can do this developer! β€οΈ
The average()
method should return the average value of the array.
The same as above, check the corresponding test to see if there's anything else to be added.
And you reached the end!
Happy Coding! β€οΈ