-
-
Notifications
You must be signed in to change notification settings - Fork 220
Birmingham | 25-ITP-Sept | Khor Biel | Sprint 2 | module-data-groups/sprint-2 #832
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
Changes from all commits
7ef97ad
879dba4
106d47e
6832864
e76a105
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,11 @@ | ||
| function contains() {} | ||
| function contains(obj, key) { | ||
| // Check if the first argument is a valid object | ||
| if (typeof obj !== "object" || obj === null) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check if the key exists in the object (only own properties) | ||
| return Object.prototype.hasOwnProperty.call(obj, key); | ||
| } | ||
|
|
||
| module.exports = contains; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,13 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(countryCurrencyPairs) { | ||
| const lookup = {}; | ||
|
|
||
| for (const pair of countryCurrencyPairs) { | ||
| const countryCode = pair[0]; | ||
| const currencyCode = pair[1]; | ||
| lookup[countryCode] = currencyCode; | ||
| } | ||
|
|
||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,29 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
|
|
||
| if (!queryString || queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| // Remove leading '?' if present | ||
| if (queryString.startsWith("?")) { | ||
| queryString = queryString.substring(1); | ||
| } | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| // Only split on the first '=' to handle values containing '=' | ||
| const equalsIndex = pair.indexOf("="); | ||
| if (equalsIndex === -1) { | ||
| // Handle keys without values | ||
| const key = decodeURIComponent(pair); | ||
| queryParams[key] = ""; | ||
| } else { | ||
| const key = decodeURIComponent(pair.substring(0, equalsIndex)); | ||
| const value = decodeURIComponent(pair.substring(equalsIndex + 1)); | ||
| queryParams[key] = value; | ||
| } | ||
|
Comment on lines
+3
to
+26
Contributor
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 work great! |
||
| } | ||
|
|
||
| return queryParams; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,22 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
|
|
||
| if (!Array.isArray(items)) { | ||
| throw new Error("Input must be an array"); | ||
| } | ||
|
|
||
| const result = Object.create(null); | ||
|
|
||
|
|
||
| for (const item of items) { | ||
| if (result[item]) { | ||
| result[item] += 1; | ||
| } else { | ||
| result[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
|
|
||
| } | ||
|
|
||
| module.exports = tally; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,3 +26,39 @@ | |
|
|
||
| 3. Order the results to find out which word is the most common in the input | ||
| */ | ||
|
|
||
|
|
||
|
|
||
| function countWords(str) { | ||
| // Use Object.create(null) to avoid inherited properties | ||
| const wordCounts = Object.create(null); | ||
|
|
||
| if (!str || str.trim() === "") { | ||
|
Contributor
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. Between |
||
| return wordCounts; | ||
| } | ||
|
|
||
| // Split into words and handle punctuation/case | ||
| const words = str.split(/\s+/); | ||
|
|
||
| for (const word of words) { | ||
| // Remove punctuation and convert to lowercase | ||
| const cleanWord = word | ||
| .toLowerCase() | ||
| .replace(/[.,!?]/g, "") | ||
| .trim(); | ||
|
|
||
| // Skip empty strings | ||
| if (cleanWord === "") continue; | ||
|
|
||
| // Count the word | ||
| if (wordCounts[cleanWord]) { | ||
| wordCounts[cleanWord] += 1; | ||
| } else { | ||
| wordCounts[cleanWord] = 1; | ||
| } | ||
| } | ||
|
|
||
| return wordCounts; | ||
|
Comment on lines
+40
to
+61
Contributor
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. If you do some pre-processing on |
||
| } | ||
|
|
||
| module.exports = countWords; | ||
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.
Could also use array destructuring to shorten the code on lines 4-6 (if you have time).
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.
Yes you're right