Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.
Merged
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/node_modules
/package-lock.json
Empty file added .setup/jest.setup.js
Empty file.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ The exercises are split into three folders: `exercises`, `mandatory` and `extra`

The `extra` folder contains exercises that you can complete to challenge yourself, but are not required for the following lesson.

## Running the code/tests

These files for the exercises are intended to be run as jest tests.

- Once you have cloned the repository, run `npm install` once in the terminal to install jest (and any necessary dependencies).
- To run all exercises/tests in the mandatory folder, run `npm test`
- To run a single exercise/test (for example `mandatory/1-writer.js`), run `npm test -- --testPathPattern mandatory/1-writer.js` (Remember, you can use tab-completion to get files relative to the current directory, so m`Tab ↹`/1-`Tab ↹` will autocomplete get you the test file starting with 1-)
- Some of the exercises do not use jest. To run these individually, use node directly. `node mandatory/11-choose-your-own-adventure.js`. These are:
- `4-water-bottle.js`
- `7-recipes.js`
- `11-choose-your-own-adventure.js`

For more information about tests, look here:

https://syllabus.codeyourfuture.io/guides/intro-to-tests

## Solutions

Expand Down
44 changes: 44 additions & 0 deletions mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ 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
};

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

function logDeadWritersInTheirForties() {
// write your code here
}

/*
Exercise 3:

Only `console.log()` out alive writers who are in their 40s (meaning between 40 and 49):

"Hi, my name is {firstName} {lastName}. I am {age} years old."
*/

function logAliveWritersInTheirForties() {
// 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."
]));

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();
};
80 changes: 32 additions & 48 deletions mandatory/10-cheap-diner.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ chosenMeal(setOne)

Should give the answer "Lobster"

If given an empty array, return null.
If given an empty array:

let emptyArray = []
chosenMeal(emptyArray)
Expand All @@ -30,66 +30,50 @@ Should give the answer "Nothing :("
**/

function chooseMeal(mealArray) {
// Write your code here
}

/*
==================================================
====== TESTS - DO NOT MODIFY BELOW THIS LINE =====
==================================================
/* ======= 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!)
*/
const util = require("util");

function test(test_name, actual, expected) {
let status;

if (actual === expected) {
status = `PASSED! You got the correct answer of ${util.inspect(expected)}`;
} else {
status = `FAILED: expected: ${util.inspect(
expected
)} but your function returned: ${util.inspect(actual)}`;
}

console.log(`${test_name}: ${status}`);
}

test(
"Test 1",
chooseMeal([
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 },
]),
"Moe's Southwest Grill"
);
])).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");
});

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

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

test("Test 4", chooseMeal([]), "Nothing :(");
test("No meals to select", () => {
expect(chooseMeal([])).toEqual("Nothing :(");
});

test(
"Test 5",
chooseMeal([
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: "Jamba Juice", price: 38.44 },
{ name: "Jason's Deli", price: 22.77 },
]),
"Jason's Deli"
);

test(
"Test 6",
chooseMeal([{ name: "Church's Chicken", price: 8.99 }]),
"Church's Chicken"
);
{ name: "Jamba Juice", price: 38.44 },
])).toEqual("Jason's Deli");
});
29 changes: 10 additions & 19 deletions mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,15 @@
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/

function eligibleStudents(attendance) {
function eligibleStudents(attendances) {

}

/* ======= TESTS - DO NOT MODIFY ===== */
/* ======= 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},
Expand All @@ -33,21 +38,7 @@ const attendances = [
{name: "Nina", attendance: 10},
];

const util = require('util');

function test(test_name, actual, expected) {
let status;
if (util.isDeepStrictEqual(actual, expected)) {
status = "PASSED";
} else {
status = `FAILED: expected: ${util.inspect(expected)} but your function returned: ${util.inspect(actual)}`;
}

console.log(`${test_name}: ${status}`);
}

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

57 changes: 18 additions & 39 deletions mandatory/3-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,63 +25,42 @@
When you finish the exercise, think about how this solution is different to your last solution.
What's better about each approach?
*/

function journeyPlanner(locations, transportMode) {

}

/* ======= TESTS - DO NOT MODIFY ===== */

/* ======= 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`
- (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"],
};

function arraysEqual(a, b) {
if (a === b) return true;
if (a == null || b == null) return false;
if (a.length != b.length) return false;

for (let i = 0; i < a.length; ++i) {
if (a[i] !== b[i]) return false;
}

return true;
}

function test(test_name, expr) {
let status;
if (expr) {
status = "PASSED";
} else {
status = "FAILED";
}

console.log(`${test_name}: ${status}`);
}

test(
"journeyPlanner function works - case 1",
arraysEqual(journeyPlanner(londonLocations, "river boat"), [
test("journeyPlanner function works - case 1", () => {
expect(journeyPlanner(londonLocations, "river boat")).toEqual([
"London Bridge",
"Greenwich",
])
);
]);
});

test(
"journeyPlanner function works - case 2",
arraysEqual(journeyPlanner(londonLocations, "bus"), [
test("journeyPlanner function works - case 2", () => {
expect(journeyPlanner(londonLocations, "bus")).toEqual([
"Angel",
"Tower Bridge",
"Greenwich",
])
);
]);
});

test(
"journeyPlanner function works - case 3",
arraysEqual(journeyPlanner(londonLocations, "tube"), [
test("journeyPlanner function works - case 3", () => {
expect(journeyPlanner(londonLocations, "tube")).toEqual([
"Angel",
"London Bridge",
"Tower Bridge",
])
);
});
Loading