diff --git a/mandatory/1-writers.js b/mandatory/1-writers.js index 55dc10b9..6fc2f246 100644 --- a/mandatory/1-writers.js +++ b/mandatory/1-writers.js @@ -59,7 +59,9 @@ Exercise 1: "Hi, my name is {firstName} {lastName}. I am {age} years old, and work as a {occupation}." */ - +for (i of writers) { + console.log(`Hi, my name is ${i.firstName} ${i.lastName}. I am ${i.age} years old, and work as a ${i.occupation}.`) +}; /* Exercise 2: @@ -68,7 +70,11 @@ Exercise 2: "Writer {firstName} {lastName} died at {age} years old." */ - +for (i of writers) { + if (i.age >= 40 && i.age < 50 && !i.alive) { + console.log(`Writer ${i.firstName} ${i.lastName} died at ${i.age} years old.`); + } +}; /* Exercise 3: @@ -76,3 +82,8 @@ Exercise 3: "Hi, my name is {firstName} {lastName}. I am {age} years old." */ +for (i of writers) { + if (i.age >= 40 && i.age < 50 && i.alive) { + console.log(`Hi, my name is ${i.firstName} ${i.lastName}. I am ${i.age} years old.`); + } +}; \ No newline at end of file diff --git a/mandatory/2-water-bottle.js b/mandatory/2-water-bottle.js index 1c54abb3..b0ca4782 100644 --- a/mandatory/2-water-bottle.js +++ b/mandatory/2-water-bottle.js @@ -23,19 +23,32 @@ You have to implement the missing features according to the specification. let bottle = { volume: 0, fillUp: function () { - // calling this function should pour your bottle full (volume = 100); + // calling this function should pour your bottle full (volume = 100); + this.volume = 100; }, pour: function () { - // calling this function should increase your bottle volume by 10 unit; + // calling this function should increase your bottle volume by 10 unit; + if (this.volume + 10 > 100) { + this.volume = 100; + } else { + this.volume += 10; + } }, drink: function () { - // calling this function should decrease your bottle volume by 10 unit; + // calling this function should decrease your bottle volume by 10 unit; + if (this.volume - 10 < 0) { + this.volume = 0; + } else { + this.volume -= 10; + } }, isFull: function () { - // this function should return true if your bottle is empty; + // this function should return true if your bottle is full; + return this.volume === 100; }, isEmpty: function () { - // this function should return true if your bottle is full; + // this function should return true if your bottle is empty; + return this.volume === 0; }, }; @@ -52,7 +65,7 @@ Extra question: */ // Write you answer to the question here - +// It is more reliable: for example, if I change the variable name it wouldn't work anymore /* Once you have completed your object run the following and see if your answer matches the expected result at the bottom :) diff --git a/mandatory/3-groceries.js b/mandatory/3-groceries.js index 02486088..ee8f0d01 100644 --- a/mandatory/3-groceries.js +++ b/mandatory/3-groceries.js @@ -29,7 +29,10 @@ Exercise 1: */ // Gather all week item names into this array let weeklyGroceriesToBuy = []; - +for (i in weeklyMealPlan) { + weeklyGroceriesToBuy = weeklyGroceriesToBuy.concat(weeklyMealPlan[i]); +} +console.log(weeklyGroceriesToBuy); /* Exercise 2: Loop through your list again, but now only collect the weekend items into the weeklyGroceriesToBuy array. @@ -37,6 +40,13 @@ Exercise 2: */ // Gather weekend item names into this array let weekendGroceriesToBuy = []; +for (i in weeklyMealPlan) { + + if (i === "saturday" || i === "sunday") { + weekendGroceriesToBuy = weekendGroceriesToBuy.concat(weeklyMealPlan[i]); + } +} +console.log(weekendGroceriesToBuy); /* Exercise 3: @@ -46,7 +56,7 @@ Exercise 3: Finally use console.log() to print out the Object. */ // Gather weekend item names into this object -let numberOfItemsPerWeak = { +let numberOfItemsPerWeek = { monday: 0, tuesday: 0, wednesday: 0, @@ -55,3 +65,8 @@ let numberOfItemsPerWeak = { saturday: 0, sunday: 0, }; + +for (i in weeklyMealPlan) { + numberOfItemsPerWeek[i] = weeklyMealPlan[i].length; +} +console.log(numberOfItemsPerWeek); \ No newline at end of file diff --git a/mandatory/4-people-I-know.js b/mandatory/4-people-I-know.js index ca7618f1..c9e21506 100644 --- a/mandatory/4-people-I-know.js +++ b/mandatory/4-people-I-know.js @@ -386,7 +386,7 @@ First, I want you to find all of my friends who are 35 or older. */ -let thirtyFiveOrOlder = []; +let thirtyFiveOrOlder = people.filter(el => el.age >= 35); /* 3) Find the email address @@ -395,7 +395,7 @@ Next, I want you to find all of the people who work for "POWERNET" and then stor */ -let powerNetEmails = []; +let powerNetEmails = people.filter(el => el.company === "POWERNET").map(el => el.email).reverse(); /* @@ -409,7 +409,7 @@ This time, I only want the full names of the people are who friends with her. */ -let friendsWithStacie = []; +let friendsWithStacie = people.filter(el => el.friends.some(s => s.name === 'Stacie Villarreal')).map(el => `${el.name.first} ${el.name.last}`).reverse(); /* @@ -424,6 +424,15 @@ This time, I only want the full names of the people who can multitask */ let friendsWhoCanMultitask = []; +people.forEach(function(el) { + for (j of el.friends) { + if (j.skills.includes("Multi-tasking")) { + friendsWhoCanMultitask.unshift(j.name); + } + } +}); +let temp = friendsWhoCanMultitask.pop(); +friendsWhoCanMultitask.unshift(temp); /* ================================================== @@ -449,7 +458,7 @@ function test(test_name, actual, expected) { test("Friends are over 35", thirtyFiveOrOlder.length, 5); test("Friends with Stacie Villarreal", friendsWithStacie, [ - "Clay Livingstone", + "Clay Livingston", "Jana Harrison", "Haley Knox", ]); diff --git a/mandatory/5-recipes.js b/mandatory/5-recipes.js index 4c07f439..56bbdc19 100644 --- a/mandatory/5-recipes.js +++ b/mandatory/5-recipes.js @@ -24,4 +24,61 @@ You should write and log at least 5 recipes **/ -let recipes = {}; +let recipes = [ + { + title: "Tiramisu", + servings: 5, + ingredients: [ + "mascarpone", + "eggs", + "coffee", + "chocolate" + ] + }, + { + title: "Pizza", + servings: 1, + ingredients: [ + "flour", + "water", + "passata" + ] + }, + { + title: "Millefoglie", + servings: 4, + ingredients: [ + "custard cream", + "flour", + "icing sugar" + ] + }, + { + title: "Piadina", + servings: 1, + ingredients: [ + "Piadina", + "stracchino", + "Parma ham" + ] + }, + { + title: "Stew", + servings: 6, + ingredients: [ + "onions", + "bay leaves", + "random vegetables" + ] + } +]; + +for (i of recipes) { + console.log(i.title); + console.log(`Serves: ${i.servings}`); + console.log("Ingredients:"); + for (j of i.ingredients) { + console.log(j); + } + console.log(); +} diff --git a/mandatory/6-reading-list.js b/mandatory/6-reading-list.js index 5dde8db9..2e8f6718 100644 --- a/mandatory/6-reading-list.js +++ b/mandatory/6-reading-list.js @@ -13,6 +13,8 @@ Loop through the array of books. For each book, log the book title and book auth You should write and log at least 5 recipes + + ===== Exercise 2 ===== @@ -22,4 +24,42 @@ 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. Tolkien", + read: true + }, + { + title: "Algorithms", + author: "Skiena", + read: false + }, + { + title: "War and Peace", + author: "L. Tolstoj", + read: true + }, + { + title: "Historical dynamics", + author: "Turchin", + read: false + }, + { + title: "Macroalgas y fanerogamas marinas del Mediterraneo occidental", + author: "C. Roriguez-Prieto", + read: false + } +]; + +for (i of books) { + console.log(`${i.title} by ${i.author}`); +} +console.log(); +for (i of books) { + if (i.read) { + console.log(`You already read "${i.title}" by ${i.author}`); + } else { + console.log(`You still need to read "${i.title}" by ${i.author}`); + } +} \ No newline at end of file