-
-
Notifications
You must be signed in to change notification settings - Fork 280
West Midlands | 26-Jan-ITP | Fida Ali Zada | Sprint 2 | Data Group #985
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
b4cca09
5187f64
67cf526
28a76d8
b2973a6
983f892
00e4862
6e5c048
629a5b3
8efa5ba
06759cb
4dadf43
f5c06bd
57a602e
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,13 @@ | ||
| function contains() {} | ||
| function contains(obj, x) { | ||
| for (const key in obj) { | ||
| if (key == x) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| module.exports = contains; | ||
|
|
||
| // Object.hasOwn(obj, key) | ||
| // This ensures the property belongs directly to the object, not its prototype. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(arr) { | ||
| return Object.fromEntries(arr); | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,34 @@ | ||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| } | ||
|
|
||
| if (!queryString) return queryParams; | ||
|
|
||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| // skip empty pairs (e.g. &&) | ||
| if (pair === "") continue; | ||
|
|
||
| const index = pair.indexOf("="); | ||
|
|
||
| let key, value; | ||
|
|
||
| if (key === -1) { | ||
| key = pair; | ||
| value = ""; | ||
| } else { | ||
| key = pair.substring(0, index); | ||
| value = pair.substring(index + 1); | ||
| } | ||
|
|
||
| // decoding URL | ||
| try { | ||
| key = decodeURIComponent(key); | ||
| value = decodeURIComponent(value); | ||
| } catch (error) { | ||
|
|
||
| } | ||
|
|
||
| queryParams[key] = value; | ||
| } | ||
|
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. For each of the following function calls, does your function return the value you expect? Note:
Author
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. `function parseQueryString(queryString) { if (!queryString) return queryParams; const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { } return queryParams; |
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,25 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("The input should be an array!"); | ||
| } | ||
|
|
||
| const countObj = Object.create(null); | ||
|
|
||
| for (const item of arr) { | ||
|
|
||
| if (countObj[item]) { | ||
| countObj[item] = countObj[item] + 1; | ||
| } else { | ||
| countObj[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| return countObj; | ||
| } | ||
|
|
||
| module.exports = tally; | ||
|
|
||
| // console.log(tally(['a', 'a', 'a', 'a', 'a', 'b', 'c'])); | ||
|
|
||
|
|
||
| // tally(['a', 'a', 'a', 'a', 'a', 'b', 'c']), target output: { a : 5, b: 1, c: 1 } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,3 +26,26 @@ | |
|
|
||
| 3. Order the results to find out which word is the most common in the input | ||
| */ | ||
|
|
||
| function countWords(str) { | ||
| const arr = str.replace(/[^a-zA-Z0-9\s]/g, '').toLowerCase().split(" ") | ||
|
|
||
| let countObj = {}; | ||
|
|
||
| for (const item of arr) { | ||
|
|
||
| if (countObj[item]) { | ||
| countObj[item] = countObj[item] + 1 | ||
| } else { | ||
| countObj[item] = 1; | ||
| } | ||
| } | ||
|
|
||
| // `Object.entries()` converts object to array | ||
| const sortedArray = Object.entries(countObj).sort((a, b) => b[1] - a[1]); | ||
|
|
||
| // `Object.fromEntries()` coverts the sorted array to object | ||
| return Object.fromEntries(sortedArray); | ||
| } | ||
|
|
||
| console.log(countWords("you? and! me, and you. me me me me hello Me")); | ||
|
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 your function return what you expect in the following function calls? Note: The spec is not clear about exactly what to expect from these function calls. This is just for self-check. |
||
Uh oh!
There was an error while loading. Please reload this page.
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.
Does your function return the value you expect from the following function calls?
contains(["a", "b", "c"], "1")contains("abc", "1")contains(null, "1")contains(undefined, "1")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.
Now, it handles correctly.