Skip to content
This repository was archived by the owner on Jan 3, 2023. 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
25 changes: 21 additions & 4 deletions mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ Exercise 1:
"Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}."
*/
function logAllWriters() {
// write your code to log all writers here
};
writers.forEach(element => {
console.log(`Hi, my name is ${element.firstName} ${element.lastName}. I am ${element.age} years old, and work as a ${element.occupation}.
`);
});
}

// logAllWriters(writers);

/*
Exercise 2:
Expand All @@ -80,9 +85,14 @@ Exercise 2:
*/

function logDeadWritersInTheirForties() {
// write your code here
writers.forEach(element =>{
if (element.age >= 40 && element.age <= 49 && element.alive === false ){
console.log(`Writer ${element.firstName} ${element.lastName} died at ${element.age} years old.`);
}
});
}

// logDeadWritersInTheirForties(writers);
/*
Exercise 3:

Expand All @@ -92,9 +102,16 @@ Exercise 3:
*/

function logAliveWritersInTheirForties() {
// write your code here
const allWriters = writers.filter(element => {
return element.age >= 40 && element.age <= 49 && element.alive === true ;
});
allWriters.forEach(element =>{
console.log(`Hi, my name is ${element.firstName} ${element.lastName}. I am ${element.age} years old.`);
});
}
// logAliveWritersInTheirForties(); <<<<<

// logAliveWritersInTheirForties(writers);
/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 1-writers.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand Down
17 changes: 17 additions & 0 deletions mandatory/10-cheap-diner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ Should give the answer "Nothing :("
**/

function chooseMeal(mealArray) {
if(mealArray.length === 0){
return "Nothing :(";
} else if (mealArray.length === 1){
return mealArray[0].name;
} else {
let firstCheapest = mealArray[0];
let secondCheapestMenu = mealArray[1];
mealArray.forEach(element => {
if(element.price < firstCheapest.price) {
secondCheapestMenu = firstCheapest;
firstCheapest = element;
} else if (element.price > firstCheapest.price && element.price < secondCheapestMenu.price){
secondCheapestMenu = element;
}
});
return secondCheapestMenu.name;
}
}

/* ======= TESTS - DO MODIFY (!!!) =====
Expand Down
9 changes: 8 additions & 1 deletion mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@
*/

function eligibleStudents(attendances) {

const arr = [];
attendances.forEach(element => {
if(element.attendance >= 8){
arr.push(element.name);
}
}); return arr;
}



/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 2-eligible-students.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand Down
9 changes: 7 additions & 2 deletions mandatory/3-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
*/

function journeyPlanner(locations, transportMode) {

const arr = [];
for(key in locations) {
if(locations[key].includes(transportMode)){
arr.push(key);
}
} return arr;
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -41,7 +46,7 @@ const londonLocations = {
"Tower Bridge": ["tube", "bus"],
"Greenwich": ["bus", "river boat"],
};

// journeyPlanner(londonLocations, 'xyz');
test("journeyPlanner function works - case 1", () => {
expect(journeyPlanner(londonLocations, "river boat")).toEqual([
"London Bridge",
Expand Down
11 changes: 11 additions & 0 deletions mandatory/4-water-bottle.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,29 @@ let bottle = {
volume: 0,
fillUp: function () {
// calling this function should completely fill your bottle (volume = 100);
this.volume = 100 ;
},
pour: function () {
// calling this function should increase your bottle volume by 10 units;
this.volume +=10;
if(this.volume >= 100){
this.volume = 100;
}
},
drink: function () {
// calling this function should decrease your bottle volume by 10 units;
this.volume -=10;
if(this.volume < 0){
this.volume = 0;
}
},
isFull: function () {
// this function should return true if your bottle is full;
return this.volume === 100 ? true : false;
},
isEmpty: function () {
// this function should return true if your bottle is empty;
return this.volume === 0 ;
},
};

Expand Down
20 changes: 19 additions & 1 deletion mandatory/5-groceries.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,29 @@ Exercise 1:
*/
// Gather all week item names into this array
let weeklyGroceriesToBuy = [];
for(const key in weeklyMealPlan){
weeklyMealPlan[key].forEach(element => {
if(!weeklyGroceriesToBuy.includes(element)){
weeklyGroceriesToBuy.push(element);
}
});
}

/*
Exercise 2:
Loop through your list again, but now only collect the weekend items into the weekendGroceriesToBuy array.
*/
// Gather weekend item names into this array
let weekendGroceriesToBuy = [];

for(const key in weeklyMealPlan){
if(key === 'saturday' || key === 'sunday') {
weeklyMealPlan[key].forEach(element => {
if(!weekendGroceriesToBuy.includes(element)){
weekendGroceriesToBuy.push(element);
}
});
}
}
/*
Exercise 3:
Loop through your weekly meal plan:
Expand All @@ -53,6 +68,9 @@ let numberOfItemsPerWeek = {
saturday: 0,
sunday: 0,
};
for(const key in weeklyMealPlan){
numberOfItemsPerWeek[key] = weeklyMealPlan[key].length;
}

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 5-groceries.js`
Expand Down
23 changes: 21 additions & 2 deletions mandatory/6-people-I-know.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,10 @@ First, I want you to find all of my friends who are 35 or older.

*/

let thirtyFiveOrOlder = [];

const thirtyFiveOrOlder = friends.filter(element => {
return element.age >=35
});

/*
3) Find the email address
Expand All @@ -391,7 +394,9 @@ Next, I want you to find all of my friends who work for "POWERNET" and then stor

*/

let powerNetEmails = [];
let powerNetEmails = friends.filter((friend) => friend.company === "POWERNET")
.map((friend) => friend.email);


/*

Expand All @@ -406,6 +411,13 @@ This time, I only want the full names ("<firstname> <lastname>") of my friends w
*/

let friendsWhoAreColleaguesOfStacie = [];
friends.forEach((element) => {
element.colleagues.forEach((colleage) => {
if (colleage.name === "Stacie Villarreal")
friendsWhoAreColleaguesOfStacie.push(`${element.name.first} ${element.name.last}`);
});
});

/*

5) Find "Multi-tasking" colleagues
Expand All @@ -419,6 +431,13 @@ This time, I only want the full names of the people who can multitask
*/

let colleaguesWhoCanMultitask = [];
friends.forEach(element => {
element.colleagues.forEach(element => {
if(element.skills.includes("Multi-tasking")){
colleaguesWhoCanMultitask.push(`${element.name}`)
}
})
});

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 6-people-I-know.js`
Expand Down
38 changes: 37 additions & 1 deletion mandatory/7-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,41 @@ You should write and log at least 5 recipes

**/

let recipes = {};
const recipes = {
spaghetti : {
title :'Classic spaghetti',
serves : 8,
ingredients :['medium onion', 'olive oil', 'garlic cloves','chopped tomatoes','mozzarella','dried spaghetti']
},
Bread : {
title :'Garlic Bread',
serves : 5,
ingredients :['ciabatta loaves', 'butter', 'garlic cloves','salt','handful parsley']
},
fish : {
title :'Fish & Chips',
serves : 4,
ingredients :['olive oil', 'lemon juice', 'cornflour', 'fish']
},
kebab : {
title :'Greek chicken kebab',
serves : 6,
ingredients :['skinless chicken thighs', 'garlic cloves', 'lemon juice','paprika']
},
stack : {
title :'Stack',
serves : 3,
ingredients :['lean beef mince', 'onion', 'paprika', 'salt', 'oil', 'garlic','chopped tomatoes']
}
};

for(const key in recipes){
console.log(`${recipes[key].title}`);
console.log(`Serves: ${recipes[key].Serves}`);
console.log('Ingredients:');
recipes[key].ingredients.forEach(element => {
console.log(element);
});
console.log('^^^^^^^^^^^^^^^^^^^^');
}

64 changes: 41 additions & 23 deletions mandatory/8-reading-list.js
Original file line number Diff line number Diff line change
@@ -1,52 +1,72 @@
/**

The Reading List
Keep track of which books you've read and which books you want to read!

=====
Exercise 1
=====

Create an array of objects, where each object describes a book and has properties for:
- The title (a string)
- Author (a string)
- and alreadyRead (a boolean indicating if you read it yet)

Write a funciton that loops through the array of books. For each book, log the book title and book author like so:

"The Hobbit by J.R.R. Tolkien"

You should write and log at least 5 books.

You should modify the tests so that they contain the values that correspond to your books.
In this style of testing it is typical to write out as strings exactly what you expect your output to be,
without using any variables or any logic like loops, template strings or if statements.

*/
const books = [
{
title: "The Hobbit",
author: "J.R.R. Tolkien",
alreadyRead: true
},
{
title: "The Map of Salt and Stars",
author: "Jennifer Zeynab Joukhadar",
alreadyRead: false
},
{
title: "Dietland",
author: "Sarai Walker",
alreadyRead: true
},
{
title: "A Place for Us",
author: "Fatima Farheen Mirza",
alreadyRead: false
},
{
title: "The House of Impossible Beauties",
author: "Joseph Cassara",
alreadyRead: true
}
];

const books = [];

// exercise 1
function logBooks() {
books.forEach((book) => {
if(book.alreadyRead){
console.log(`You've already read '${book.title}' by ${book.author}`);
} else {
console.log(`You still need to read '${book.title}' by ${book.author}`);
}
})
}


/*

/*
=====
Exercise 2
=====
Now modify the function, using an if/else statement to change the output depending on whether you have read it yet or not.

If you've read it, log a string like 'You've already read "The Hobbit" by J.R.R. Tolkien',
and if not, log a string like 'You still need to read "The Lord of the Rings" by J.R.R. Tolkien.'

You will need to modify the tests to check the correct output. If you have already learnt about red-green refactoring,
remember to practice:
- first change the test to the value that should be output,
- run the test to check that your test goes red
- now change your code to make the test pass

As an example for this exercise, you might do the following steps
- Modify the tests so that they all say 'You've already read <name> by <author>'
- Run the test (they will all fail)
Expand All @@ -58,9 +78,7 @@ As an example for this exercise, you might do the following steps
- Run the test (the test will fail but there will be some successful results)
- Modify the books so that they have the correct alreadyRead value
- All tests should turn green!!

**/

/* ======= TESTS - DO MODIFY (!!!) =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 8-reading-list.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand All @@ -69,12 +87,12 @@ As an example for this exercise, you might do the following steps

test("books are logged", function() {
expectLogBooksToLog([
"The Hobbit by J.R.R. Tolkien",
"The Map of Salt and Stars by Jennifer Zeynab Joukhadar",
"Dietland by Sarai Walker",
"A Place for Us by Fatima Farheen Mirza",
"The House of Impossible Beauties by Joseph Cassara"
]);
"You've already read 'The Hobbit' by J.R.R. Tolkien",
"You still need to read 'The Map of Salt and Stars' by Jennifer Zeynab Joukhadar",
"You've already read 'Dietland' by Sarai Walker",
"You still need to read 'A Place for Us' by Fatima Farheen Mirza",
"You've already read 'The House of Impossible Beauties' by Joseph Cassara",
]);
});

/* ======= TESTS - DO NOT MODIFY ===== */
Expand Down
Loading