-
-
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
base: main
Are you sure you want to change the base?
Changes from all commits
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,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; |
| 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; |
| 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; |
| 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; |
|
Poonam-raj marked this conversation as resolved.
|
| 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" } | ||
|
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. Not quite The original code read as this Can you give me a corrected answer based on the original code here? |
||
| // 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 }. | ||
|
Comment on lines
+25
to
+28
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 each iteration overwrites to the same literal "key" property take another look at your answer - Your answer isn't quite right here |
||
|
|
||
| // 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. | ||
|
Comment on lines
+38
to
+43
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. great explanation |
||
|
|
||
| // 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; | ||
| 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); | ||
| }); |
| 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; |
| 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 }); | ||
| }); |
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