generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 271
Birmingham | ITP-Jan | Roman Sanaye | Sprint 2 | Exercises #1019
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
RomanSanaye
wants to merge
11
commits into
CodeYourFuture:main
Choose a base branch
from
RomanSanaye: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
11 commits
Select commit
Hold shift + click to select a range
15a213d
files have been debugged in debug folder of sprint-2
RomanSanaye ad212d7
Jest test added and Functions were fixed
RomanSanaye ef27922
inverted function and jest test implemented
RomanSanaye df349cf
test and functions implemented for stretch files
RomanSanaye 4b5aff7
Nothing special
RomanSanaye 748465b
recipe.js has been refactored
RomanSanaye 40215f8
Jest tests and contains function updated
RomanSanaye 17557e2
replaced let with const in createLookup function
RomanSanaye f5ad08e
tally function and jest tests updated
RomanSanaye 05d578c
count-words and mode function have been refactored and updated
RomanSanaye a76c37b
Add test to ensure arrays are rejected in contains function
RomanSanaye 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() {} | ||
| // start your function | ||
|
|
||
| function contains(object, key) { | ||
| // Reject arrays, null, or non-object types | ||
| if (object === null || typeof object !== "object" || Array.isArray(object)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Safely check if the object has the property | ||
| return Object.prototype.hasOwnProperty.call(object, key); | ||
| } | ||
|
|
||
| let key = "name"; | ||
| const object = { | ||
| name: "Roman", | ||
| age: 33, | ||
| city: "Birmingham", | ||
| }; | ||
|
|
||
| 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,15 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| // start your function; | ||
| function createLookup(input) { | ||
| const output = {}; | ||
| for (let pair of input) { | ||
| const key = pair[0]; | ||
| const value = pair[1]; | ||
| output[key] = value; | ||
| } | ||
| return output; | ||
| } | ||
|
|
||
| let input = [ | ||
| ["US", "USD"], | ||
| ["CA", "CAD"], | ||
| ]; | ||
| 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,24 @@ | ||
| // start your function; | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| // URLSearchParams is a built-in JavaScript object that can read query strings. | ||
| const params = new URLSearchParams(queryString); // params stores the query string internally as key-value pairs. | ||
| for (let pair of params) { | ||
| let key = pair[0]; | ||
| let value = pair[1]; | ||
| queryParams[key] = value.replace(/ /g, "+"); | ||
cjyuan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| return queryParams; | ||
| } | ||
| let queryString = "equation=x=y+1"; | ||
|
|
||
| module.exports = parseQueryString; | ||
|
|
||
| // Notice: ===> URLSearchParams is a class, not a plain function. | ||
| // Classes in JS must be called with new. | ||
| // new URLSearchParams(...) → creates a new instance (object) of the class. | ||
| // That instance knows how to store and iterate key-value pairs. | ||
| // URLSearchParams decodes URL-encoded characters, and in query strings, a + represents a space. and this is why we need .replace(/ /g, "+"); | ||
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,15 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) throw new Error("Invalid input"); | ||
|
|
||
| const output = {}; | ||
| for (const item of arr) { | ||
| if (output[item]) { | ||
| output[item] += 1; // increment count if already exists | ||
| } else { | ||
| output[item] = 1; // first occurrence | ||
| } | ||
| } | ||
| return output; | ||
| } | ||
| let arr = ["a", "b"]; | ||
| 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
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,13 @@ | ||
| const invert = require("./invert.js"); | ||
| // testing time | ||
| test("Empty object returns empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("Single key-value pair", () => { | ||
| expect(invert({ a: 1 })).toEqual({ 1: "a" }); | ||
| }); | ||
|
|
||
| test("Given an object, then it should swap the keys and values in the object", () => { | ||
| expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); | ||
| }); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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.
In practice, we prepare tests not just to check our own implementation, but also to guard against future modification.
Currently, this incorrectly implemented function could also pass this test:
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.
Thanks for the feedback! I’ve added a test to ensure arrays are rejected even when the key exists, so incorrect implementations will now fail.