From 78d58e31abd6f0eac1acdc99c8a3d7f28770d267 Mon Sep 17 00:00:00 2001 From: Cecilia Date: Sat, 12 Dec 2020 15:25:19 +0000 Subject: [PATCH 1/3] Completed mandatory exercises - test 4 not working --- mandatory/1-writers.js | 15 +++++++-- mandatory/2-water-bottle.js | 25 +++++++++++---- mandatory/3-groceries.js | 18 +++++++++-- mandatory/4-people-I-know.js | 30 +++++++++++++++--- mandatory/5-recipes.js | 59 +++++++++++++++++++++++++++++++++++- mandatory/6-reading-list.js | 42 ++++++++++++++++++++++++- 6 files changed, 173 insertions(+), 16 deletions(-) diff --git a/mandatory/1-writers.js b/mandatory/1-writers.js index 55dc10b9..0a72a96b 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) { + 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.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..593c99c0 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 += 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,12 @@ Exercise 2: */ // Gather weekend item names into this array let weekendGroceriesToBuy = []; +for (i in weeklyMealPlan) { + if (i === "saturday" || i === "sunday") { + weekendGroceriesToBuy += weeklyMealPlan[i]; + } +} +console.log(weekendGroceriesToBuy); /* Exercise 3: @@ -46,7 +55,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 +64,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..435bc17d 100644 --- a/mandatory/4-people-I-know.js +++ b/mandatory/4-people-I-know.js @@ -387,7 +387,11 @@ First, I want you to find all of my friends who are 35 or older. */ let thirtyFiveOrOlder = []; - +for (i of people) { + if (i.age >= 35) { + thirtyFiveOrOlder.push(i.name); + } +} /* 3) Find the email address @@ -396,7 +400,11 @@ Next, I want you to find all of the people who work for "POWERNET" and then stor */ let powerNetEmails = []; - +for (i of people) { + if (i.company === "POWERNET") { + powerNetEmails.unshift(i.email); + } +} /* 3) Friends with "Stacie Villarreal" @@ -410,7 +418,14 @@ This time, I only want the full names of the people are who friends with her. */ let friendsWithStacie = []; - +for (i of people) { + for (j of i.friends) { + if (j.name === "Stacie Villarreal") { + let name = `${i.name.first} ${i.name.last}`; + friendsWithStacie.unshift(name); + } + } +} /* 4) Find "Multi-tasking" friends @@ -424,6 +439,13 @@ This time, I only want the full names of the people who can multitask */ let friendsWhoCanMultitask = []; +for (i of people) { + for (j of i.friends) { + if (j.skills.includes("Multi-tasking")) { + friendsWhoCanMultitask.push(j.name); + } + } +} /* ================================================== @@ -449,7 +471,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..af26209a 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 read "${i.title}" by ${i.author}`); + } +} \ No newline at end of file From a983fda6c969d62f365098c1680bf3b6854bb8a8 Mon Sep 17 00:00:00 2001 From: Cecilia Date: Sun, 10 Jan 2021 13:53:38 +0000 Subject: [PATCH 2/3] Fixed exercises 1, 3 and 6. Fixed tests for exercise 4. --- mandatory/1-writers.js | 4 ++-- mandatory/3-groceries.js | 5 +++-- mandatory/4-people-I-know.js | 8 ++++---- mandatory/6-reading-list.js | 2 +- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/mandatory/1-writers.js b/mandatory/1-writers.js index 0a72a96b..6fc2f246 100644 --- a/mandatory/1-writers.js +++ b/mandatory/1-writers.js @@ -71,7 +71,7 @@ Exercise 2: "Writer {firstName} {lastName} died at {age} years old." */ for (i of writers) { - if (i.age >= 40 && i.age < 50) { + if (i.age >= 40 && i.age < 50 && !i.alive) { console.log(`Writer ${i.firstName} ${i.lastName} died at ${i.age} years old.`); } }; @@ -83,7 +83,7 @@ Exercise 3: "Hi, my name is {firstName} {lastName}. I am {age} years old." */ for (i of writers) { - if (i.alive) { + 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/3-groceries.js b/mandatory/3-groceries.js index 593c99c0..ee8f0d01 100644 --- a/mandatory/3-groceries.js +++ b/mandatory/3-groceries.js @@ -30,7 +30,7 @@ Exercise 1: // Gather all week item names into this array let weeklyGroceriesToBuy = []; for (i in weeklyMealPlan) { - weeklyGroceriesToBuy += weeklyMealPlan[i]; + weeklyGroceriesToBuy = weeklyGroceriesToBuy.concat(weeklyMealPlan[i]); } console.log(weeklyGroceriesToBuy); /* @@ -41,8 +41,9 @@ Exercise 2: // Gather weekend item names into this array let weekendGroceriesToBuy = []; for (i in weeklyMealPlan) { + if (i === "saturday" || i === "sunday") { - weekendGroceriesToBuy += weeklyMealPlan[i]; + weekendGroceriesToBuy = weekendGroceriesToBuy.concat(weeklyMealPlan[i]); } } console.log(weekendGroceriesToBuy); diff --git a/mandatory/4-people-I-know.js b/mandatory/4-people-I-know.js index 435bc17d..e744632f 100644 --- a/mandatory/4-people-I-know.js +++ b/mandatory/4-people-I-know.js @@ -476,15 +476,15 @@ test("Friends with Stacie Villarreal", friendsWithStacie, [ "Haley Knox", ]); -test("Powernet email addresses", powerNetEmails, [ +test("Powernet email addresses", new Set(powerNetEmails), new Set([ "clay.livingston@powernet.com", "gloria.hall@powernet.com", -]); +])); -test("Friends who can multitask", friendsWhoCanMultitask, [ +test("Friends who can multitask", new Set(friendsWhoCanMultitask), new Set([ "Rush May", "Luz Newton", "Castro Castaneda", "Cunningham Shelton", "Gena Good", -]); +])); diff --git a/mandatory/6-reading-list.js b/mandatory/6-reading-list.js index af26209a..2e8f6718 100644 --- a/mandatory/6-reading-list.js +++ b/mandatory/6-reading-list.js @@ -60,6 +60,6 @@ for (i of books) { if (i.read) { console.log(`You already read "${i.title}" by ${i.author}`); } else { - console.log(`You still need read "${i.title}" by ${i.author}`); + console.log(`You still need to read "${i.title}" by ${i.author}`); } } \ No newline at end of file From a9d6b7438288dcd4be5b7caecae98d82dd9c5eae Mon Sep 17 00:00:00 2001 From: Cecilia Date: Sun, 10 Jan 2021 15:36:29 +0000 Subject: [PATCH 3/3] Improved exercise 4 solutions --- mandatory/4-people-I-know.js | 47 +++++++++++++----------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/mandatory/4-people-I-know.js b/mandatory/4-people-I-know.js index e744632f..c9e21506 100644 --- a/mandatory/4-people-I-know.js +++ b/mandatory/4-people-I-know.js @@ -386,12 +386,8 @@ First, I want you to find all of my friends who are 35 or older. */ -let thirtyFiveOrOlder = []; -for (i of people) { - if (i.age >= 35) { - thirtyFiveOrOlder.push(i.name); - } -} +let thirtyFiveOrOlder = people.filter(el => el.age >= 35); + /* 3) Find the email address @@ -399,12 +395,8 @@ Next, I want you to find all of the people who work for "POWERNET" and then stor */ -let powerNetEmails = []; -for (i of people) { - if (i.company === "POWERNET") { - powerNetEmails.unshift(i.email); - } -} +let powerNetEmails = people.filter(el => el.company === "POWERNET").map(el => el.email).reverse(); + /* 3) Friends with "Stacie Villarreal" @@ -417,15 +409,8 @@ This time, I only want the full names of the people are who friends with her. */ -let friendsWithStacie = []; -for (i of people) { - for (j of i.friends) { - if (j.name === "Stacie Villarreal") { - let name = `${i.name.first} ${i.name.last}`; - friendsWithStacie.unshift(name); - } - } -} +let friendsWithStacie = people.filter(el => el.friends.some(s => s.name === 'Stacie Villarreal')).map(el => `${el.name.first} ${el.name.last}`).reverse(); + /* 4) Find "Multi-tasking" friends @@ -439,13 +424,15 @@ This time, I only want the full names of the people who can multitask */ let friendsWhoCanMultitask = []; -for (i of people) { - for (j of i.friends) { +people.forEach(function(el) { + for (j of el.friends) { if (j.skills.includes("Multi-tasking")) { - friendsWhoCanMultitask.push(j.name); + friendsWhoCanMultitask.unshift(j.name); } - } -} + } +}); +let temp = friendsWhoCanMultitask.pop(); +friendsWhoCanMultitask.unshift(temp); /* ================================================== @@ -476,15 +463,15 @@ test("Friends with Stacie Villarreal", friendsWithStacie, [ "Haley Knox", ]); -test("Powernet email addresses", new Set(powerNetEmails), new Set([ +test("Powernet email addresses", powerNetEmails, [ "clay.livingston@powernet.com", "gloria.hall@powernet.com", -])); +]); -test("Friends who can multitask", new Set(friendsWhoCanMultitask), new Set([ +test("Friends who can multitask", friendsWhoCanMultitask, [ "Rush May", "Luz Newton", "Castro Castaneda", "Cunningham Shelton", "Gena Good", -])); +]);