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
18 changes: 15 additions & 3 deletions mandatory/1-writers.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,13 @@ 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
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 @@ -80,7 +85,10 @@ Exercise 2:
*/

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

/*
Expand All @@ -92,7 +100,11 @@ Exercise 3:
*/

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

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
9 changes: 9 additions & 0 deletions mandatory/10-cheap-diner.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ Should give the answer "Nothing :("
**/

function chooseMeal(mealArray) {
mealArray.sort((mealA, mealB) => mealA.price - mealB.price);

const secondCheapest = mealArray[1];
const cheapest = mealArray[0];
const noMeal = "Nothing :(";

if (secondCheapest) return secondCheapest.name;
else if (cheapest) return cheapest.name;
else return noMeal;
}

/* ======= TESTS - DO MODIFY (!!!) =====
Expand Down
10 changes: 9 additions & 1 deletion mandatory/2-eligible-students.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,15 @@
*/

function eligibleStudents(attendances) {

return attendances.filter(isEligible).map(getName);
}

function getName (student){
return student.name;
}

function isEligible(student){
return student.attendance >= 8;
}

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

function journeyPlanner(locations, transportMode) {

return Object.keys(locations)
.filter(location => locations[location].includes(transportMode));
}

/* ======= TESTS - DO NOT MODIFY =====
Expand Down
15 changes: 9 additions & 6 deletions mandatory/4-water-bottle.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,23 @@ You have to implement the missing features according to the specification.
// Here is your starting point:
let bottle = {
volume: 0,
fullCap: 100,
fillUp: function () {
// calling this function should completely fill your bottle (volume = 100);
if (!this.isFull()) this.volume = this.fullCap;
},
pour: function () {
// calling this function should increase your bottle volume by 10 units;
const tenDrinkUnits = 10;
if (!this.isFull()) this.volume += tenDrinkUnits;
},
drink: function () {
// calling this function should decrease your bottle volume by 10 units;
const tenDrinkUnits = 10;
if (!this.isEmpty()) this.volume -= tenDrinkUnits;
},
isFull: function () {
// this function should return true if your bottle is full;
return this.volume === this.fullCap;
},
isEmpty: function () {
// this function should return true if your bottle is empty;
return this.volume === 0;
},
};

Expand All @@ -51,7 +54,7 @@ Extra question:
Leave your answer below:
*/

// Write you answer to the question here
//I think it has to do with scope bottle can only be used inside this function this can be used outside if needed.

/*
Once you have completed your object run the following
Expand Down
16 changes: 16 additions & 0 deletions mandatory/5-groceries.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,25 @@ Exercise 1:
*/
// Gather all week item names into this array
let weeklyGroceriesToBuy = [];
Object.values(weeklyMealPlan).forEach((ingredientsForDay) => {
ingredientsForDay.forEach((ingredient) => {
if (!weeklyGroceriesToBuy.includes(ingredient)) weeklyGroceriesToBuy.push(ingredient);
});
});

/*
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 = [];
Object.entries(weeklyMealPlan).forEach((keyValuePair) => {
const day = keyValuePair[0];
const ingredientsForDay = keyValuePair[1];
if (day === "saturday" || day === "sunday") {
weekendGroceriesToBuy = weekendGroceriesToBuy.concat(ingredientsForDay);
}
});

/*
Exercise 3:
Expand All @@ -54,6 +66,10 @@ let numberOfItemsPerWeek = {
sunday: 0,
};

Object.entries(weeklyMealPlan).forEach(([day, ingredientsForDay]) => {
numberOfItemsPerWeek[day] = ingredientsForDay.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`
Expand Down
36 changes: 33 additions & 3 deletions mandatory/6-people-I-know.js
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,11 @@ First, I want you to find all of my friends who are 35 or older.

*/

let thirtyFiveOrOlder = [];
let thirtyFiveOrOlder = friends
.filter(function (friend) {
const lowerAge = 35;
return friend.age >= lowerAge;
});

/*
3) Find the email address
Expand All @@ -391,7 +395,14 @@ Next, I want you to find all of my friends who work for "POWERNET" and then stor

*/

let powerNetEmails = [];
let powerNetEmails = friends
.filter(function (friend) {
const companySearch = "POWERNET";
return friend.company === companySearch;
})
.map(function (friend) {
return friend.email;
});

/*

Expand All @@ -405,7 +416,16 @@ This time, I only want the full names ("<firstname> <lastname>") of my friends w

*/

let friendsWhoAreColleaguesOfStacie = [];
let friendsWhoAreColleaguesOfStacie = friends
.filter(function isColleagueWithStacie(person) {
const colleagueToFind = "Stacie Villarreal";
return person.colleagues.some((colleague) => colleague.name === colleagueToFind);
})
.map(function getName(colleague) {
const firstName = colleague.name.first;
const lastName = colleague.name.last;
return `${firstName} ${lastName}`;
});
/*

5) Find "Multi-tasking" colleagues
Expand All @@ -419,6 +439,16 @@ This time, I only want the full names of the people who can multitask
*/

let colleaguesWhoCanMultitask = [];
friends
.map((person) => person.colleagues)
.forEach((colleagueList) => {
const thoseWhoMultiTask = colleagueList.filter((colleague) => {
const wantedTask = "Multi-tasking";
return colleague.skills.includes(wantedTask);
});
const fullNames = thoseWhoMultiTask.map((colleague) => colleague.name);
colleaguesWhoCanMultitask = colleaguesWhoCanMultitask.concat(fullNames);
});

/* ======= TESTS - DO NOT MODIFY =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 6-people-I-know.js`
Expand Down
21 changes: 20 additions & 1 deletion mandatory/7-recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,24 @@ You should write and log at least 5 recipes

**/

let recipes = {};
let recipes = {
title: "Apple Pie",
servings: 3,
ingredients: [
"Granulated Sugar",
"Thinly Sliced Apples",
"Butter",
"Cinnamon",
"Double Flake Pastry",
"Chubby Sparkling Sugar",
],
};

console.log(recipe.title);
console.log(`Serves ${recipe.servings}`);
console.log("Ingredients:");

recipe.ingredients.forEach(function (ingredient) {
console.log(ingredient);
});

45 changes: 42 additions & 3 deletions mandatory/8-reading-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,40 @@ without using any variables or any logic like loops, template strings or if stat

*/

const books = [];
const books = [
{
title: "The Shining",
author: "Stephen King",
haveRead: true,
},
{
title: "Tommyknockers",
author: "Stephen King",
haveRead: true,
},
{
title: "Prey",
author: "Micheal Crichton",
haveRead: true,
},
{
title: "Jurassic Park",
author: "Micheal Crichton",
haveRead: true,
},
{
title: "Mr Mercedes",
author: "Stephen King",
haveRead: false,
}
];

// exercise 1
function logBooks() {
}
//function logBooks() {
//books.forEach(function (book) {
//console.log(`${book.title} by ${book.author}`);
//});
//}


/*
Expand Down Expand Up @@ -61,6 +90,16 @@ As an example for this exercise, you might do the following steps

**/

function logBooks() {
books.forEach(function (book) {
if (book.haveRead) {
console.log(`You have already read ${book.title} by ${book.author}`);
} else {
console.log(`You have NOT read ${book.title} by ${book.author}`);
}
});
}

/* ======= TESTS - DO MODIFY (!!!) =====
- To run the tests for this exercise, run `npm test -- --testPathPattern 8-reading-list.js`
- To run all exercises/tests in the mandatory folder, run `npm test`
Expand Down
5 changes: 5 additions & 0 deletions mandatory/9-budgets.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ Should give return the answer of 62600.
**/

function getBudgets(peopleArray) {
let total = 0;
for (const person of peopleArray) {
total += person.budget;
}
return total;
}

/* ======= TESTS - DO MODIFY (!!!) =====
Expand Down
51 changes: 51 additions & 0 deletions mandatory/let writers = [.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
let writers = [
{
firstName: "Virginia",
lastName: "Woolf",
occupation: "writer",
age: 59,
alive: false,
},
{
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
},
{
firstName: "Jane",
lastName: "Austen",
occupation: "writer",
age: 41,
alive: false,
},
{
firstName: "Bell",
lastName: "Hooks",
occupation: "writer",
age: 63,
alive: true,
},
{
firstName: "Yukiko",
lastName: "Motoya",
occupation: "writer",
age: 49,
alive: true,
}
];

/*
Exercise 1:

Loop through the Array, and for each object, use `console.log()` to print out the below sentence
and insert the corresponding values to the place holders that are indicated in curly braces:

"Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}."
*/
function logAllWriters(writers) {
`Hi, my name is ${writers.firstName} ${writers.lastName}. I am ${writers.age} years old, and work as a ${writers.occupation}`;
};

console.log([logAllWriters]);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"version": "1.0.0",
"description": "Exercises for JS2 Week 1",
"scripts": {
"test": "jest --testRegex='mandatory[/\\\\].*\\.js$' --testPathIgnorePatterns='choose-your-own|recipes|water-bottle'"
"test": "jest --testRegex='mandatory[/\\\\].*\\.js$'"
},
"repository": {
"type": "git",
Expand Down