-
-
Notifications
You must be signed in to change notification settings - Fork 327
Cape Town | 26-ITP-May| Enice Mutanda| Sprint 2| Data objects #1419
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
Enice-Codes
wants to merge
5
commits into
CodeYourFuture:main
Choose a base branch
from
Enice-Codes:Data-Objects
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
5 commits
Select commit
Hold shift + click to select a range
22d78b2
Complete all Sprint 1 tasks: dedupe, sum, max, median, includes
f4a3df7
all tasks modified
2a4905f
fixed the logic of some excercise to pass the test
bccf5d7
fix: return expression directly instead of storing in unused constant
05c6ac4
made corrections to file
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,6 @@ | ||
| function contains() {} | ||
| function contains(obj, prop) { | ||
| if (typeof obj !== "object" || obj === null) return false; | ||
| return Object.prototype.hasOwnProperty.call(obj, prop); | ||
| } | ||
|
|
||
| module.exports = contains; | ||
| 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,9 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(pairs) { | ||
| const lookup = {}; | ||
| for (const [countryCode, currencyCode] of pairs) { | ||
| lookup[countryCode] = currencyCode; | ||
| } | ||
| return lookup; | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| 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 @@ | ||
| function decodeComponent(str) { | ||
| return decodeURIComponent(str.replaceAll("+", " ")); | ||
| } | ||
|
|
||
| function parseQueryString(queryString) { | ||
| const queryParams = {}; | ||
| if (queryString.length === 0) { | ||
| return queryParams; | ||
| return {}; | ||
| } | ||
| const keyValuePairs = queryString.split("&"); | ||
|
|
||
| for (const pair of keyValuePairs) { | ||
| const [key, value] = pair.split("="); | ||
| queryParams[key] = value; | ||
| const params = {}; | ||
| const pairs = queryString.split("&").filter((pair) => pair !== ""); | ||
|
|
||
| for (const pair of pairs) { | ||
| const [rawKey, ...rawValueParts] = pair.split("="); | ||
| const key = decodeComponent(rawKey); | ||
| const value = decodeComponent(rawValueParts.join("=")); | ||
|
|
||
| params[key] = key in params ? [].concat(params[key], value) : value; | ||
| } | ||
|
|
||
| return queryParams; | ||
| return params; | ||
| } | ||
|
|
||
| module.exports = parseQueryString; | ||
| module.exports = parseQueryString; |
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,13 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (!Array.isArray(arr)) { | ||
| throw new Error("tally expects an array"); | ||
| } | ||
|
|
||
| module.exports = tally; | ||
| const counts = {}; | ||
| for (const item of arr) { | ||
| 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
|
Poonam-raj marked this conversation as resolved.
|
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,29 +1,60 @@ | ||
| // Let's define how invert should work | ||
|
|
||
| // inverse typically means to reverse the key and value in an object. | ||
| // Given an object | ||
| // When invert is passed this object | ||
| // Then it should swap the keys and values in the object | ||
|
|
||
| // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
|
||
| // The original buggy implementation looked like this: | ||
|
|
||
| // function invert(obj) { | ||
| // const invertedObj = {}; | ||
| // for (const [key, value] of Object.entries(obj)) { | ||
| // invertedObj.key = value; | ||
| // } | ||
| // return invertedObj; | ||
| // } | ||
|
|
||
| // a) What is the current return value when invert is called with { a: 1 }? | ||
| // -> { key: 1 } | ||
| // Only invertedObj.key = value runs, setting a property literally | ||
| // named "key" to 1. There is no swap line, so no "1": "a" pair | ||
| // is ever added. | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 }? | ||
| // -> { key: 2 } | ||
| // Each loop iteration overwrites the same literal "key" property. | ||
| // First a: key becomes 1, then b: key gets overwritten to 2. | ||
| // No other properties are ever added, since there's no line that | ||
| // writes to invertedObj[value]. | ||
|
|
||
| // c) What is the target return value when invert is called with { a: 1, b: 2 }? | ||
| // -> { "1": "a", "2": "b" } | ||
|
|
||
| // c) What does Object.entries return, and why is it needed? | ||
| // -> Object.entries(obj) returns an array of [key, value] pairs, e.g. | ||
| // [["a", 1], ["b", 2]]. It's needed because a for...of loop can't | ||
| // iterate directly over an object's properties - Object.entries | ||
| // converts the object into something iterable, and array | ||
| // destructuring ([key, value]) lets us pull out both parts at once. | ||
|
|
||
| // d) Why is the current return value different from the target output? | ||
| // -> Two problems: (1) invertedObj.key uses dot notation with the | ||
| // literal word "key", so it always writes to a property called | ||
| // "key" rather than using the loop variable's value. (2) The line | ||
| // that would actually perform the inversion - invertedObj[value] = key - | ||
| // is missing entirely, so the object is never built up with the | ||
| // swapped key/value pairs. | ||
|
|
||
| // e) Fix: replace invertedObj.key = value with invertedObj[value] = key, | ||
| // using bracket notation so the loop variable's value is used as the | ||
| // property name. See invert.test.js for tests proving the fix works. | ||
| function invert(obj) { | ||
| const invertedObj = {}; | ||
|
|
||
| for (const [key, value] of Object.entries(obj)) { | ||
| invertedObj.key = value; | ||
| invertedObj[value] = key; | ||
| } | ||
|
|
||
| return invertedObj; | ||
| } | ||
|
|
||
| // a) What is the current return value when invert is called with { a : 1 } | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
|
|
||
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
|
|
||
| // c) What does Object.entries return? Why is it needed in this program? | ||
|
|
||
| // d) Explain why the current return value is different from the target output | ||
|
|
||
| // e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
| module.exports = invert; |
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,17 @@ | ||
| const invert = require("./invert"); | ||
|
|
||
| test("invert swaps keys and values in an object", () => { | ||
| const input = { a: 1, b: 2 }; | ||
| const expectedOutput = { "1": "a", "2": "b" }; | ||
| expect(invert(input)).toEqual(expectedOutput); | ||
| }); | ||
|
|
||
| test("invert returns an empty object when given an empty object", () => { | ||
| expect(invert({})).toEqual({}); | ||
| }); | ||
|
|
||
| test("invert works with string values", () => { | ||
| const input = { x: "10", y: "20" }; | ||
| const expectedOutput = { "10": "x", "20": "y" }; | ||
| expect(invert(input)).toEqual(expectedOutput); | ||
| }); |
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,28 +1,14 @@ | ||
| /* | ||
| 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) { | ||
| const wordCount = {}; | ||
| const words = str | ||
| .toLowerCase() | ||
| .replace(/[^\w\s]/g, "") | ||
| .split(/\s+/) | ||
| .filter(Boolean); | ||
| for (const word of words) { | ||
| wordCount[word] = (wordCount[word] || 0) + 1; | ||
| } | ||
| return wordCount; | ||
| } | ||
|
|
||
| module.exports = countWords; |
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,17 @@ | ||
| const countWords = require("./count-words"); | ||
|
|
||
| test("counts word occurrences in a string", () => { | ||
| expect(countWords("you and me and you")).toEqual({ you: 2, and: 2, me: 1 }); | ||
| }); | ||
|
|
||
| test("returns an empty object for an empty string", () => { | ||
| expect(countWords("")).toEqual({}); | ||
| }); | ||
|
|
||
| test("ignores punctuation", () => { | ||
| expect(countWords("Hello, world! Hello?")).toEqual({ hello: 2, world: 1 }); | ||
| }); | ||
|
|
||
| test("ignores case", () => { | ||
| expect(countWords("A a Hello hello")).toEqual({ a: 2, hello: 2 }); | ||
| }); |
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.
Uh oh!
There was an error while loading. Please reload this page.