From d93e8df58fd82f74e307c594644b4519c4709966 Mon Sep 17 00:00:00 2001 From: Daniel Wang Date: Sat, 16 Oct 2021 15:25:13 +0200 Subject: [PATCH] all homework completed --- .../practice-exercises/1-remove-the-comma.js | 2 +- .../practice-exercises/2-even-odd-reporter.js | 7 +++ Week2/practice-exercises/3-recipe-card.js | 6 ++ Week2/practice-exercises/4-reading-list.js | 60 +++++++++++++++++++ .../practice-exercises/5-who-wants-a-drink.js | 48 ++++++++++++++- .../solutions/5-who-wants-a-drink.js | 3 +- 6 files changed, 123 insertions(+), 3 deletions(-) diff --git a/Week2/practice-exercises/1-remove-the-comma.js b/Week2/practice-exercises/1-remove-the-comma.js index b71cffd..be03e08 100644 --- a/Week2/practice-exercises/1-remove-the-comma.js +++ b/Week2/practice-exercises/1-remove-the-comma.js @@ -5,7 +5,7 @@ * hello this is a difficult to read sentence */ -let myString = 'hello,this,is,a,difficult,to,read,sentence'; +let myString = `hello this is a difficult to read sentence`; diff --git a/Week2/practice-exercises/2-even-odd-reporter.js b/Week2/practice-exercises/2-even-odd-reporter.js index 6edf23e..128cdce 100644 --- a/Week2/practice-exercises/2-even-odd-reporter.js +++ b/Week2/practice-exercises/2-even-odd-reporter.js @@ -7,3 +7,10 @@ * If it's even, log to the console The number [PUT_NUMBER_HERE] is even!. */ +for (i = 0;i < 21; i++){ + if (i%2 ==1 ){ + console.log(`${i} is odd!`)} + else{ + console.log(`${i} is even!`) + } + } diff --git a/Week2/practice-exercises/3-recipe-card.js b/Week2/practice-exercises/3-recipe-card.js index 24bcb54..4fb08d6 100644 --- a/Week2/practice-exercises/3-recipe-card.js +++ b/Week2/practice-exercises/3-recipe-card.js @@ -12,3 +12,9 @@ * Ingredients: 4 eggs, 2 strips of bacon, 1 tsp salt/pepper */ +let mealRecipie = {} +mealRecipie.mealName = 'Omelette' +mealRecipie.serves = 2 +mealRecipie.ingredients = ["4 eggs", "2 strips of bacon", "1 tsp salt/pepper"] + +console.log(mealRecipie) \ No newline at end of file diff --git a/Week2/practice-exercises/4-reading-list.js b/Week2/practice-exercises/4-reading-list.js index f535657..c23671b 100644 --- a/Week2/practice-exercises/4-reading-list.js +++ b/Week2/practice-exercises/4-reading-list.js @@ -9,3 +9,63 @@ * If you haven't read it log a string like You still need to read "The Lord of the Rings" */ + +class book { + constructor(title, author, alreadyRead){ + this.title = title + this.author = author + this.alreadyRead = alreadyRead + this.bookRegistered = false; + } + bookRegistry(){ + this.bookRegistered = true; + return`${this.title}is registered` + } +} + +const book1 = new book ("The Bobbit","J.R.R Tolkien", false) +const book2 = new book ("Harry Potter","J.K. Rowling", true) +const book3 = new book ("The Lord of the Rings","J.R.R Tolkien", false) + +let readingList = [book1,book2,book3] + +console.log(readingList[0].alreadyRead) + +for (i = 0 ; i < 3; i++){ + if (readingList[i].alreadyRead === true) + { + console.log(`You have read ${readingList[i].title}`) + } + else{ + console.log(`You have not read ${readingList[i].title} yet`) + } +} + + +//another way of doing it +/** +function BookRepo (title,author,alreadyRead) +{ + this.title = title; + this.author = author; + this.alreadyRead = alreadyRead; +} + +const book1 = new BookRepo ("The Bobbit","J.R.R Tolkien", false) +const book2 = new BookRepo ("Harry Potter","J.K. Rowling", true) +const book3 = new BookRepo ("The Lord of the Rings","J.R.R Tolkien", false) + +let readingList = [book1,book2,book3] + +console.log(readingList[0].alreadyRead) + +for (i = 0 ; i < 3; i++){ + if (readingList[i].alreadyRead === true) + { + console.log(`You have read ${readingList[i].title}`) + } + else{ + console.log(`You have not read ${readingList[i].title} yet`) + } +} +*/ \ No newline at end of file diff --git a/Week2/practice-exercises/5-who-wants-a-drink.js b/Week2/practice-exercises/5-who-wants-a-drink.js index f37f02b..14a8d2d 100644 --- a/Week2/practice-exercises/5-who-wants-a-drink.js +++ b/Week2/practice-exercises/5-who-wants-a-drink.js @@ -8,4 +8,50 @@ */ // There are 3 different types of drinks: -const drinkTypes = ['cola', 'lemonade', 'water']; \ No newline at end of file +const drinkTypes = ['cola', 'lemonade', 'water']; + +function chooseOne(drinkTypes){ +return drinkTypes[Math.floor(Math.random()*drinkTypes.length)] +} +console.log(chooseOne(drinkTypes)) + +let drinkTray = [] + +for (i=0; i<5; i++){ + drinkTray[i] = chooseOne(drinkTypes) +} + +console.log(drinkTray) + +const isCola = drinkTray.find(function(drink) { + if (drink == "cola") { + return true; + } else { + return false; + } + }); + + const isLemon = drinkTray.find(function(drink) { + if (drink == "lemonade") { + return true; + } else { + return false; + } + }); + + const isWater = drinkTray.find(function(drink) { + if (drink == "water") { + return true; + } else { + return false; + } + }); + +if (isCola&&isLemon&&isWater == true) +{ + console.log(`Hey guys! I've brought ${drinkTray} for you guys! Enjoy!`) +} +else{ + console.log(`task failed`) +} + diff --git a/Week2/practice-exercises/solutions/5-who-wants-a-drink.js b/Week2/practice-exercises/solutions/5-who-wants-a-drink.js index 5db1ea2..3b0a9ef 100644 --- a/Week2/practice-exercises/solutions/5-who-wants-a-drink.js +++ b/Week2/practice-exercises/solutions/5-who-wants-a-drink.js @@ -21,4 +21,5 @@ for (let i = 0; i < drinkTypes.length; i++) { } } -console.log(`Hey guys, I brought a ${drinkTray.join(', ')}!`); \ No newline at end of file +console.log(`Hey guys, I brought a ${drinkTray.join(', ')}!`); +