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
28 changes: 24 additions & 4 deletions mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,16 @@ 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
};
function logAllWriters() {

return 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 @@ -79,8 +86,15 @@ Exercise 2:
"Writer {firstName} {lastName} died at {age} years old."
*/

// write your code here

function logDeadWritersInTheirForties() {
// write your code here

writers.forEach((writer) => {
if (writer.alive === false && writer.age >= 40 && writer.age <= 50) {
console.log('Writer ' + writer.firstName + ' ' + writer.lastName + ' died at ' + writer.age + ' years old.');
}
});
}

/*
Expand All @@ -93,7 +107,13 @@ Exercise 3:

function logAliveWritersInTheirForties() {
// write your code here
writers.forEach((writer) => {
if (writer.alive === true && writer.age >= 40 && writer.age <= 50) {
console.log('Hi, my name is ' + writer.firstName + ' ' + writer.lastName + '. I am ' + writer.age + ' years old.');
}
});
}


/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 1-writers.js`
Expand Down Expand Up @@ -126,4 +146,4 @@ function expectFunctionToLog(f, values) {
expect(consoleLogSpy).nthCalledWith(i+1, value);
});
consoleLogSpy.mockRestore();
};
}
26 changes: 26 additions & 0 deletions mandatory/10-cheap-diner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,34 @@ Should give the answer "Nothing :("
**/

function chooseMeal(mealArray) {

let sortedPrices = [];
let cheapestPrice = 0;
let mealResult = " ";
if (!mealArray.length) {
mealResult = "Nothing :(";
} else if(mealArray.length === 1) {
mealResult = mealArray[0].name;
}
else {
mealArray.forEach(function(meal) {
sortedPrices.push(meal.price);
})
sortedPrices.sort((a, b) => a - b);
cheapestPrice = sortedPrices[1];

mealArray.forEach(function(meal) {
if (meal.price === cheapestPrice) {
mealResult = meal.name;
}
});
}
return mealResult;

}



/* ======= 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`
Expand Down
11 changes: 9 additions & 2 deletions mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@
(see tests to confirm how this data will be structured)
- Returns an array containing only the names of the who have attended AT LEAST 8 classes
*/
let newArr = [];

function eligibleStudents(attendances) {
for (let i =0; i < attendances.length; i++) {
if (attendances[i].attendance >= 8) {
newArr.push(attendances[i].name);
}
}
return newArr;

function eligibleStudents(attendances) {

}

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
28 changes: 21 additions & 7 deletions mandatory/3-journey-planner.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,36 @@
When you finish the exercise, think about how this solution is different to your last solution.
What's better about each approach?
*/
const londonLocations = {
"Angel": ["tube", "bus"],
"London Bridge": ["tube", "river boat"],
"Tower Bridge": ["tube", "bus"],
"Greenwich": ["bus", "river boat"],
};

function journeyPlanner(locations, transportMode) {

let locationRange = [];

const locationKeys = Object.keys(locations);

locationKeys.forEach((key) => {
if (locations[key].includes(transportMode)) {
locationRange.push(key);
}
});
console.log(locationRange);
return locationRange;

}
journeyPlanner(londonLocations, "tube");


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

test("journeyPlanner function works - case 1", () => {
expect(journeyPlanner(londonLocations, "river boat")).toEqual([
Expand All @@ -63,4 +77,4 @@ test("journeyPlanner function works - case 3", () => {
"London Bridge",
"Tower Bridge",
])
});
});
25 changes: 22 additions & 3 deletions mandatory/4-water-bottle.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,27 @@ You have to implement the missing features according to the specification.
let bottle = {
volume: 0,
fillUp: function () {
this.volume = 100;
// calling this function should completely fill your bottle (volume = 100);
},
pour: function () {
this.volume += 10;
// calling this function should increase your bottle volume by 10 units;
},
drink: function () {
this.volume -= 10;
// calling this function should decrease your bottle volume by 10 units;
},
isFull: function () {
if(this.volume === 100){
return true;
}
// this function should return true if your bottle is full;
},
isEmpty: function () {
if(this.volume === 0){
return true;
}
// this function should return true if your bottle is empty;
},
};
Expand All @@ -49,15 +58,25 @@ TIP:
Extra question:
Why do you think it is preferred to use `this` inside the object rather than its variable name, in our case `bottle`?
Leave your answer below:
*/

// Write you answer to the question here
T*/


// Write you answer to the question here
// This is preferable inside the object because in some cases we might not know the name of the variable(s) inside the object.
// This can be used as an identifying function providing variables a way of referring to themselves.
// Its an easy way to refer to objects being created, can be used to reference the most global thing it can for web pages (window projects).
// This can be used to refer to the DOM element that generates the event in Event AudioListener.
//
/*
Once you have completed your object run the following
and see if your answer matches the expected result at the bottom :)
and see if your answer matches the expected result at the bottom :)he keyword 'this' can be uses in any function even if its not a method of an Object.


*/



/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 4-water-bottle.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand Down
43 changes: 38 additions & 5 deletions mandatory/5-groceries.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,39 @@ Exercise 1:
The weeklyGroceriesToBuy array shouldn't contain any repeating items.
*/
// Gather all week item names into this array
let weeklyGroceriesToBuy = [];


//let allWeeklyIngredients = Object.values(weeklyMealPlan).flat();
let allWeeklyIngredients = [].concat(...Object.values(weeklyMealPlan));

let weeklyGroceriesToBuy = allWeeklyIngredients.filter((ingredient, index) => {
return allWeeklyIngredients.indexOf(ingredient) === index;
});


/*
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 = [];

let allWeekendIngredients = Object.keys(weeklyMealPlan)
.map((key) => {
if (key === "saturday" || key === "sunday") {
return Object.values(weeklyMealPlan[key]);
}
})
.reduce((acc, val) => acc.concat(val), []);


let weekendGroceriesToBuy = allWeekendIngredients.filter(
(ingredient, index) => {
return (
ingredient !== undefined &&
allWeekendIngredients.indexOf(ingredient) === index
);
}
);

/*
Exercise 3:
Expand All @@ -53,13 +78,21 @@ let numberOfItemsPerWeek = {
saturday: 0,
sunday: 0,
};
let itemsPerDay = Object.keys(numberOfItemsPerWeek).forEach((Day) => {
numberOfItemsPerWeek[Day] = Object.keys(weeklyMealPlan[Day]).length;
});





/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 5-groceries.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 - Weekly groceries to buy contains correct items", () => {
const expectedWeeklyGroceriesToBuy = [
'Cheese', 'Eggs',
Expand All @@ -68,7 +101,7 @@ test("Exercise 1 - Weekly groceries to buy contains correct items", () => {
'Tuna', 'Canned beans',
'Carrot', 'Aubergine',
'Orange Juice', 'Apple',
'Ananas', 'Black tea',
'Banana', 'Black tea',
'Lamb', 'Salt',
'Bulgur', 'Potato',
'Rice milk', 'Blueberries',
Expand All @@ -84,7 +117,7 @@ test("Exercise 2 - Weekend groceries to buy contains correct items", () => {
expect(weekendGroceriesToBuy).toIncludeSameMembers(expectedWeekendGroceriesToBuy);
});

test("Exercise 3 - Numer of items per week contains the correct counts", () => {
test("Exercise 3 - Number of items per week contains the correct counts", () => {
const expectedNumberOfItemsPerWeek = {
monday: 5,
tuesday: 6,
Expand All @@ -95,4 +128,4 @@ test("Exercise 3 - Numer of items per week contains the correct counts", () => {
sunday: 0,
};
expect(numberOfItemsPerWeek).toEqual(expectedNumberOfItemsPerWeek);
});
});
Loading