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
86 changes: 54 additions & 32 deletions mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ let writers = [
occupation: "writer",
age: 49,
alive: true,
}
},
];

/*
Expand All @@ -66,9 +66,14 @@ Exercise 1:

"Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}."
*/
function logAllWriters() {
function logAllWriters(array) {
array.forEach((element) =>
console.log(
`Hi, my name is ${element.firstName} ${element.lastName}. I am ${element.age} years old, and work as a ${element.occupation}.`
)
);
// write your code to log all writers here
};
}

/*
Exercise 2:
Expand All @@ -79,8 +84,14 @@ Exercise 2:
"Writer {firstName} {lastName} died at {age} years old."
*/

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

/*
Expand All @@ -91,39 +102,50 @@ Exercise 3:
"Hi, my name is {firstName} {lastName}. I am {age} years old."
*/

function logAliveWritersInTheirForties() {
// write your code here
}
function logAliveWritersInTheirForties(array) {
array.forEach(function (array) {
if (array.age >= 40 && array.age <= 49 && array.alive === true) {
console.log(
`Hi, my name is ${array.firstName} ${array.lastName}. I am ${array.age} years old.`
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

well done here...clean code

);
}
});
}// write your code here



/* ======= 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`
- (Reminder: You must have run `npm install` one time before this will work!)
*/

test("exercise 1", () => expectFunctionToLog(logAllWriters, [
"Hi, my name is Virginia Woolf. I am 59 years old, and work as a writer.",
"Hi, my name is Zadie Smith. I am 40 years old, and work as a writer.",
"Hi, my name is Jane Austen. I am 41 years old, and work as a writer.",
"Hi, my name is Bell Hooks. I am 63 years old, and work as a writer.",
"Hi, my name is Yukiko Motoya. I am 49 years old, and work as a writer."
]));

test("exercise 2", () => expectFunctionToLog(logDeadWritersInTheirForties, [
"Writer Jane Austen died at 41 years old."
]));

test("exercise 3", () => expectFunctionToLog(logAliveWritersInTheirForties, [
"Hi, my name is Zadie Smith. I am 40 years old.",
"Hi, my name is Yukiko Motoya. I am 49 years old."
]));
test("exercise 1", () =>
expectFunctionToLog(logAllWriters, [
"Hi, my name is Virginia Woolf. I am 59 years old, and work as a writer.",
"Hi, my name is Zadie Smith. I am 40 years old, and work as a writer.",
"Hi, my name is Jane Austen. I am 41 years old, and work as a writer.",
"Hi, my name is Bell Hooks. I am 63 years old, and work as a writer.",
"Hi, my name is Yukiko Motoya. I am 49 years old, and work as a writer.",
]));

test("exercise 2", () =>
expectFunctionToLog(logDeadWritersInTheirForties, [
"Writer Jane Austen died at 41 years old.",
]));

test("exercise 3", () =>
expectFunctionToLog(logAliveWritersInTheirForties, [
"Hi, my name is Zadie Smith. I am 40 years old.",
"Hi, my name is Yukiko Motoya. I am 49 years old.",
]));

function expectFunctionToLog(f, values) {
const consoleLogSpy = jest.spyOn(console, 'log');
f();
expect(consoleLogSpy).toBeCalledTimes(values.length);
values.forEach((value, i) => {
expect(consoleLogSpy).nthCalledWith(i+1, value);
});
consoleLogSpy.mockRestore();
};
const consoleLogSpy = jest.spyOn(console, "log");
f();
expect(consoleLogSpy).toBeCalledTimes(values.length);
values.forEach((value, i) => {
expect(consoleLogSpy).nthCalledWith(i + 1, value);
});
consoleLogSpy.mockRestore();
}
64 changes: 43 additions & 21 deletions mandatory/10-cheap-diner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,58 @@ Should give the answer "Nothing :("
**/

function chooseMeal(mealArray) {
mealArray.sort((meal1, meal2) => meal1.price - meal2.price);

let cheapestMeal = mealArray[0];
let secondCheapestMeal = mealArray[1];
let noMealMessage = "Nothing :(";

if(secondCheapestMeal) {
return secondCheapestMeal.name;
}
else if(cheapestMeal) {
return cheapestMeal.name;
}
else return noMealMessage;
}


/* ======= TESTS - DO MODIFY (!!!) =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 10-cheap-diner.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
- (Reminder: You must have run `npm install` one time before this will work!)
*/

test("Meal to select is last", () => {
expect(chooseMeal([
{ name: "Dunkin' Donuts", price: 8.99 },
{ name: "Captain D's", price: 13.99 },
{ name: "Moe's Southwest Grill", price: 10.99 },
])).toEqual("Moe's Southwest Grill");
expect(
chooseMeal([
{ name: "Dunkin' Donuts", price: 8.99 },
{ name: "Captain D's", price: 13.99 },
{ name: "Moe's Southwest Grill", price: 10.99 },
])
).toEqual("Moe's Southwest Grill");
});

test("Meal to select is first", () => {
expect(chooseMeal([
{ name: "Moe's Southwest Grill", price: 10.99 },
{ name: "Dunkin' Donuts", price: 8.99 },
{ name: "Captain D's", price: 13.99 },
])).toEqual("Moe's Southwest Grill");
expect(
chooseMeal([
{ name: "Moe's Southwest Grill", price: 10.99 },
{ name: "Dunkin' Donuts", price: 8.99 },
{ name: "Captain D's", price: 13.99 },
])
).toEqual("Moe's Southwest Grill");
});

test("Meal to select is also most expensive", () => {
expect(chooseMeal([
{ name: "Burger King", price: 8.99 },
{ name: "Wingstop", price: 9.99 },
])).toEqual("Wingstop");
expect(
chooseMeal([
{ name: "Burger King", price: 8.99 },
{ name: "Wingstop", price: 9.99 },
])
).toEqual("Wingstop");
});

test("Only one meal to select", () => {
test("Only one meal to select", () => {
expect(chooseMeal([{ name: "Subway", price: 8.99 }])).toEqual("Subway");
});

Expand All @@ -70,10 +90,12 @@ test("No meals to select", () => {
});

test("Meal to select is second cheapest, not second most expensive", () => {
expect(chooseMeal([
{ name: "Church's Chicken", price: 8.99 },
{ name: "Smoothie King", price: 109.99 },
{ name: "Jason's Deli", price: 22.77 },
{ name: "Jamba Juice", price: 38.44 },
])).toEqual("Jason's Deli");
expect(
chooseMeal([
{ name: "Church's Chicken", price: 8.99 },
{ name: "Smoothie King", price: 109.99 },
{ name: "Jason's Deli", price: 22.77 },
{ name: "Jamba Juice", price: 38.44 },
])
).toEqual("Jason's Deli");
});
29 changes: 23 additions & 6 deletions mandatory/11-choose-your-own-adventure.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ let game = {
currentRoom: null,

start: function (roomName) {
if (roomName === "hall") {
this.currentRoom = rooms.hall;
} else if (roomName === "classroom") {
this.currentRoom = rooms.classroom;
} else if (roomName === "library") {
this.currentRoom = rooms.library;
}

// This function is called with the name of the room that the player wants
// to start in.
// Finish the function so that the currentRoom property is set to the room
Expand All @@ -60,13 +68,22 @@ let game = {
},

move: function (direction) {
// This function is called with the direction that the player wants to move.
// Finish the function so that the currentRoom property is updated with new
// room in the direction that the player wants to move in.
//
// Hint: the room objects have north/east/south/west methods which return
// a new room object that is in the relevant direction.
if (direction === "north") {
this.currentRoom = this.currentRoom.north();
} else if (direction === "east") {
this.currentRoom = this.currentRoom.east();
} else if (direction === "south") {
this.currentRoom = this.currentRoom.south();
} else if (direction === "west") {
this.currentRoom = this.currentRoom.west();
}
},
// This function is called with the direction that the player wants to move.
// Finish the function so that the currentRoom property is updated with new
// room in the direction that the player wants to move in.
//
// Hint: the room objects have north/east/south/west methods which return
// a new room object that is in the relevant direction.
};

/*
Expand Down
27 changes: 16 additions & 11 deletions mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,30 @@
*/

function eligibleStudents(attendances) {

return attendances
.filter((element) => element.attendance >= 8)
.map((element) => element.name);
}

/* ======= 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`
- (Reminder: You must have run `npm install` one time before this will work!)
*/

const attendances = [
{name: "Ahmed", attendance: 8},
{name: "Clement", attendance: 10},
{name: "Elamin", attendance: 6},
{name: "Adam", attendance: 7},
{name: "Tayoa", attendance: 11},
{name: "Nina", attendance: 10},
{ name: "Ahmed", attendance: 8 },
{ name: "Clement", attendance: 10 },
{ name: "Elamin", attendance: 6 },
{ name: "Adam", attendance: 7 },
{ name: "Tayoa", attendance: 11 },
{ name: "Nina", attendance: 10 },
];

test("eligibleStudents function works", () => {
expect(eligibleStudents(attendances)).toEqual(["Ahmed", "Clement", "Tayoa", "Nina"]);
expect(eligibleStudents(attendances)).toEqual([
"Ahmed",
"Clement",
"Tayoa",
"Nina",
]);
});

10 changes: 9 additions & 1 deletion mandatory/3-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,17 @@
*/

function journeyPlanner(locations, transportMode) {

let array1 = [];
for (const property in locations) {
console.log(property);
if (londonLocations[property].includes(transportMode)) {
array1.push(property);
}
}
return array1;
}


/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 3-journey-planner.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand Down
27 changes: 24 additions & 3 deletions mandatory/4-water-bottle.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,45 @@ You have to implement the missing features according to the specification.

// Here is your starting point:
let bottle = {
volume: 0,
volume: 5,
fillUp: function () {
// calling this function should completely fill your bottle (volume = 100);
this.volume = 100;
return this.volume;
},
pour: function () {
// calling this function should increase your bottle volume by 10 units;
if (this.volume <= 90) {
this.volume += 10;
return this.volume;
} else {
return this.volume;
}
},
drink: function () {
// calling this function should decrease your bottle volume by 10 units;
if (this.volume >= 10) {
this.volume -= 10;
return this.volume;
} else {
return this.volume;
}
},
isFull: function () {
// this function should return true if your bottle is full;
if (this.volume === 100) {
return true;
}
return false;
},
isEmpty: function () {
// this function should return true if your bottle is empty;
if (this.volume === 0) {
return true;
}
return false;
},
};

/*
TIP:
Remember that for changing properties on the current object inside one of its
Expand Down Expand Up @@ -149,4 +170,4 @@ test("Given an empty bottle, calling pour then drink, then the bottle is empty",
bottle.pour();
bottle.drink();
expect(bottle.volume).toEqual(0);
});
});
Loading