Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 326
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?
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
22d78b2f4a3df72a4905fbccf5d705c6ac4File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!There was an error while loading. Please reload this page. |
| 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; |
| 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 }); | ||
| }); |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.