diff --git a/mandatory/1-writers.js b/mandatory/1-writers.js index 55dc10b9..caf38bea 100644 --- a/mandatory/1-writers.js +++ b/mandatory/1-writers.js @@ -59,7 +59,10 @@ Exercise 1: "Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}." */ - +writers.forEach(function(writer){ + console.log( `Hi my name is ${writer.firstName} ${writer.lastName}. I am ${writer.age} years old, and I work as a ${writer.occupation}.`); +}); + /* Exercise 2: @@ -68,6 +71,12 @@ Exercise 2: "Writer {firstName} {lastName} died at {age} years old." */ +const writersIn40sAndDead = writers.filter(function(writer){ + if(writers.age <= 40 && writers.age > 50 && writers.alive === 'false') { + } + return console.log(`Writer ${writer.firstName} ${writer.lastName} died at age ${writer.age}`); +}); + /* Exercise 3: @@ -76,3 +85,9 @@ Exercise 3: "Hi, my name is {firstName} {lastName}. I am {age} years old." */ + +const writersIn40sAndAlive = writers.filter(function(writer){ + if(writers.age <= 40 && writers.age > 50 && writers.alive === 'true') { + } + return console.log(`Writer ${writer.firstName} ${writer.lastName} I am ${writer.age} years old`); +}); diff --git a/mandatory/2-water-bottle.js b/mandatory/2-water-bottle.js index 1c54abb3..5c166231 100644 --- a/mandatory/2-water-bottle.js +++ b/mandatory/2-water-bottle.js @@ -22,20 +22,25 @@ You have to implement the missing features according to the specification. // Here is your starting point: let bottle = { volume: 0, - fillUp: function () { + fillUp: function (fill) { // calling this function should pour your bottle full (volume = 100); + return this.volume = this.volume + (100 - this.volume); }, pour: function () { // calling this function should increase your bottle volume by 10 unit; + return this.volume = this.volume <= 90 ? this.volume + 10 : this.volume; }, drink: function () { // calling this function should decrease your bottle volume by 10 unit; + return this.volume = this.volume >= 10 ? this.volume - 10 : this.volume; }, isFull: function () { // this function should return true if your bottle is empty; + return this.volume === 100 ? true : false; }, isEmpty: function () { // this function should return true if your bottle is full; + return this.volume === 0 ? true : false; }, }; diff --git a/mandatory/3-groceries.js b/mandatory/3-groceries.js index 02486088..82e53704 100644 --- a/mandatory/3-groceries.js +++ b/mandatory/3-groceries.js @@ -29,6 +29,12 @@ Exercise 1: */ // Gather all week item names into this array let weeklyGroceriesToBuy = []; + for (j in weeklyMealPlan) { + weeklyGroceriesToBuy = weeklyGroceriesToBuy.concat(weeklyMealPlan[j]); + } + + console.log(weeklyGroceriesToBuy); + /* Exercise 2: @@ -37,7 +43,11 @@ Exercise 2: */ // Gather weekend item names into this array let weekendGroceriesToBuy = []; - +for (j in weeklyMealPlan) +if (j === 'saturday' || j === 'sunday') { + weekendGroceriesToBuy = weekendGroceriesToBuy.concat(weeklyMealPlan[j]); +} +console.log(weekendGroceriesToBuy); /* Exercise 3: Loop through your weekly meal plan: @@ -55,3 +65,9 @@ let numberOfItemsPerWeak = { saturday: 0, sunday: 0, }; + +for (j in weeklyMealPlan) { + numberOfItemsPerWeak[j] = weeklyMealPlan[j].length; +} + +console.log(numberOfItemsPerWeak); diff --git a/mandatory/4-people-I-know.js b/mandatory/4-people-I-know.js index ca7618f1..ffcbfe37 100644 --- a/mandatory/4-people-I-know.js +++ b/mandatory/4-people-I-know.js @@ -344,9 +344,9 @@ let people = [ company: "POWERNET", name: { first: "Clay", - last: "Livingston", + last: "Livingstone", }, - email: "clay.livingston@powernet.com", + email: "clay.livingstone@powernet.com", friends: [ { name: "Stacie Villarreal", @@ -386,7 +386,10 @@ First, I want you to find all of my friends who are 35 or older. */ -let thirtyFiveOrOlder = []; +//arrow function +let thirtyFiveOrOlder = people.filter(arr => arr.age >= 35); + + /* 3) Find the email address @@ -395,21 +398,28 @@ Next, I want you to find all of the people who work for "POWERNET" and then stor */ -let powerNetEmails = []; +let powerNetEmails = people.filter(arr => arr.company === "POWERNET").map(arr => arr.email).reverse(); /* 3) Friends with "Stacie Villarreal" - Next, I want you to find all of my friends who are friends with Stacie Villarreal. - You can see who people's friends are by seeing the "friends" array in each of my friends objects. - This time, I only want the full names of the people are who friends with her. - */ + let friendsWithStacie = []; +for (let person of people) { + for (let friend of person.friends) { + if (friend.name === "Stacie Villarreal") { + friendsWithStacie.push(`${person.name.first} ${person.name.last}`); + } + } +} +friendsWithStacie.reverse().forEach(f => console.log(f)); + + /* @@ -423,7 +433,10 @@ This time, I only want the full names of the people who can multitask */ -let friendsWhoCanMultitask = []; +let friendsWhoCanMultitask = people.flatMap((info) => +info.friends.filter(hello => hello.skills.includes("Multi-tasking"))) +.map((elem)=> `${elem.name}`); +console.log(friendsWhoCanMultitask); /* ================================================== @@ -455,7 +468,7 @@ test("Friends with Stacie Villarreal", friendsWithStacie, [ ]); test("Powernet email addresses", powerNetEmails, [ - "clay.livingston@powernet.com", + "clay.livingstone@powernet.com", "gloria.hall@powernet.com", ]); diff --git a/mandatory/5-recipes.js b/mandatory/5-recipes.js index 4c07f439..cafeb941 100644 --- a/mandatory/5-recipes.js +++ b/mandatory/5-recipes.js @@ -24,4 +24,43 @@ You should write and log at least 5 recipes **/ -let recipes = {}; +let recipes = [ + { + title: 'Pizza', + serves: 2, + ingredients: ['pizza dough', 'Mozarella', 'Pizza sauce'], + +}, +{ + title: 'Beans on toast', + serves: 1, + ingredients: ['Beans', 'Bread', 'Butter'], +}, +{ + title: 'Pasta Bake', + serves: 4, + ingredients: ['Pasta', 'Pasta sauce', 'Cheese'], +}, + +{ + title: 'Bangers and mash', + serves: 2, + ingredients: ['Sausages', 'Potatoes', 'Gravy'], +}, + +{ + title: 'Apple pie and custard', + serves: 2, + ingredients: ['Apples', 'Custard', 'Shortcrust pastry'], + +} +]; + +for (i of recipes) { + console.log(i.title); + console.log(`Serves: ${i.serves}`); + console.log("Ingredients:"); + for (j of i.ingredients) { + console.log(j); + } +} \ No newline at end of file diff --git a/mandatory/6-reading-list.js b/mandatory/6-reading-list.js index 5dde8db9..021d595f 100644 --- a/mandatory/6-reading-list.js +++ b/mandatory/6-reading-list.js @@ -22,4 +22,45 @@ If you read it, log a string like 'You already read "The Hobbit" by J.R.R. Tolki **/ -let books = []; +let books = [ + { + title: 'The Hobbit', + author: 'J.R.R. TOLKEIN', + read: false, + }, + { + title: 'The Lord of the rings', + author: 'J.R.R. TOLKEIN', + read: false, + }, + { + title: 'The Two Towers', + author: 'J.R.R. TOLKEIN', + read: true, + }, + { + title: 'The King returns', + author: 'J.R.R. TOLKEIN', + read: false, + }, + { + title: 'The Hobbit: Battle of 5 armys', + author: 'J.R.R. TOLKEIN', + read: true, + }, +]; + +for (i of books) { + console.log(`${i.title} by ${i.author}`); +} + + +// exercise 2 + +for (i of books) { + if (i.read === true) { + console.log(`You have read ${i.title} by ${i.author}`); + } else { + console.log(`You still need to read ${i.title} by ${i.author}`); + } +}