-
-
Notifications
You must be signed in to change notification settings - Fork 326
London | 26-May-ITP | Yonatan Teklemariam | Sprint 2 | exercises #1237
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
Yonatanteklemariam
wants to merge
11
commits into
CodeYourFuture:main
Choose a base branch
from
Yonatanteklemariam: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
11 commits
Select commit
Hold shift + click to select a range
28a696f
reverted to original
Yonatanteklemariam cff19af
reverted to original
Yonatanteklemariam 7f760a0
debugged the three functions and fixed them
Yonatanteklemariam 7ed6911
added some comments for highlighting purposes.
Yonatanteklemariam 6ba6fdc
implemented the function and wrote the tests, re-run the test to conf…
Yonatanteklemariam 0c16c1d
implemented the functions and added some jest test for the functions
Yonatanteklemariam 5ebaa3d
implemented the tally fiunction and added some jest test for the func…
Yonatanteklemariam 0960929
implemented the key, value conversion and written jest tests all passed
Yonatanteklemariam 4ee8959
Comment out the redundant test
Yonatanteklemariam 4c91954
Merge branch 'main' into Sprint-2
Yonatanteklemariam 871fc49
fixed the empty array validator
Yonatanteklemariam 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,10 @@ | ||
| function contains() {} | ||
| function contains(obj, key) { | ||
| for (const currentKey of Object.keys(obj)) { | ||
| if (currentKey === key) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| 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
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,18 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countrycurrency) { | ||
| const lookup = {}; // lookup object is created empty to store the new keys and values mapped from the function | ||
| countrycurrency.map(function ([country, currency]) { | ||
| // maps every keys and values passed as argument in countrycurrency and stores to the variable names of country and currency | ||
| console.log(`${country}: ${currency}`); | ||
| lookup[country] = currency; // this lookup object is then used to store the already mapped keys and values of the countrycurrency | ||
| }); | ||
| return lookup; | ||
| } | ||
| console.log( | ||
| createLookup([ | ||
| ["US", "USD"], | ||
| ["UAE", "AED"], | ||
| ["ERI", "ERN"], | ||
| ]) | ||
| ); | ||
|
|
||
| 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
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,16 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) throw new TypeError("Input must be an array"); | ||
| const counts = Object.create(null); | ||
| if (arr.length === 0) { | ||
| return counts; | ||
| } | ||
| for (const item of arr) { | ||
| if (typeof item !== "string" || !/^[A-Za-z0-9]+$/.test(item)) { | ||
| throw new Error("Please Enter Valid Input!"); | ||
| } | ||
| counts[item] = (counts[item] ?? 0) + 1; | ||
| } | ||
| return counts; | ||
| } | ||
|
|
||
| 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
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 |
|---|---|---|
|
|
@@ -7,23 +7,50 @@ | |
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| function invert(obj) { | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| // Validate input type | ||
| if (!obj || typeof obj !== "object" || Array.isArray(obj)) { | ||
| throw new Error("Input must be an object"); | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
| const entries = Object.entries(obj); | ||
| if (entries.length === 0) return {}; | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| // take only the values | ||
| const values = entries.map(([_, v]) => v); | ||
|
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. Instead of getting the entries, then mapping for the values, is there a faster route to getting the values? |
||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // Detect duplicate values | ||
| const hasDuplicate = new Set(values).size !== values.length; | ||
| if (hasDuplicate) { | ||
| throw new Error("Values cannot be duplicated"); | ||
| } | ||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| const inverted = {}; | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
| for (const [key, value] of entries) { | ||
| // Validate key and value types | ||
| if ( | ||
| (typeof key !== "string" && typeof key !== "number") || | ||
| (typeof value !== "string" && typeof value !== "number") | ||
| ) { | ||
| throw new Error("Keys and values must be a string or a number"); | ||
| } | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
| inverted[value] = key; | ||
| } | ||
|
|
||
| return inverted; | ||
| } | ||
| module.exports = invert; | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| // a) What is the current return value when invert is called with { a : 1 } | ||
| // the current value is 1 | ||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
| // the current return value at this call is 2 | ||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| // the target return value at this call is to supposed to be 'b' | ||
| // d) What does Object.entries return? Why is it needed in this program? | ||
| // Object.entries is used because both key and value are needed to be returned from the loop | ||
| // e) Explain why the current return value is different from the target output | ||
| // it's because the code in the line 13 is only returning single literal property named "key" on every loop iteration, so each assignment overwrites the previous one and the final object ends up with { key: 2 } but the intended inverted output requires using the loop variables as property names (dynamic keys) so each iteration creates a distinct property (for example invertedObj[value] = key), producing separate entries like { 1: 'a', 2: 'b' } instead of repeatedly writing to the same "key" property which the current function is doing | ||
| // f) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| // Above is the fixed function | ||
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,59 @@ | ||
| const invert = require("./invert"); | ||
|
|
||
| test("inverts a simple object", () => { | ||
| const input = { a: "x", b: "y" }; | ||
| const expected = { x: "a", y: "b" }; | ||
| expect(invert(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| test("inverts an object with number values", () => { | ||
| const input = { a: 1, b: 2 }; | ||
| const expected = { 1: "a", 2: "b" }; | ||
| expect(invert(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| test("accepts empty string as a key", () => { | ||
| const input = { "": "x" }; | ||
| const expected = { x: "" }; | ||
| expect(invert(input)).toEqual(expected); | ||
| }); | ||
|
|
||
| test("returns an empty object when input is empty", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("throws when input is null", () => { | ||
| expect(() => invert(null)).toThrow("Input must be an object"); | ||
| }); | ||
|
|
||
| test("throws when input is not an object", () => { | ||
| expect(() => invert("hello")).toThrow("Input must be an object"); | ||
| expect(() => invert(123)).toThrow("Input must be an object"); | ||
| expect(() => invert(true)).toThrow("Input must be an object"); | ||
| }); | ||
|
|
||
| test("throws when input is an array", () => { | ||
| expect(() => invert(["a", "b"])).toThrow("Input must be an object"); | ||
| }); | ||
|
|
||
| test("throws when a value is not a string or number", () => { | ||
| const obj = { a: {} }; | ||
| expect(() => invert(obj)).toThrow( | ||
| "Keys and values must be a string or a number" | ||
| ); | ||
| }); | ||
|
|
||
| test("throws when values are duplicated (string)", () => { | ||
| const obj = { a: "x", b: "y", c: "x" }; | ||
| expect(() => invert(obj)).toThrow("Values cannot be duplicated"); | ||
| }); | ||
|
|
||
| test("throws when numeric values are duplicated", () => { | ||
| const obj = { a: 1, b: 2, c: 1 }; | ||
| expect(() => invert(obj)).toThrow("Values cannot be duplicated"); | ||
| }); | ||
|
|
||
| test("duplicate keys are overwritten by JavaScript before reaching invert()", () => { | ||
| const obj = { a: "x", a: "y" }; // JS overwrites first 'a' | ||
| expect(invert(obj)).toEqual({ y: "a" }); | ||
| }); |
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.
Can you explain how ?? works here?