Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.
Closed
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
140 changes: 107 additions & 33 deletions mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@
You might think "This is madness!" but in everyday coding life
it is quite a frequent combination. Just think about what benefits we can get from this construct.

An object lets you store multiple values in a single variable, then you can store complex objects in an array.
Let's assume you have a list of data about people names and their birthday and you would like to print each name
An object lets you store multiple values in a single variable, then you can store
complex objects in an array.
Let's assume you have a list of data about people names and their birthday
and you would like to print each name
with corresponding birthday together.

Storing these pieces of information in different arrays and then pairing them up
makes the iteration unnecessarily complicated, code will be less intuitive, needs extra cognitive effort to
reason about and last but not least it can be error-prone (for example, you match up the wrong birthday to a name).

In this exercise you will practice how to access Objects stored in an Array and their properties. You already know
different ways of looping through Arrays, it won't be different in this case. The only extra step is that you have to
makes the iteration unnecessarily complicated, code will be less intuitive,
needs extra cognitive effort to
reason about and last but not least it can be error-prone
(for example, you match up the wrong birthday to a name).

In this exercise you will practice how to access Objects stored in an Array
and their properties. You already know
different ways of looping through Arrays,
it won't be different in this case. The only extra step is that you have to
use values inside Objects.
*/

Expand Down Expand Up @@ -55,7 +61,7 @@ let writers = [
occupation: "writer",
age: 49,
alive: true,
}
},
];

/*
Expand All @@ -68,7 +74,37 @@ Exercise 1:
*/
function logAllWriters() {
// write your code to log all writers here
};
// for (let i = 0; i < writers.length; i++) {
// console.log(
// "Hi, my name is " +
// writers[i].firstName +
// " " +
// writers[i].lastName +
// ". I am" +
// " " +
// writers[i].age +
// " years old, and work as a " +
// writers[i].occupation +
// "."
// );
// }
writers.forEach(logWriter);
}

function logWriter(writer) {
console.log(
"Hi, my name is " +
writer.firstName +
" " +
writer.lastName +
". I am" +
" " +
writer.age +
" years old, and work as a " +
writer.occupation +
"."
);
}

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

function diedInForty(writer) {
return writer.age >= 40 && writer.age <= 49 && !writer.alive;
}

function logWriter1(writer) {
console.log(
"Writer " +
writer.firstName +
" " +
writer.lastName +
" died at " +
writer.age +
" years old."
);
}

function logDeadWritersInTheirForties() {
// write your code here
writers.filter(diedInForty).forEach(logWriter1);
}

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

function aliveInForty(writer) {
return writer.age >= 40 && writer.age <= 49 && writer.alive;
}

function logWriter2(writer) {
console.log(
"Hi, my name is " +
writer.firstName +
" " +
writer.lastName +
". I am" +
" " +
writer.age +
" years old."
);
}

function logAliveWritersInTheirForties() {
// write your code here
writers.filter(aliveInForty).forEach(logWriter2);
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -101,29 +172,32 @@ function logAliveWritersInTheirForties() {
- (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();
}
67 changes: 46 additions & 21 deletions mandatory/10-cheap-diner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,38 +30,61 @@ Should give the answer "Nothing :("
**/

function chooseMeal(mealArray) {
mealArray.sort(compareFunction);
// console.log(mealArray.length);
if (mealArray.length > 1) {
return mealArray[1].name;
} else if (mealArray.length === 1) {
return mealArray[0].name;
} else {
return "Nothing :(";
}
}

function compareFunction(a, b) {
if (b.price > a.price) return -1;
if (b.price < a.price) return 1;
return 0;
}

// console.log(chooseMeal([{ name: "Subway", price: 8.99 }]));

/* ======= 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 +93,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");
});
23 changes: 21 additions & 2 deletions mandatory/11-choose-your-own-adventure.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ let game = {
// object for the correct room.
//
// Hint: the only valid rooms are "hall", "classroom" and "library".
this.currentRoom = rooms[roomName];
},

move: function (direction) {
Expand All @@ -66,6 +67,24 @@ let game = {
//
// Hint: the room objects have north/east/south/west methods which return
// a new room object that is in the relevant direction.

// console.log(this.currentRoom);
if (
direction === "north" ||
direction === "east" ||
direction === "west" ||
direction === "south"
) {
let newRoom = this.currentRoom[direction]();
// console.log(newRoom);
if (newRoom) {
this.currentRoom = newRoom;
} else {
console.log("Please choose a valid direction");
}
} else {
console.log("Choose one of north/south/east/west only");
}
},
};

Expand Down Expand Up @@ -232,14 +251,14 @@ test("start in classroom and go west", () => {
});

// remove ".skip" if your code handles trying to go in a direction with no room (by staying in the same room)
test.skip("start in hall and go north (to non-existent room) -> stay in same room", () => {
test("start in hall and go north (to non-existent room) -> stay in same room", () => {
game.currentRoom = rooms.hall;
game.move("north");
expect(game.currentRoom.name).toEqual("hall");
});

// remove ".skip" if your code handles trying to go in a direction that doesn't exist (by staying in the same room)
test.skip("start in hall and go backwards (non-existent direction) -> stay in same room", () => {
test("start in hall and go backwards (non-existent direction) -> stay in same room", () => {
game.currentRoom = rooms.hall;
game.move("backwards");
expect(game.currentRoom.name).toEqual("hall");
Expand Down
30 changes: 20 additions & 10 deletions mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@
*/

function eligibleStudents(attendances) {

let arr = [];
attendances.filter((student) => {
if (student.attendance >= 8) {
arr.push(student.name);
}
});
return arr;
}

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -30,15 +36,19 @@ function eligibleStudents(attendances) {
*/

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 },
];
// console.log(eligibleStudents(attendances));
test("eligibleStudents function works", () => {
expect(eligibleStudents(attendances)).toEqual(["Ahmed", "Clement", "Tayoa", "Nina"]);
expect(eligibleStudents(attendances)).toEqual([
"Ahmed",
"Clement",
"Tayoa",
"Nina",
]);
});

Loading