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
14 changes: 13 additions & 1 deletion 2-mandatory/1-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,16 @@
You should write and log at least 5 recipes
*/

// write code here
// write code here

const recipe = {
title: "Mole",
servings: 2,
ingredients: ["cinnamon", "cumin", "cocoa"],
};

console.log(recipe.title);
console.log(`Serves: ${recipe.servings}`);
console.log(
`Ingredients:\n${recipe.ingredients[0]}\n${recipe.ingredients[1]}\n${recipe.ingredients[2]}`
);
8 changes: 7 additions & 1 deletion 2-mandatory/2-currency-code-lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ const COUNTRY_CURRENCY_CODES = [

function createLookup(countryCurrencyCodes) {
// write code here
const countryCurrencyObject = {};
for (let arr of countryCurrencyCodes) {
countryCurrencyObject[arr[0]] = arr[1];
}

return countryCurrencyObject;
}

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

function createShoppingList(recipe) {
// write code here
const newPantry = pantry.fridgeContents.concat(pantry.cupboardContents);
const missingItems = recipe.ingredients.slice();
for (let item of recipe.ingredients) {
for (let i = 0; i < newPantry.length; i++) {
if (item == newPantry[i]) {
missingItems.splice(missingItems.indexOf(item), 1);
}
}
}
return { name: recipe.name, items: missingItems };
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -43,11 +53,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"],
});
});
});
18 changes: 17 additions & 1 deletion 2-mandatory/4-restaurant.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,23 @@ const MENU = {

let cashRegister = {
// write code here
}
orderBurger: function (balance) {
if (balance - MENU.burger >= 0) {
let newBalance = balance - MENU.burger;
return newBalance;
} else {
return balance;
}
},
orderFalafel: function (balance) {
if (balance - MENU.falafel >= 0) {
let newBalance = balance - MENU.falafel;
return newBalance;
} else {
return balance;
}
},
};

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js`
Expand Down
39 changes: 26 additions & 13 deletions 2-mandatory/5-writing-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,41 +35,54 @@ function convertScoreToGrade(score) {
passes.
*/
test("a score of 83 is grade A", () => {
expect(convertScoreToGrade(83), "Z");
expect(convertScoreToGrade(83)).toEqual("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. */
let number = 71;
expect(convertScoreToGrade(number)).toEqual("B");
});
/*
Write a test that checks a score of 68 is grade C
*/
test("a score of 68 is grade C", () => {
let number = 68;
expect(convertScoreToGrade(number)).toEqual("C");
});

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

/*
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 49 is grade E
*/
/* Write a test that checks a score of 49 is grade E
*/
test("a score of 49 is grade E", () => {
let number = 49;
expect(convertScoreToGrade(number)).toEqual("E");
});

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

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

function convertScoreToGrade() {
function convertScoreToGrade(score) {
let grade = null;

if (score >= 80) {
Expand All @@ -29,17 +29,55 @@ function convertScoreToGrade() {
}

function formatCourseworkResult(trainee) {
if (!trainee.name) {
return "Error: No trainee name!";
}
let traineeName = trainee.name;

if (typeof trainee.score != "number") {
return "Error: Coursework percent is not a number!";
}
let traineeGrade = convertScoreToGrade(trainee.score);

return `${traineeName}'s coursework was marked as grade ${traineeGrade}.`;
/* Added */
let traineeAge = trainee.age;

/* Added */
let traineeSubjects = trainee.subjects;

/* First condition*/
if (trainee.age == undefined && trainee.subjects == undefined) {
if (!trainee.name) {
return "Error: No trainee name!";
}
if (typeof trainee.score != "number") {
return "Error: Coursework percent is not a number!";
}
return `${traineeName}'s coursework was marked as grade ${traineeGrade}.`;
}

/* Second condition*/
if (trainee.name == undefined && trainee.subjects == undefined) {
if (typeof trainee.age != "number") {
return "Error: Age is not a number!";
}
if (typeof trainee.score != "number") {
return "Error: Coursework percent is not a number!";
}
return `A student with the age of ${traineeAge} was marked as grade ${traineeGrade}.`;
}

/* Third condition*/
if (trainee.score == undefined && trainee.age == undefined) {
if (!Array.isArray(trainee.subjects)) {
return "Error: Not a subject list!";
}
if (!trainee.name) {
return "Error: No trainee name!";
}
return `${traineeName} completed the following subjects: ${traineeSubjects.join(
", "
)}.`;
}

/* All properties available in the object*/
return `Name: ${traineeName}, Age: ${traineeAge}, Subjects: ${traineeSubjects.join(
", "
)}, Grade: ${traineeGrade}.`;
}

/* ======= TESTS - FOR THIS EXERCISE YOU SHOULD MODIFY THEM! =====
Expand All @@ -55,6 +93,15 @@ function formatCourseworkResult(trainee) {
score: 63
}
*/
test("checking trainee name and grade with a score of 63", () => {
const student = {
name: "Xin",
score: 63,
};
expect(formatCourseworkResult(student)).toEqual(
"Xin's coursework was marked as grade C."
);
});

/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -63,6 +110,15 @@ function formatCourseworkResult(trainee) {
score: 78
}
*/
test("checking trainee name and grade with a score of 78", () => {
const student = {
name: "Mona",
score: 78,
};
expect(formatCourseworkResult(student)).toEqual(
"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,6 +129,17 @@ function formatCourseworkResult(trainee) {
subjects: ["JavaScript", "React", "CSS"]
}
*/
test("checking trainee name, age, subjects and grade with a score of 78", () => {
const student = {
name: "Ali",
score: 49,
age: 33,
subjects: ["JavaScript", "React", "CSS"],
};
expect(formatCourseworkResult(student)).toEqual(
"Name: Ali, Age: 33, Subjects: JavaScript, React, CSS, Grade: E."
);
});

/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -81,6 +148,15 @@ function formatCourseworkResult(trainee) {
age: 29
}
*/
test("checking trainee age and grade with a score of 90", () => {
const student = {
score: 90,
age: 29,
};
expect(formatCourseworkResult(student)).toEqual(
"A student with the age of 29 was marked as grade A."
);
});

/*
Write a test that checks the output of formatCourseworkResult when passed the following trainee:
Expand All @@ -89,3 +165,12 @@ function formatCourseworkResult(trainee) {
subjects: ["HTML", "CSS", "Databases"]
}
*/
test("checking trainee name and subjects", () => {
const student = {
name: "Aman",
subjects: ["HTML", "CSS", "Databases"],
};
expect(formatCourseworkResult(student)).toEqual(
"Aman completed the following subjects: HTML, CSS, Databases."
);
});