This repository was archived by the owner on Jan 14, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 264
London8 Daniel Piga Core2 week1 #3
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| */ | ||
|
|
||
| let student = { | ||
| getName:(name)=>console.log("Student name: " +name) | ||
| // write code here | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,16 +10,21 @@ | |
|
|
||
| The createShoppingList function should return an object with two properties: | ||
| - "name" of the recipe, which is a string, | ||
| - "items", which is an arry of the missing ingredients that need to be on the shopping list | ||
| - "items", which is an array of the missing ingredients that need to be on the shopping list | ||
| */ | ||
|
|
||
| let pantry = { | ||
| fridgeContents: ["butter", "milk"], | ||
| cupboardContents: ["salt", "tinned tomatoes", "oregano"], | ||
| }; | ||
|
|
||
| function createShoppingList(recipe) { | ||
| // write code here | ||
| function createShoppingList({ name, ingredients }) { | ||
| const { cupboardContents,fridgeContents} = pantry; | ||
|
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. 👍 |
||
| const myItems = [...cupboardContents, ...fridgeContents].reduce( | ||
| (acc, item) => ({ ...acc, [item]: 1 }), | ||
| {} | ||
| ); | ||
| return { name, items: ingredients.filter((item) => !myItems[item]) }; | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
|
|
@@ -43,11 +48,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"], | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,9 +19,20 @@ const MENU = { | |
| falafel: 7.25, | ||
| }; | ||
|
|
||
| const order = (price) => (balance) => | ||
|
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. Very good - functions that produce functions is an important mental shift. That's a very powerful technique. Don't overuse it though :) |
||
| balance >= price ? balance - price : balance; | ||
|
|
||
| let cashRegister = { | ||
| // write code here | ||
| } | ||
| orderBurger: order(MENU.burger), | ||
| orderFalafel: order(MENU.falafel), | ||
| }; | ||
|
|
||
| // let cashRegister = { | ||
| // orderBurger: (balance) => | ||
| // balance >= MENU.burger ? balance - MENU.burger : balance, | ||
| // orderFalafel: (balance) => | ||
| // balance >= MENU.falafel ? balance - MENU.falafel : balance, | ||
| // }; | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
| - To run the tests for this exercise, run `npm test -- --testPathPattern 4-restaurant.js` | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,11 +24,11 @@ | |
| */ | ||
|
|
||
| function countWords(string) { | ||
| const wordCount = {}; | ||
|
|
||
| // write code here | ||
|
|
||
| return wordCount; | ||
| return (string ? string.split(" ") : []).reduce( | ||
|
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 a decent implementation and you used quite a few tricks here. However, I ague that you need to look for simpler (more readable) solutions: function countWords(string) {
const wordCount = {};
const words = string ? string.split(" ") : []
for (const word of words) {
wordCount[word] = 1 + (wordCount[word] || 0)
}
return wordCount
} |
||
| (acc, item) => ({ ...acc, [item]: acc[item] ? ++acc[item] : 1 }), | ||
| {} | ||
| ); | ||
| // string.split() | ||
| } | ||
|
|
||
| /* ======= TESTS - DO NOT MODIFY ===== | ||
|
|
@@ -46,17 +46,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, | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You use
recipes[recipe]multiple times. I usually suggest introducing a variable to reduce code duplication. But in this case - wouldfor-ofloop work better?