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
99 changes: 65 additions & 34 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,10 +66,16 @@ Exercise 1:

"Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}."
*/
//var result = writers.map(logAllWriters);
function logAllWriters() {
// write your code to log all writers here
};

for (var 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}.`
);
}
}
//console.log(logAllWriters());
logAllWriters();
/*
Exercise 2:

Expand All @@ -78,10 +84,22 @@ Exercise 2:

"Writer {firstName} {lastName} died at {age} years old."
*/

//var notAlive = writers.filter(logDeadWritersInTheirForties);
function logDeadWritersInTheirForties() {
// write your code here
for (var i = 0; i < writers.length; i++) {
if (
50 > writers[i].age &&
writers[i].age >= 40 &&
writers[i].alive === false
) {
console.log(
`Writer ${writers[i].firstName} ${writers[i].lastName} died at ${writers[i].age} years old.`
);
}
}
}
logDeadWritersInTheirForties();
// write your code here

/*
Exercise 3:
Expand All @@ -92,38 +110,51 @@ Exercise 3:
*/

function logAliveWritersInTheirForties() {
// write your code here
for (var i = 0; i < writers.length; i++) {
if (
writers[i].age >= 40 &&
writers[i].age <= 49 &&
writers[i].alive === true
) {
console.log(
`Hi, my name is ${writers[i].firstName} ${writers[i].lastName}. I am ${writers[i].age} years old.`
);
}
}
}

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 1-writers.js`
logAliveWritersInTheirForties();
/* ======= TESTS - DO NOT MODIFY ===== npm test -- --testPathPattern 1-writers.js
- To run the tests for this exercise, run ``
- 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();
}
32 changes: 32 additions & 0 deletions mandatory/10-cheap-diner.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,41 @@ chosenMeal(emptyArray)
Should give the answer "Nothing :("

**/
let setOne = [
{ name: "Turkey", price: 8.99 },
{ name: "Chicken", price: 13.99 },
{ name: "Lobster", price: 10.99 },
];
let emptyArray = [];
let priceArray = [];

function chooseMeal(mealArray) {

for( var i =0; i < mealArray.length; i ++){
priceArray.push(mealArray[i].price);
}
let sortedPriceArray = priceArray.sort(function(a, b){return a-b});
if (mealArray.length >= 2) {
let indexOfPrice =mealArray.indexOf(sortedPriceArray[1]);
console.log(mealArray[indexOfPrice].name);
}
console.log(chooseMeal(setOne));

/*} else if ((array.length = 1)) {
emptyArray.push(mealArray[0].price);
console.log(emptyArray);
} else {
return null;
}
console.log(chooseMeal(setOne));







//console.log(chooseMeal(setOne));

/* ======= TESTS - DO MODIFY (!!!) =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 10-cheap-diner.js`
Expand Down
48 changes: 37 additions & 11 deletions mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,53 @@
(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
*/
var students = [
{ name: "Ahmed", attendance: 8 },
{ name: "Clement", attendance: 10 },
{ name: "Elamin", attendance: 6 },
{ name: "Adam", attendance: 7 },
{ name: "Tayoa", attendance: 11 },
{ name: "Nina", attendance: 10 },
];
// var studentAttendence = students.filter(function eligibleStudents(element) {
// var students = [];
// if (element.attendance >= 8) {
// return students.push(element.name);
// }
// });

function eligibleStudents(attendances) {

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

console.log(eligibleStudents(students));

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

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

function journeyPlanner(locations, transportMode) {

let arr = [];
for (x in locations) {
if (locations[x].indexOf(transportMode) >= 0) {
arr.push(x);
}
}
return arr;
}
console.log(journeyPlanner(transport, "bus"));

/* ======= 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"],
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 +75,5 @@ test("journeyPlanner function works - case 3", () => {
"Angel",
"London Bridge",
"Tower Bridge",
])
});
]);
});
24 changes: 23 additions & 1 deletion mandatory/4-water-bottle.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,40 @@ 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 ;
if (this.volume > 100){
volume = 100;
}
// calling this function should increase your bottle volume by 10 units;
},
drink: function () {
this.volume -= 10;
if(this.volume < 0 ){
this.volume = 0;
}

// calling this function should decrease your bottle volume by 10 units;
},
isFull: function () {
if(this.volume === 100){
return true;

}else {
return false;
}
// this function should return true if your bottle is full;
},
isEmpty: function () {
isEmpty: function () {
if(this.volume === 0){
return true;
}else{
return false;
}
// this function should return true if your bottle is empty;
},
};
Expand Down
Loading