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
78 changes: 52 additions & 26 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 @@ -68,7 +68,12 @@ Exercise 1:
*/
function logAllWriters() {
// write your code to log all writers here
};
writers.forEach((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 @@ -81,6 +86,15 @@ Exercise 2:

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

/*
Expand All @@ -93,6 +107,15 @@ Exercise 3:

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

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -101,29 +124,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();
}
3 changes: 2 additions & 1 deletion mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
*/

function eligibleStudents(attendances) {

const enoughAttendances = attendances.filter(person => person.attendance >= 8);
return enoughAttendances.map(person => person.name);
}

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
20 changes: 13 additions & 7 deletions mandatory/3-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,13 @@
*/

function journeyPlanner(locations, transportMode) {

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

/* ======= TESTS - DO NOT MODIFY =====
Expand All @@ -36,10 +42,10 @@ function journeyPlanner(locations, transportMode) {
- (Reminder: You must have run `npm install` one time before this will work!)
*/
const londonLocations = {
"Angel": ["tube", "bus"],
"London Bridge": ["tube", "river boat"],
"Tower Bridge": ["tube", "bus"],
"Greenwich": ["bus", "river boat"],
Angel: ["tube", "bus"],
"London Bridge": ["tube", "river boat"],
"Tower Bridge": ["tube", "bus"],
Greenwich: ["bus", "river boat"],
};

test("journeyPlanner function works - case 1", () => {
Expand All @@ -62,5 +68,5 @@ test("journeyPlanner function works - case 3", () => {
"Angel",
"London Bridge",
"Tower Bridge",
])
});
]);
});