Workshop to learn the basics of Mocha into a Node.js environment.
Please install dependencies first.
npm install
To launch the tests.
npm test
To lint sources.
npm run lint
To start program.
npm start
Please verify that after all exercises the tests and the linter should not return errors.
You have a fruit shop, on every 3 same products, you offer the third.
You want to start a business online, so your software should be able to return the details of a basket.
A function getPrices
that doesn't take parameter should return an object which describes the prices of your products.
// For example
console.log(getPrices()) // { banana: 1, potato: 2, tomato: 3, cucumber: 4, salad: 5, apple: 6 }
Add tests into file test/shop-tools.test.js. that will verify this assertion.
Add the function getPrices
into file src/shop-tools.js.
👉 function .eql
from Chai should help you.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 1"
A function countArticles
that take one parameter (an array) should return the number of the elements in an array.
// For example
const products = [ 'tomato', 'cucumber', 'tomato', 'salad', 'potato', 'cucumber', 'potato', 'potato', 'tomato', 'potato' ];
console.log(countArticles(products)) // 10
Add tests into file test/shop-tools.test.js. that will verify this assertion.
Add the function countArticles
into file src/shop-tools.js.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 2"
A function countProducts
that take one parameter (an array) should return the number of distinct elements in a array.
// For example
const products = [ 'tomato', 'cucumber', 'tomato', 'salad', 'potato', 'cucumber', 'potato', 'potato', 'tomato', 'potato' ];
console.log(countProducts(products)) // 4
Add tests into file test/shop-tools.test.js. that will verify this assertion.
Add the function countProducts
into file src/shop-tools.js.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 3"
A function removeFreeArticles
that take one parameter (an array) should return an array where the business discount will be applied.
// For example
const products = [ 'tomato', 'cucumber', 'tomato', 'salad', 'potato', 'cucumber', 'potato', 'potato', 'tomato', 'potato' ];
console.log(removeFreeArticles(products)) // [ 'tomato', 'cucumber', 'tomato', 'salad', 'potato', 'cucumber', 'potato', 'potato' ]
Add tests into file test/shop-tools.test.js. that will verify this assertion.
Add the function removeFreeArticles
into file src/shop-tools.js.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 4"
A function calculateBasket
that take two parameters (an object and an array) should calculate the price of the basket.
// For example
const prices = { banana: 1, potato: 2, tomato: 3, cucumber: 4, salad: 5, apple: 6 };
const products = [ 'tomato', 'cucumber', 'salad'];
console.log(calculateBasket(products, prices)) // 12
Add tests into file test/shop-tools.test.js. that will verify this assertion.
Add the function calculateBasket
into file src/shop-tools.js.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 5"
A function summarizeBasket
that take two parameters (an object and an array) should build our expected result.
// For example
const prices = { banana: 1, potato: 2, tomato: 3, cucumber: 4, salad: 5, apple: 6 };
const products = [ 'tomato', 'cucumber', 'tomato', 'salad', 'potato', 'cucumber', 'potato', 'potato', 'tomato', 'potato' ];
console.log(summarizeBasket(products, prices)) // { price: 25, countArticles: 10, countProducts: 4 }
Add tests into file test/shop-tools.test.js. that will verify this assertion.
Add the function summarizeBasket
into file src/shop-tools.js.
After finish this exercise, please commit all your files.
git add .
git commit -m "Exercise 6"