-
-
Notifications
You must be signed in to change notification settings - Fork 280
Sheffield | 26-ITP-Jan | Seti Mussa | Sprint 2 | Data Group #1172
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
base: main
Are you sure you want to change the base?
Changes from all commits
edfc2fb
1a35459
9786cba
8bf4cdc
253ebd0
0853394
6ea7608
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,9 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
| if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { | ||
| return false; | ||
| } | ||
|
|
||
| return Object.prototype.hasOwnProperty.call(obj, prop); | ||
|
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. Can also explore |
||
| } | ||
|
|
||
| module.exports = contains; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,26 @@ as the object doesn't contains a key of 'c' | |
| // Given an empty object | ||
| // When passed to contains | ||
| // Then it should return false | ||
| test.todo("contains on empty object returns false"); | ||
| test("contains on empty object returns false", () => { | ||
| expect(contains({}, "a")).toBe(false); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with an existing property name | ||
| // Then it should return true | ||
| test("returns true when property exists", () => { | ||
| expect(contains({ a: 1, b: 2 }, "a")).toBe(true); | ||
| }); | ||
|
|
||
| // Given an object with properties | ||
| // When passed to contains with a non-existent property name | ||
| // Then it should return false | ||
|
|
||
| test("return false when property does not exit", () => { | ||
| expect(contains({ a: 1, b: 2 }, "c")).to(fasle); | ||
| }); | ||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test("return false for invalid input like an array", () => { | ||
| expect(contains([], "a")).toBe(false); | ||
| }); | ||
|
Comment on lines
40
to
+45
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 test does not yet confirm that the function correctly returns false when the first argument is an array. Arrays are objects, with their indices acting as keys. A proper test should use a non-empty array along with a valid key to ensure the function returns |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,14 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| if (!Array.isArray(pairs)) return {}; | ||
| const lookup = {}; | ||
|
|
||
| for (const pair of pairs) { | ||
| if (!Array.isArray(pair) || pair.length !== 2) continue; | ||
|
|
||
| const [country, currency] = pair; | ||
| lookup[country] = currency; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,20 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| if (typeof queryString !== "string" || queryString.length === 0) { | ||
| return {}; | ||
| } | ||
|
|
||
| const queryParams = {}; | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| if (!pair) continue; | ||
|
|
||
| const index = pair.indexOf("="); | ||
| if (index === -1) continue; | ||
|
Comment on lines
+12
to
+13
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. FYI: No change required. |
||
|
|
||
| const key = pair.slice(0, index); | ||
| const value = pair.slice(index + 1); | ||
|
|
||
| queryParams[key] = value; | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,15 @@ | ||
| function tally() {} | ||
| function tally(items) { | ||
| if (!Array.isArray(items)) { | ||
| throw new Error("Expected an array"); | ||
| } | ||
|
|
||
| const counts = {}; | ||
|
|
||
| for (const item of items) { | ||
| counts[item] = (counts[item] || 0) + 1; | ||
| } | ||
|
Comment on lines
+5
to
+10
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. Does the following function call return the value you expect? Suggestion: Look up an approach to create an empty object with no inherited properties. |
||
|
|
||
| return counts; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,24 @@ | ||
| /* | ||
| Count the number of times a word appears in a given string. | ||
|
|
||
| Write a function called countWords that | ||
| - takes a string as an argument | ||
| - returns an object where | ||
| - the keys are the words from the string and | ||
| - the values are the number of times the word appears in the string | ||
|
|
||
| Example | ||
| If we call countWords like this: | ||
|
|
||
| countWords("you and me and you") then the target output is { you: 2, and: 2, me: 1 } | ||
|
|
||
| To complete this exercise you should understand | ||
| - Strings and string manipulation | ||
| - Loops | ||
| - Comparison inside if statements | ||
| - Setting values on an object | ||
|
|
||
| ## Advanced challenges | ||
|
|
||
| 1. Remove all of the punctuation (e.g. ".", ",", "!", "?") to tidy up the results | ||
|
|
||
| 2. Ignore the case of the words to find more unique words. e.g. (A === a, Hello === hello) | ||
|
|
||
| 3. Order the results to find out which word is the most common in the input | ||
| */ | ||
| function countWords(str) { | ||
| // handle empty or invalid input | ||
| if (typeof str !== "string") { | ||
| throw new Error("Expected a string"); | ||
| } | ||
|
|
||
| // 1. clean string (remove punctuation + lowercase) | ||
| const cleanStr = str.replace(/[.,!?]/g, "").toLowerCase(); | ||
|
|
||
| // 2. split into words | ||
| const words = cleanStr.split(" "); | ||
|
|
||
| // 3. count words | ||
| const result = {}; | ||
| for (const word of words) { | ||
| if (word !== "") { | ||
| result[word] = (result[word] || 0) + 1; | ||
| } | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| 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.
Why place two extra space in the separator?