-
-
Notifications
You must be signed in to change notification settings - Fork 318
London | 26-ITP-May | Dipa Sarker | Sprint 2 | Sprint-2-exercises #1272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dipa-Sarker
wants to merge
9
commits into
CodeYourFuture:main
Choose a base branch
from
Dipa-Sarker:Sprint-2-exercises
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d5927fb
predicted and fixed address.js
Dipa-Sarker 657acff
predicted and fixed code for author.js
Dipa-Sarker d1f7aea
predicted and fixed code for recipe.js
Dipa-Sarker e2aade9
implemented function and tests for contains
Dipa-Sarker 8a95137
implement function and tests for lookup.js
Dipa-Sarker 0355a09
fixed implementation for querystring.js function
Dipa-Sarker 05ac31a
implemented function and tests for tally.js
Dipa-Sarker c06fbe1
implemented function and tests for tally.js
Dipa-Sarker 252377a
fixed invert function and add test
Dipa-Sarker 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| // Predict and explain first... | ||
|
|
||
| // My prediction is for log out the title, how many it serves and the ingridients we have to use several console.log for each of the property | ||
| // value. Besides, for each ingredient should be logged on a new line, we can use array index for the ingredients array. | ||
| // This program should log out the title, how many it serves and the ingredients. | ||
| // Each ingredient should be logged on a new line | ||
| // How can you fix it? | ||
|
|
@@ -10,6 +11,10 @@ const recipe = { | |
| ingredients: ["olive oil", "tomatoes", "salt", "pepper"], | ||
| }; | ||
|
|
||
| console.log(`${recipe.title} serves ${recipe.serves} | ||
| ingredients: | ||
| ${recipe}`); | ||
|
|
||
| console.log(recipe.title); | ||
| console.log(recipe.serves); | ||
| console.log(recipe.ingredients[0]); | ||
|
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. Is there a way to make this work with any number of ingredients? |
||
| console.log(recipe.ingredients[1]); | ||
| console.log(recipe.ingredients[2]); | ||
| console.log(recipe.ingredients[3]); | ||
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 |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| function contains() {} | ||
| function contains(input, propertyName) { | ||
| const result = Object.hasOwn(input, propertyName); | ||
| return result; | ||
| } | ||
|
|
||
| module.exports = contains; |
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 |
|---|---|---|
| @@ -1,35 +1,42 @@ | ||
| const contains = require("./contains.js"); | ||
|
|
||
| /* | ||
| Implement a function called contains that checks an object contains a | ||
| particular property | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'a') // returns true | ||
| as the object contains a key of 'a' | ||
|
|
||
| E.g. contains({a: 1, b: 2}, 'c') // returns false | ||
| as the object doesn't contains a key of 'c' | ||
| */ | ||
|
|
||
| // Acceptance criteria: | ||
|
|
||
| // Given a contains function | ||
| // When passed an object and a property name | ||
| // Then it should return true if the object contains the property, false otherwise | ||
|
|
||
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("contains on empty object returns false", function () { | ||
| const object = {}; | ||
| const currentOutput = contains(object, "a"); | ||
| const targetOutput = false; | ||
|
|
||
| expect(currentOutput).toBe(targetOutput); | ||
| }); | ||
|
|
||
| test("contains with an existing property name should return true", function () { | ||
| const object = { | ||
| a: 1, | ||
| b: 2, | ||
| flower: "red", | ||
| }; | ||
| const currentOutput = contains(object, "a"); | ||
| const targetOutput = true; | ||
|
|
||
| expect(currentOutput).toBe(targetOutput); | ||
| }); | ||
|
|
||
| test("contains with a non-existing property name should return false", function () { | ||
| const object = { | ||
| a: 1, | ||
| b: 2, | ||
| flower: "red", | ||
| }; | ||
| const currentOutput = contains(object, "c"); | ||
| const targetOutput = false; | ||
|
|
||
| expect(currentOutput).toBe(targetOutput); | ||
| }); | ||
|
|
||
| test("contains with an array should return false", function () { | ||
| const object = ["a", "b", "c"]; | ||
|
|
||
| const currentOutput = contains(object, "c"); | ||
| const targetOutput = false; | ||
|
|
||
| expect(currentOutput).toBe(targetOutput); | ||
| }); |
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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const keyValuePairs = Object.fromEntries(countryCurrencyPairs); | ||
| return keyValuePairs; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
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 |
|---|---|---|
| @@ -1,35 +1,17 @@ | ||
| const createLookup = require("./lookup.js"); | ||
|
|
||
| test.todo("creates a country currency code lookup for multiple codes"); | ||
|
|
||
| /* | ||
|
|
||
| Create a lookup object of key value pairs from an array of code pairs | ||
|
|
||
| Acceptance Criteria: | ||
|
|
||
| Given | ||
| - An array of arrays representing country code and currency code pairs | ||
| e.g. [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| - createLookup function is called with the country-currency array as an argument | ||
|
|
||
| Then | ||
| - It should return an object where: | ||
| - The keys are the country codes | ||
| - The values are the corresponding currency codes | ||
|
|
||
| Example | ||
| Given: [['US', 'USD'], ['CA', 'CAD']] | ||
|
|
||
| When | ||
| createLookup(countryCurrencyPairs) is called | ||
|
|
||
| Then | ||
| It should return: | ||
| { | ||
| 'US': 'USD', | ||
| 'CA': 'CAD' | ||
| } | ||
| */ | ||
| test("creates a country currency code lookup for multiple codes", function () { | ||
| const arr = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ["BD", "BDT"], | ||
| ]; | ||
| const currentOutput = createLookup(arr); | ||
| const targetOutput = { | ||
| US: "USD", | ||
| CA: "CAD", | ||
| BD: "BDT", | ||
| }; | ||
|
|
||
| expect(currentOutput).toEqual(targetOutput); | ||
| }); |
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 |
|---|---|---|
|
|
@@ -4,10 +4,37 @@ function parseQueryString(queryString) { | |
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
| const emptyElement = ""; | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| for (i = 0; i < keyValuePairs.length; i++) { | ||
| const currentPair = keyValuePairs[i]; | ||
| if (currentPair !== emptyElement) { | ||
| const index = currentPair.indexOf("="); | ||
| if (index !== -1) { | ||
| const key = currentPair.slice(0, index); | ||
| const value = currentPair.slice(index + 1); | ||
| const decodeKey = decodeURIComponent(key); | ||
| const decodeValue = decodeURIComponent(value); | ||
| const replacedKey = decodeKey.replace("+", " "); | ||
| const replacedValue = decodeValue.replace("+", " "); | ||
|
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. what happens if you have a value like |
||
| if (queryParams.hasOwnProperty(replacedKey)) { | ||
| if (Array.isArray(queryParams[replacedKey])) { | ||
| queryParams[replacedKey].push(replacedValue); | ||
| } else { | ||
| queryParams[replacedKey] = [ | ||
| queryParams[replacedKey], | ||
| replacedValue, | ||
| ]; | ||
| } | ||
| } else { | ||
| queryParams[replacedKey] = replacedValue; | ||
| } | ||
| } else if (index === -1) { | ||
| const key = currentPair; | ||
| const value = ""; | ||
| queryParams[key] = value; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
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 |
|---|---|---|
| @@ -1,3 +1,21 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (array.length === 0) { | ||
| return {}; | ||
| } | ||
| if (typeof array === "string") { | ||
| throw new Error(); | ||
| } | ||
| const object = {}; | ||
| for (i = 0; i < array.length; i++) { | ||
| const currentItem = array[i]; | ||
| if (object.hasOwnProperty(currentItem)) { | ||
| object[currentItem] += 1; | ||
| } else { | ||
| object[currentItem] = 1; | ||
| } | ||
| } | ||
|
|
||
| return object; | ||
| } | ||
|
|
||
| module.exports = tally; |
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 |
|---|---|---|
| @@ -1,34 +1,18 @@ | ||
| const tally = require("./tally.js"); | ||
|
|
||
| /** | ||
| * tally array | ||
| * | ||
| * In this task, you'll need to implement a function called tally | ||
| * that will take a list of items and count the frequency of each item | ||
| * in an array | ||
| * | ||
| * For example: | ||
| * | ||
| * tally(['a']), target output: { a: 1 } | ||
| * tally(['a', 'a', 'a']), target output: { a: 3 } | ||
| * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } | ||
| */ | ||
| test("should return an empty object when given an empty array", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
|
|
||
| // Acceptance criteria: | ||
| test("should return counts for each unique item when given an array with one item and with duplicate items", () => { | ||
| expect(tally(["a"])).toEqual({ a: 1 }); | ||
| expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
| }); | ||
|
|
||
| // Given a function called tally | ||
| // When passed an array of items | ||
| // Then it should return an object containing the count for each unique item | ||
|
|
||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| test.todo("tally on an empty array returns an empty object"); | ||
|
|
||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
|
|
||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| test("should throw an error when given an invalid input", () => { | ||
| function invalidInput() { | ||
| tally("string"); | ||
| } | ||
| expect(invalidInput).toThrow(Error); | ||
| }); |
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| const invert = require("./invert.js"); | ||
|
|
||
| test("should return swaped keys and values for an given object", () => { | ||
| expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b"}); | ||
| }); |
Oops, something went wrong.
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.
Is there a way to program this for any / all keys without listing them individually?