-
-
Notifications
You must be signed in to change notification settings - Fork 147
Glasgow | ITP MAY | MIRABELLE MORAH | Module Data Groups | Sprint-2 #587
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,12 @@ | ||
function contains() {} | ||
function contains(obj, property) { | ||
if ( | ||
obj === null || | ||
typeof obj !== "object" || | ||
Array.isArray(obj) | ||
) { | ||
return false; | ||
} | ||
return Object.prototype.hasOwnProperty.call(obj, property); | ||
} | ||
|
||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,17 @@ | ||
function createLookup() { | ||
// implementation here | ||
function createLookup(codePairs) { | ||
|
||
const lookup = {}; // Initialize an empty object to store the lookup pairs | ||
|
||
// Iterate over each pair in the input array | ||
for (const pair of codePairs) { | ||
// Ensure the pair has at least two elements (key and value) | ||
if (pair && pair.length >= 2) { | ||
const key = pair[0]; // The first element is the key (e.g., 'US') | ||
const value = pair[1]; // The second element is the value (e.g., 'USD') | ||
lookup[key] = value; // Assign the value to the key in the lookup object | ||
} | ||
} | ||
return lookup; | ||
} | ||
|
||
module.exports = createLookup; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,26 @@ test("parses querystring values containing =", () => { | |
"equation": "x=y+1", | ||
}); | ||
}); | ||
|
||
test("parses multiple key-value pairs", () => { | ||
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); | ||
}); | ||
|
||
test("parses keys with empty values", () => { | ||
expect(parseQueryString("a=&b=2")).toEqual({ a: "", b: "2" }); | ||
}); | ||
|
||
test("parses keys with no value", () => { | ||
expect(parseQueryString("a&b=2")).toEqual({ a: "", b: "2" }); | ||
}); | ||
|
||
test("parses empty string", () => { | ||
expect(parseQueryString("")).toEqual({}); | ||
}); | ||
|
||
test("parses encoded characters", () => { | ||
expect(parseQueryString("name=John%20Doe&city=New%20York")).toEqual({ | ||
name: "John Doe", | ||
city: "New York", | ||
}); | ||
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. Good test cases |
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
function tally() {} | ||
function tally(list) { | ||
if (!Array.isArray(list)) { | ||
throw new Error("Input must be an array"); | ||
} | ||
const counts = {}; | ||
for (const item of list) { | ||
counts[item] = (counts[item] || 0) + 1; | ||
} | ||
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. This is a clever and quick way of initialising the object |
||
return counts; | ||
} | ||
|
||
module.exports = tally; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ | |
|
||
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||
|
||
/* | ||
function invert(obj) { | ||
const invertedObj = {}; | ||
|
||
|
@@ -16,14 +17,43 @@ function invert(obj) { | |
return invertedObj; | ||
} | ||
|
||
console.log(invert({ a: 1, b: 2 })); | ||
|
||
*/ | ||
|
||
// a) What is the current return value when invert is called with { a : 1 } | ||
|
||
// ... currently gives { key: 1 } | ||
|
||
// b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
|
||
// ... currently gives { key: 2 } | ||
|
||
// c) What is the target return value when invert is called with {a : 1, b: 2} | ||
|
||
// ... should be { "1": "a", "2": "b" } | ||
|
||
// 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 | ||
// ... Object.entries returns an array of key-value pairs from the object, which allows us to iterate over them easily. | ||
|
||
// d) Explain why the current return value is different from the target output | ||
|
||
// ... The current implementation incorrectly assigns the value to the key property of invertedObj instead of using the value as a key in invertedObj. fixed by using invertedObj[value] = key; | ||
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. Good explanations 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. I'm grateful and thank you |
||
|
||
// e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
|
||
function invert(obj) { | ||
const invertedObj = {}; | ||
|
||
for (const [key, value] of Object.entries(obj)) { | ||
invertedObj[value] = key; | ||
} | ||
|
||
return invertedObj; | ||
} | ||
|
||
console.log(invert({ a: 1 })); | ||
console.log(invert({ a: 1, b: 2 })); | ||
|
||
module.exports = invert; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
const invert = require("./invert.js"); | ||
|
||
test("invert swaps keys and values for single pair", () => { | ||
expect(invert({ a: 1 })).toEqual({ "1": "a" }); | ||
}); | ||
|
||
test("invert swaps keys and values for multiple pairs", () => { | ||
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" }); | ||
expect(invert({ x: 10, y: 20 })).toEqual({ "10": "x", "20": "y" }); | ||
}); | ||
|
||
test("invert works with string values", () => { | ||
expect(invert({ a: "apple", b: "banana" })).toEqual({ apple: "a", banana: "b" }); | ||
}); | ||
|
||
test("invert returns empty object for empty input", () => { | ||
expect(invert({})).toEqual({}); | ||
}); |
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.
Good tests