-
-
Notifications
You must be signed in to change notification settings - Fork 265
NW5 Manchester - Doris Siu - JS2 - Week1 #119
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 |
|---|---|---|
|
|
@@ -21,7 +21,9 @@ let basketballTeam = { | |
| */ | ||
|
|
||
| // write code here | ||
| let topPlayersArr = basketballTeam.topPlayers.sort(); | ||
|
|
||
| topPlayersArr.forEach(player => console. log(player)); | ||
|
Comment on lines
+24
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. really good, well done |
||
|
|
||
| /* EXPECTED RESULT | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,11 @@ let capitalCities = { | |
| */ | ||
|
|
||
| // write code here | ||
| capitalCities.UnitedKingdom.population = 8980000; | ||
| capitalCities.China.population = 21500000; | ||
| capitalCities.Peru = {}; | ||
| capitalCities.Peru.name = "Lima"; | ||
| capitalCities.Peru.population = 975000; | ||
|
Comment on lines
+28
to
+30
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 works, you can also set the properties of the Peru object all at once, by setting the name & population when you create the object |
||
|
|
||
| console.log(capitalCities); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,11 @@ let student = { | |
| */ | ||
|
|
||
| // write code here | ||
| student["attendence"] = 90; | ||
|
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. good use of bracket notation here |
||
|
|
||
| if (student["attendence"] >= 90 && student["examScore"] > 60) { | ||
| student["hasPassed"] = true; | ||
| } | ||
|
|
||
| /* | ||
| - Write an "if" statement that changes the value of hasPassed to true | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ let car = { | |
|
|
||
| console.log(car["colour"]); | ||
|
|
||
| // Because there's no such a property named colour in the object car. | ||
|
|
||
| // Example 2 | ||
| function sayHelloToUser(user) { | ||
| console.log(`Hello ${user.firstName}`); | ||
|
|
@@ -27,6 +29,8 @@ let user = { | |
|
|
||
| sayHelloToUser(user); | ||
|
|
||
| // Because there's no such a property called firstName in the object user. | ||
|
|
||
| // Example 3 | ||
| let myPet = { | ||
| animal: "Cat", | ||
|
|
@@ -36,3 +40,5 @@ let myPet = { | |
| }; | ||
|
|
||
| console.log(myPet.getName()); | ||
|
|
||
| // Because in the object method, it omits the keyword return for the string. | ||
|
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. good explanations for why JavaScript is returning undefined |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,4 +22,49 @@ | |
| You should write and log at least 5 recipes | ||
| */ | ||
|
|
||
| // write code here | ||
| // write code here | ||
|
|
||
| let chicken = { | ||
| title:"chicken", | ||
| serves:4, | ||
| ingredients: ["chicken","potatoes"] | ||
| } | ||
|
|
||
| let pork = { | ||
| title:"pork", | ||
| serves:4, | ||
| ingredients: ["pork","tomtoes"] | ||
| } | ||
|
|
||
| let steak = { | ||
| title:"steak", | ||
| serves:2, | ||
| ingredients: ["beef","pepper"] | ||
| } | ||
|
|
||
| let lamb = { | ||
| title:"lamb", | ||
| serves:3, | ||
| ingredients: ["lamb","garlic"] | ||
| } | ||
|
|
||
| let fish = { | ||
| title:"fish", | ||
| serves:6, | ||
| ingredients: ["fish","onion"] | ||
| } | ||
| console.log(Object.entries(fish)); | ||
|
|
||
| function printRecipe(obj) { | ||
|
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 really good and it's clear you have a good understanding of accessing object keys & values. The question asks for each recipe to be in this format when console.log is used: Can you think of ways you might change your approach so that it follows this format? (for example not having the title key logged & having the ingredients on a new line) |
||
| for ([key, value] of Object.entries(obj)) { | ||
| console.log(`${key}: ${value}`); | ||
| } | ||
| } | ||
|
|
||
| printRecipe(chicken); | ||
| printRecipe(pork); | ||
| printRecipe(steak); | ||
| printRecipe(lamb); | ||
| printRecipe(fish); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,11 @@ const COUNTRY_CURRENCY_CODES = [ | |
|
|
||
| function createLookup(countryCurrencyCodes) { | ||
| // write code here | ||
| let result = {}; | ||
| for (let [country, currency] of countryCurrencyCodes) { | ||
| result[country] = currency; | ||
| } | ||
| return result; | ||
|
Comment on lines
+22
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. really good, well done :) |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,14 @@ let pantry = { | |
|
|
||
| function createShoppingList(recipe) { | ||
| // write code here | ||
| let result = {}; | ||
| result.name = recipe["name"]; | ||
| let pantryArr = pantry.fridgeContents.concat(pantry.cupboardContents); | ||
|
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. I really like how you use |
||
| let missingArr = recipe["ingredients"].filter( | ||
| (food) => pantryArr.indexOf(food) === -1 | ||
| ); | ||
| result.items = missingArr; | ||
| return result; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
|
|
@@ -43,11 +51,18 @@ test("createShoppingList works for pancakes recipe", () => { | |
| test("createShoppingList works for margherita pizza recipe", () => { | ||
| let recipe2 = { | ||
| name: "margherita pizza", | ||
| ingredients: ["flour", "salt", "yeast", "tinned tomatoes", "oregano", "mozarella"], | ||
| ingredients: [ | ||
| "flour", | ||
| "salt", | ||
| "yeast", | ||
| "tinned tomatoes", | ||
| "oregano", | ||
| "mozarella", | ||
| ], | ||
| }; | ||
|
|
||
| expect(createShoppingList(recipe2)).toEqual({ | ||
| name: "margherita pizza", | ||
| items: ["flour", "yeast", "mozarella"] | ||
| items: ["flour", "yeast", "mozarella"], | ||
| }); | ||
| }); | ||
| }); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,24 @@ const MENU = { | |
|
|
||
| let cashRegister = { | ||
| // write code here | ||
| } | ||
| orderBurger: function (balance) { | ||
| if (balance >= MENU.burger) { | ||
| let newBalance = balance - MENU.burger; | ||
| return newBalance; | ||
| } else { | ||
| return balance; | ||
| } | ||
| }, | ||
|
Comment on lines
+24
to
+31
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. good job here, this is a very clear solution & your variable names make it easy to understand if you are unfamiliar with the code |
||
| 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` | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,10 +25,21 @@ | |
|
|
||
| function countWords(string) { | ||
| const wordCount = {}; | ||
|
|
||
| if (!string || string.trim().length === 0) { | ||
| return wordCount; | ||
| } | ||
| let strArr = string.split(" "); | ||
| strArr.map((word) => { | ||
| if (wordCount[word] === undefined) { | ||
| wordCount[word] = 1; | ||
| } else { | ||
| wordCount[word] = wordCount[word] + 1; | ||
| } | ||
| }); | ||
| // write code here | ||
|
|
||
| return wordCount; | ||
|
|
||
|
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 job making it onto the extra tasks and on having passing tests! A shorthand way of doing |
||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
|
|
@@ -46,17 +57,25 @@ test("Code works for a small string", () => { | |
| }); | ||
|
|
||
| test("A string with, some punctuation", () => { | ||
| expect(countWords("A string with, some punctuation")).toEqual( | ||
| { A: 1, string: 1, "with,": 1, some: 1, punctuation: 1 } | ||
| ); | ||
| expect(countWords("A string with, some punctuation")).toEqual({ | ||
| A: 1, | ||
| string: 1, | ||
| "with,": 1, | ||
| some: 1, | ||
| punctuation: 1, | ||
| }); | ||
| }); | ||
|
|
||
| test("Empty string", () => { | ||
| expect(countWords("")).toEqual({}); | ||
| }); | ||
|
|
||
| test("Example task string", () => { | ||
| expect(countWords("you're braver than you believe, stronger than you seem, and smarter than you think")).toEqual({ | ||
| expect( | ||
| countWords( | ||
| "you're braver than you believe, stronger than you seem, and smarter than you think" | ||
| ) | ||
| ).toEqual({ | ||
| "you're": 1, | ||
| and: 1, | ||
| "believe,": 1, | ||
|
|
||
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, but you have the
myCountryvariable available to you so you can also use this in the bracket notation :)