-
-
Notifications
You must be signed in to change notification settings - Fork 327
London | 26-ITP-May | Edina Kurdi | Sprint 2 | Coursework #1422
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
edinakurdi
wants to merge
26
commits into
CodeYourFuture:main
Choose a base branch
from
edinakurdi:coursework/sprint-2
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
26 commits
Select commit
Hold shift + click to select a range
6a0643a
explain and fix the bug in address
edinakurdi 6de1246
debug logging the values of an object
edinakurdi 2488a71
format recipe output to log ingredients on new lines
edinakurdi 0291dfa
implement contains function
edinakurdi bced4fe
write tests for contains function
edinakurdi 5866558
implement createLookup()
edinakurdi 3423b50
create tests for createLookup()
edinakurdi 2a62f58
implement tally function
edinakurdi 0b6fc65
write tests for tally function
edinakurdi 9c07c98
implement queryString()
edinakurdi 95e7d98
add one extra test
edinakurdi 4de66ef
implement function and answer Qs in the comments
edinakurdi 078648c
add export for testing
edinakurdi 6d32545
create test file and add first test
edinakurdi 1e8685b
add another test
edinakurdi c0107ff
implement an alternative solution to return the values from the autho…
edinakurdi a6c0620
shorten code
edinakurdi 0b68a26
shorten code more
edinakurdi 4a89c7d
simplify code
edinakurdi 8ef6662
add a simpler test for valid input to start with
edinakurdi 0ac7c8a
fix bracket that broke the test
edinakurdi f7b2d6f
break down a test to 3 component tests
edinakurdi c83a6e2
adjust function to filter out any non-array elements
edinakurdi cbed42f
add aitional tests to check if input is not an array
edinakurdi c02c23e
update keyword for variable declaration (tallySet
edinakurdi b46cc1c
update keyword for variable declaration (filteredValuePairs
edinakurdi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,20 @@ | ||
| function contains() {} | ||
| function contains(object, propertyName) { | ||
| if (typeof object !== "object" || object === null || Array.isArray(object)) { | ||
| throw new Error("Input should be an object"); | ||
| } | ||
| const keysInObject = Object.keys(object); | ||
| return keysInObject.includes(propertyName); | ||
| } | ||
|
|
||
| module.exports = contains; | ||
|
|
||
| /* | ||
| 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' | ||
| */ |
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,52 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| //if countryCurrencyPairs is not an array, throw error | ||
| if (!Array.isArray(countryCurrencyPairs)) { | ||
| throw new Error("Invalid input. It should be an array"); | ||
| } | ||
| //if it is an empty array, throw error | ||
| if (countryCurrencyPairs.length === 0) { | ||
| throw new Error("Input should not be an empty array"); | ||
| } | ||
|
|
||
| //if not all elements are an array in the array, throw an error | ||
| if (!countryCurrencyPairs.every(Array.isArray)) { | ||
| throw new Error("Invalid input. All elements should be arrays"); | ||
| } | ||
|
|
||
| return Object.fromEntries(countryCurrencyPairs); | ||
|
Poonam-raj marked this conversation as resolved.
|
||
| } | ||
|
|
||
| module.exports = createLookup; | ||
|
|
||
| // console.log( | ||
| // createLookup([ | ||
| // ["US", "USD"], | ||
| // ["CA", "CAD"], | ||
| // ]) | ||
| // ); | ||
|
|
||
| // console.log(createLookup([])); | ||
|
|
||
| /* | ||
| 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' | ||
| } | ||
|
|
||
| */ | ||
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 |
|---|---|---|
| @@ -1,3 +1,18 @@ | ||
| function tally() {} | ||
| function tally(array) { | ||
| if (!Array.isArray(array)) { | ||
| throw new Error("Input should be an array"); | ||
| } | ||
|
|
||
| const tallySet = {}; | ||
|
|
||
| for (let item of array) { | ||
| if (!tallySet[item]) { | ||
| tallySet[item] = 1; | ||
| } else { | ||
| tallySet[item] += 1; | ||
| } | ||
| } | ||
| return tallySet; | ||
| } | ||
|
|
||
| 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 |
|---|---|---|
|
|
@@ -19,16 +19,46 @@ const tally = require("./tally.js"); | |
| // Given a function called tally | ||
| // When passed an array of items | ||
| // Then it should return an object containing the count for each unique item | ||
| describe("tally()", () => { | ||
| // Given an array with duplicate items | ||
| // When passed to tally | ||
| // Then it should return counts for each unique item | ||
| describe("when given an array with a single item", () => { | ||
| test("should return an object with counts for that item (1)", () => { | ||
| expect(tally(["a"])).toEqual({ a: 1 }); | ||
| }); | ||
| }); | ||
| describe("when given an array with duplicate items", () => { | ||
| test("should return an object with counts that item", () => { | ||
| expect(tally(["a", "a", "a"])).toEqual({ a: 3 }); | ||
| }); | ||
| }); | ||
| describe("when given an array with duplicate items", () => { | ||
|
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. Avoid having two describe blocks with the same text (this is the same as line 31) |
||
| test("should return an object with counts for each unique item", () => { | ||
| expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); | ||
|
Poonam-raj marked this conversation as resolved.
|
||
| }); | ||
| }); | ||
| // Given an empty array | ||
| // When passed to tally | ||
| // Then it should return an empty object | ||
| describe("when given an empty array", () => { | ||
| test("should return an empty object", () => { | ||
| expect(tally([])).toEqual({}); | ||
| }); | ||
| }); | ||
|
|
||
| // 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 | ||
| // Given an invalid input like a string | ||
| // When passed to tally | ||
| // Then it should throw an error | ||
| describe("when given invalid input", () => { | ||
| test("should throw an error for a string input", () => { | ||
| expect(() => tally("apple")).toThrow("Input should be an array"); | ||
| }); | ||
| test("should throw an error for a boolean input", () => { | ||
| expect(() => tally(true)).toThrow("Input should be an array"); | ||
| }); | ||
| test("should throw an error for a number input", () => { | ||
| expect(() => tally(3)).toThrow("Input should be an array"); | ||
| }); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
A good workaround to print the values of the object.
Question - how could the for loop on lines 21 - 23 be changed (to a different loop for example) to achieve the same effect?
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.
Created an alternative using a loop
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.
Good refactor- I was also thinking of a
for inloop, if we changed line 21 to afor inyou'd have a direct way of printing the values in a readable way