-
-
Notifications
You must be signed in to change notification settings - Fork 265
Glasgow Class 6 Melese Berehannu JavaScript-Core-2-Coursework-Week1 #218
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,10 +13,10 @@ let car = { | |
| brand: "Ford", | ||
| yearsOld: 8, | ||
| }; | ||
|
|
||
| // no colour property defined | ||
| console.log(car["colour"]); | ||
|
|
||
| // Example 2 | ||
| // Example 2 the property firstName is not defined | ||
| function sayHelloToUser(user) { | ||
| console.log(`Hello ${user.firstName}`); | ||
| } | ||
|
|
@@ -34,5 +34,5 @@ let myPet = { | |
| "My pet's name is Fluffy"; | ||
| }, | ||
| }; | ||
|
|
||
| //we should log myPet.getName only () not required | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not the case. getName: function() {
return "My pet's name is Fluffy";
} |
||
| console.log(myPet.getName()); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,9 +9,13 @@ | |
|
|
||
| let student = { | ||
| // write code here | ||
| getName: function getName(name){ | ||
| return name | ||
| } | ||
|
Comment on lines
+12
to
+14
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great 👏 |
||
| } | ||
|
|
||
| student.getName("Daniel"); | ||
| let studentName = student.getName("Daniel"); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't affect the code working, but we should only use |
||
| console.log(`Student name: ${studentName}`) | ||
|
|
||
| /* EXPECTED RESULT | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,5 +21,31 @@ | |
|
|
||
| You should write and log at least 5 recipes | ||
| */ | ||
| let favouriteRecipe = { | ||
| ScrambleEgg: { title: 'Sclamble Egg', | ||
| servings: 2, | ||
| Ingredients: ['Egg', 'Oil', 'Salt'] | ||
| },Smoothies :{ | ||
| title:'Smoothie', | ||
| servings: 4, | ||
| Ingredients: ['Milk','strawberry','banana'], | ||
|
|
||
|
|
||
| },CottagePie:{ | ||
| title: 'CottagePie', | ||
| servings: 10, | ||
| Ingredients: ['Tender meat', 'CreamyMashedPotatoes', 'Milk'] | ||
| },Roastedchicken:{ | ||
| title: 'Roastedchicken', | ||
| servings: 7, | ||
| Ingredients: ['Chicken', 'Oil', 'Salt'] | ||
| },MacaronniCheese:{ | ||
| title: 'MacarronniCheese', | ||
| servings: 5, | ||
| Ingredients: ['Macharpnni', 'Cheese', 'Salt'] | ||
| }} | ||
| for (item of favouriteRecipe){ | ||
| console.log(item.) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This exercise has made me hungry 😂 |
||
|
|
||
| } | ||
| // write code here | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,17 @@ const COUNTRY_CURRENCY_CODES = [ | |
| ]; | ||
|
|
||
| function createLookup(countryCurrencyCodes) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great 👏 |
||
| // write code here | ||
| // write code here | ||
| const newObject = new Object() ; | ||
| for (let countryCurrencyCode of countryCurrencyCodes){ | ||
|
|
||
| let countryCode = countryCurrencyCode[1]; | ||
| let country = countryCurrencyCode[0]; | ||
|
Comment on lines
+25
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, here we can use const. It may seem that we are reassigning these variables as we loop through the array, but we aren't, we are creating new ones on each iteration. This means they are never reassigned and disappear after each loop iteration. |
||
|
|
||
| newObject[country] = countryCode; | ||
|
|
||
|
|
||
| }return newObject; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Be careful with formatting here. The indentation on line 23 and this return statement should be on a new line. |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,24 @@ let pantry = { | |
|
|
||
| function createShoppingList(recipe) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice! Another way of doing this would be to use filter: const missingIngredients = recipe.ingredients.filter(function (item) {
!pantryContents.includes(ingredients)
} |
||
| // write code here | ||
|
|
||
| const createShoppingListMissing = new Object(); | ||
| const ingredientsMssing = []; | ||
| const pantryContents = pantry.fridgeContents.concat(pantry.cupboardContents); | ||
| for (let ingredient of recipe.ingredients){ | ||
| if(pantryContents.includes(ingredient)===false){ | ||
| ingredientsMssing.push(ingredient); | ||
| } | ||
| } | ||
| createShoppingListMissing.name = recipe.name; | ||
| createShoppingListMissing.items = ingredientsMssing; | ||
| return createShoppingListMissing; | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,8 +21,28 @@ const MENU = { | |
|
|
||
| let cashRegister = { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great |
||
| // write code here | ||
| orderBurger: function(balance){ | ||
| if(balance >= MENU.burger){ | ||
| let newBalance = balance - MENU.burger; | ||
| return newBalance; | ||
| }else{ | ||
| return balance; | ||
| } | ||
|
|
||
| }, | ||
| orderFalafel: function(balance){ | ||
| if(balance >= MENU.falafel){ | ||
| let newBalance = balance - MENU.falafel; | ||
| return newBalance; | ||
| }else{ | ||
| return balance; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` | ||
| - To run all exercises/tests in the mandatory folder, run `npm test` | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works here since all the strings use upper case for the first letter. Be careful with using sort for strings without passing a custom function. This is because all uppercase letters are "smaller" than lower case ones. So "M" for example would be sorted before "a". Here is an article explaining in more detail and how to handle it if you want to make your sort case insensitive https://www.danywalls.com/understand-how-the-sort-method-operates-on-javascript-arrays