-
-
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
4
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
4 commits
Select commit
Hold shift + click to select a range
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
|
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. You're missing all the explanations and answers to questions a, b, c, c, d, e - please add them in
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. hi @Poonam-raj , i answered all questions |
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,57 @@ | ||
| // 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; | ||
| // invertedObj[value] = key; | ||
| // } | ||
| // return invertedObj; | ||
| // } | ||
|
|
||
| // a) What is the current return value when invert is called with { a: 1 }? | ||
| // -> { key: 1, "1": "a" } | ||
| // The line `invertedObj.key = value` sets a property literally named | ||
| // "key" (not the loop variable's value) to 1, alongside the correct | ||
| // inverted pair "1": "a" from the line below it. | ||
|
|
||
| // b) What is the current return value when invert is called with { a: 1, b: 2 }? | ||
| // -> { key: 2, "1": "a", "2": "b" } | ||
| // Each loop iteration overwrites the same literal "key" property, so | ||
| // only the last value assigned to it survives - here, 2 from { b: 2 }. | ||
|
|
||
| // 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? | ||
| // -> The bug is `invertedObj.key = value`. Because "key" is written as a | ||
| // literal property name (dot notation), it always sets a property | ||
| // called "key" rather than using the value of the loop variable | ||
| // `key`. Only bracket notation - invertedObj[key] - would use the | ||
| // variable's actual value as the property name. This line also isn't | ||
| // needed at all for a correct inversion; it should be removed. | ||
|
|
||
| // e) Fix: remove the incorrect `invertedObj.key = value` line entirely, | ||
| // leaving only `invertedObj[value] = key`, which correctly maps each | ||
| // value to its original key. See invert.test.js for tests proving | ||
| // the fix works, including empty objects and string values. | ||
| 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.
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.
I would consider having a smaller starting test for the valid input, to build the test suite up more gradually, prove the function can take different inputs and isn't hardcoded