Skip to content
This repository was archived by the owner on Jan 14, 2024. It is now read-only.
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
46 changes: 45 additions & 1 deletion 2-mandatory/1-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,48 @@
You should write and log at least 5 recipes
*/

// write code here
// write code here

const recipe1 = {
title: "Strawberry Banana Smoothie",
servings: 2,
ingredients: ["strawberries", "banana", "orange juice"],
};

const recipe2 = {
title: "Mixed Berry Smoothie",
servings: 2,
ingredients: ["mixed berries", "banana", "almond milk"],
};

const recipe3 = {
title: "Mango Pineapple Smoothie",
servings: 2,
ingredients: ["mango", "pineapple", "coconut milk"],
};

const recipe4 = {
title: "Peach Raspberry Smoothie",
servings: 2,
ingredients: ["peaches", "raspberries", "vanilla yogurt"],
};

const recipe5 = {
title: "Kiwi Strawberry Smoothie",
servings: 2,
ingredients: ["kiwi", "strawberries", "apple juice"],
};

const smoothieRecipes = [recipe1, recipe2, recipe3, recipe4, recipe5];

for (const recipe of smoothieRecipes) {
console.log(recipe.title);
console.log(`Serves: ${recipe.servings}`);
console.log("Ingredients:");
for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
console.log("\n");
console.log("🍹🍹🍹🍹🍹");
console.log("\n");
}
13 changes: 12 additions & 1 deletion 2-mandatory/2-currency-code-lookup.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The approach here is fine, and it's really good that you've been able to use array restructuring here to write simpler and more comprehensible code!

The format of COUNTRY_CURRENCY_CODES_OBJECT would more usually be written as countryCurrencyCodeObject in JavaScript - the capital letters joined by underscores format is more for constants that don't change their values during the lifetime of the program, rather than (in this case) a temporary variable for an object that is being built up and then returned. But that's a minor comment.

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@ const COUNTRY_CURRENCY_CODES = [

function createLookup(countryCurrencyCodes) {
// write code here
// This is the easy way, using The Object.fromEntries() method
// return Object.fromEntries(countryCurrencyCodes);

const COUNTRY_CURRENCY_CODES_OBJECT = {};
for (let i = 0; i < countryCurrencyCodes.length; i++) {
const [countryCode, currencyCode] = countryCurrencyCodes[i];
COUNTRY_CURRENCY_CODES_OBJECT[countryCode] = currencyCode;

// I learned this: so we can pass the array's items into the bracket notation, and not always a string. as well as setting key/value pairs - (values) - again using a variable name.
}
return COUNTRY_CURRENCY_CODES_OBJECT;
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -34,4 +45,4 @@ test("creates country currency code lookup", () => {
NG: "NGN",
MX: "MXN",
});
});
});
30 changes: 26 additions & 4 deletions 2-mandatory/3-shopping-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

The createShoppingList function should return an object with two properties:
- "name" of the recipe, which is a string,
- "items", which is an arry of the missing ingredients that need to be on the shopping list
- "items", which is an array of the missing ingredients that need to be on the shopping list
*/

let pantry = {
Expand All @@ -20,6 +20,21 @@ let pantry = {

function createShoppingList(recipe) {
// write code here

let missingIngredients = [];

// 2 lovely array methods which I learned here

const allPantryContents = Object.values(pantry).flat();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a neat way to get the ingredients in the pantry into one list! Note that it wouldn't work if the pantry object had other properties too that didn't contain lists of ingredients. But if we know that it never will (and that might be quite reasonable here) then this is a good way to go.


for (let i = 0; i < recipe.ingredients.length; i++) {
if (!allPantryContents.includes(recipe.ingredients[i]))
missingIngredients.push(recipe.ingredients[i]);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works fine, but it would also be quite a good opportunity to use .filter() on the recipe ingredients, e.g.:

const missingIngredients = recipe.ingredients.filter(
  (ingredient) => !allPantryContents.includes(ingredient)
);

return {
name: recipe.name,
items: missingIngredients,
};
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -43,11 +58,18 @@ test("createShoppingList works for pancakes recipe", () => {
test("createShoppingList works for margherita pizza recipe", () => {
let recipe2 = {
name: "margherita pizza",
ingredients: ["flour", "salt", "yeast", "tinned tomatoes", "oregano", "mozarella"],
ingredients: [
"flour",
"salt",
"yeast",
"tinned tomatoes",
"oregano",
"mozarella",
],
};

expect(createShoppingList(recipe2)).toEqual({
name: "margherita pizza",
items: ["flour", "yeast", "mozarella"]
items: ["flour", "yeast", "mozarella"],
});
});
});
21 changes: 20 additions & 1 deletion 2-mandatory/4-restaurant.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These functions are fine and clear - it's generally not advised though to change the value of an argument passed in to a function unless that is the explicit purpose of the function. It happens that we expect a number to be passed in here, which won't cause problems to any code calling these functions. In general, though, if we are building a value to be returned, it's best to declare a new variable to hold this new value as we are building it rather than modifying an argument (here balance).

Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,26 @@ const MENU = {

let cashRegister = {
// write code here
}

orderBurger: function (balance) {
let enoughBalance = balance >= MENU.burger;
// or balance - MENU.burger must be > or = 0
if (enoughBalance) {
balance = balance - MENU.burger; // OR balance -= MENU.burger
}
return balance;
},

// and same goes for the orderFalafel too

orderFalafel: function (balance) {
let enoughBalance = balance >= MENU.falafel;
if (enoughBalance) {
balance -= MENU.falafel;
}
return balance;
},
};

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js`
Expand Down
27 changes: 19 additions & 8 deletions 2-mandatory/5-writing-tests.js
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is all fine!

Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,52 @@ function convertScoreToGrade(score) {
passes.
*/
test("a score of 83 is grade A", () => {
expect(convertScoreToGrade(83), "Z");
expect(convertScoreToGrade(83)).toBe("A");
});

/*
The rest of the tests have comments describing what to test and you need to
write a matching test
*/

test.skip("a score of 71 is grade B", () => {
test("a score of 71 is grade B", () => {
/* Remove the .skip above, then write the test body. */
expect(convertScoreToGrade(71)).toBe("B");
});
/*
Write a test that checks a score of 68 is grade C
*/

/*
Write a test that checks a score of 55 is grade D
*/


/*
Write a test that checks a score of 68 is grade C
*/
test("a score of 68 is grade C", () => {
expect(convertScoreToGrade(68)).toBe("C");
});

/*
Write a test that checks a score of 55 is grade D
*/
test("a score of 55 is grade D", () => {
expect(convertScoreToGrade(55)).toBe("D");
});

/*
Write a test that checks a score of 49 is grade E
*/
test("a score of 49 is grade E", () => {
expect(convertScoreToGrade(49)).toBe("E");
});

/*
Write a test that checks a score of 30 is grade E
*/
test("a score of 30 is grade E", () => {
expect(convertScoreToGrade(30)).toBe("E");
});

/*
Write a test that checks a score of 70 is grade B
*/
test("a score of 70 is grade B", () => {
expect(convertScoreToGrade(70)).toBe("B");
});
51 changes: 48 additions & 3 deletions 2-mandatory/6-writing-tests-advanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
trainee has completed.
*/

function convertScoreToGrade() {
function convertScoreToGrade(score) {
//the score parameter was not being passed to the function
let grade = null;

if (score >= 80) {
Expand Down Expand Up @@ -56,13 +57,32 @@ function formatCourseworkResult(trainee) {
}
*/

test("formatCourseworkResult", () => {
const trainee = {
name: "Xin",
score: 63,
};
expect(formatCourseworkResult(trainee)).toBe(
"Xin's coursework was marked as grade C."
);
});

/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
{
name: "Mona",
score: 78
}
*/
test("formatCourseworkResult", () => {
const trainee = {
name: "Mona",
score: 78,
};
expect(formatCourseworkResult(trainee)).toBe(
"Mona's coursework was marked as grade B."
);
});

/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -73,19 +93,44 @@ function formatCourseworkResult(trainee) {
subjects: ["JavaScript", "React", "CSS"]
}
*/

test("formatCourseworkResult", () => {
const trainee = {
name: "Ali",
score: 49,
age: 33,
subjects: ["JavaScript", "React", "CSS"],
};
expect(formatCourseworkResult(trainee)).toBe(
"Ali's coursework was marked as grade E."
);
});
/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
{
score: 90,
age: 29
}
*/

test("formatCourseworkResult", () => {
const trainee = {
score: 90,
age: 29,
};
expect(formatCourseworkResult(trainee)).toBe("Error: No trainee name!");
});
/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
{
name: "Aman",
subjects: ["HTML", "CSS", "Databases"]
}
*/
test("formatCourseworkResult", () => {
const trainee = {
name: "Aman",
subjects: ["HTML", "CSS", "Databases"],
};
expect(formatCourseworkResult(trainee)).toBe(
"Error: Coursework percent is not a number!"
);
});