-
-
Notifications
You must be signed in to change notification settings - Fork 144
LONDON | MAY_2025 | EMILIANO_URUENA | DATA_GROUPS | SPRINT2 #580
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
3be7b63
d9a8561
0c34049
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 |
---|---|---|
|
@@ -11,6 +11,7 @@ const author = { | |
alive: true, | ||
}; | ||
|
||
for (const value of author) { | ||
for (const value of Object.keys(author)) { | ||
console.log(value); | ||
} | ||
} | ||
Comment on lines
+14
to
+16
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. An object is made up of keys and values:
The keys in this example are firstName, lastName, etc. The comment at the top asks you to log the values. Is that what your code does? |
||
// An object is not iterable. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
function contains() {} | ||
|
||
function contains(object,property) { | ||
// contains({a: 1, b: 2}, 'c') // returns false | ||
return object.hasOwnProperty(property); | ||
} | ||
//console.log(contains({a:1, b:2},'a')) | ||
module.exports = contains; |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,5 +1,12 @@ | ||||||||||||||||||||
function createLookup() { | ||||||||||||||||||||
function createLookup(countryCurrencyPairs) { | ||||||||||||||||||||
// implementation here | ||||||||||||||||||||
let objectLookup = {} | ||||||||||||||||||||
for (i = 0 ; i< countryCurrencyPairs.length ; i++){ | ||||||||||||||||||||
//const [country, currency] = countryCurrencyPairs[i] | ||||||||||||||||||||
//objectLookup[country] = currency; | ||||||||||||||||||||
objectLookup[countryCurrencyPairs[i][0]] = countryCurrencyPairs[i][1]; | ||||||||||||||||||||
} | ||||||||||||||||||||
return objectLookup | ||||||||||||||||||||
Comment on lines
2
to
+9
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 good! Alternatively, there's actually a built in javascript function to do this:
Suggested change
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries |
||||||||||||||||||||
} | ||||||||||||||||||||
|
||||||||||||||||||||
module.exports = createLookup; |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -6,11 +6,11 @@ function parseQueryString(queryString) { | |||||||
const keyValuePairs = queryString.split("&"); | ||||||||
|
||||||||
for (const pair of keyValuePairs) { | ||||||||
const [key, value] = pair.split("="); | ||||||||
queryParams[key] = value; | ||||||||
//const [key, value] = pair.split("=", 2); | ||||||||
const [key, value] = [pair.slice(0,pair.indexOf('=')),pair.slice(pair.indexOf('=')+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. I find this line a bit hard to understand. Maybe we could split it up into multiple variables?
Suggested change
Breakingn it up into multiple lines with clear names makes it easier to follow. You could also break out all the other parts of this. |
||||||||
queryParams[key] = value; | ||||||||
} | ||||||||
|
||||||||
return queryParams; | ||||||||
} | ||||||||
|
||||||||
console.log(parseQueryString("a=1&b=2")) | ||||||||
module.exports = parseQueryString; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,22 @@ const parseQueryString = require("./querystring.js") | |
|
||
test("parses querystring values containing =", () => { | ||
expect(parseQueryString("equation=x=y+1")).toEqual({ | ||
"equation": "x=y+1", | ||
equation: "x=y+1", | ||
}); | ||
}); | ||
|
||
test("parses querystring with multiple key-value pairs", () => { | ||
expect(parseQueryString("a=1&b=2")).toEqual({ | ||
a: "1", | ||
b: "2", | ||
}); | ||
}); | ||
|
||
test("Handle more than 1 pair in the query string", () => { | ||
expect(parseQueryString("sort=newest&color=blue")).toEqual({ | ||
sort: "newest", | ||
color: "blue", | ||
}); | ||
}); | ||
Comment on lines
+22
to
+26
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. We could also test for these additional cases: What happens if the string contains multiple equals signs? e.g. What happens if the query string is null, e.g. |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,19 @@ | ||
function tally() {} | ||
function tally(listItems) { | ||
if (typeof listItems === "string" || !Array.isArray(listItems)) return new Error("Input must be array"); | ||
if (listItems.length === 0) return {}; | ||
|
||
let objectItems = {}; | ||
|
||
for (const item of listItems) { | ||
//objectItems[item] = (objectItems[item] || 0) + 1; | ||
if (objectItems[item]) { | ||
objectItems[item] += 1; | ||
} else { | ||
objectItems[item] = 1; | ||
} | ||
} | ||
|
||
return objectItems; | ||
} | ||
console.log(tally(['a','a','a','b','c','c','a'])) | ||
module.exports = tally; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,7 @@ const tally = require("./tally.js"); | |
// Given an empty array | ||
// When passed to tally | ||
// Then it should return an empty object | ||
test.todo("tally on an empty array returns an empty object"); | ||
//test.todo("tally on an empty array returns an empty object"); | ||
|
||
// Given an array with duplicate items | ||
// When passed to tally | ||
|
@@ -32,3 +32,15 @@ test.todo("tally on an empty array returns an empty object"); | |
// Given an invalid input like a string | ||
// When passed to tally | ||
// Then it should throw an error | ||
describe('tally',() =>{ | ||
[ | ||
{input:['a'], expected:{ a: 1 }}, | ||
{input:['a', 'a', 'a'], expected:{ a: 3 }}, | ||
{input:['a', 'a', 'b', 'c'], expected:{ a : 2, b: 1, c: 1 }}, | ||
{input:[], expected:{}}, | ||
{input: 'String', expected: new Error("Input must be array")}, | ||
].forEach(({input,expected})=> | ||
it(`return an object containing the count for each unique item for [${input}]`,()=> | ||
expect(tally(input)).toEqual(expected)) | ||
); | ||
Comment on lines
+35
to
+45
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 testing pattern is great for adding new test cases easily, but the downside is that if a test fails, it can be hard to work out why it has failed, if each test has a clear message, it's more obvious what has gone wrong. I think either approach is valid though! |
||
}); |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,12 @@ | ||||||
const invert = require("./invert.js"); | ||||||
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} | ||||||
describe("invert", () =>{ | ||||||
[ | ||||||
{input:{x : 10, y : 20}, expected:{"10": "x", "20": "y"}}, | ||||||
{input:{ a : 1 }, expected:{ 1 : "a" }}, | ||||||
{input:{a : 1, b: 2}, expected:{ 1 : "a" , 2 : "b" }}, | ||||||
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. what happens if there are multiple identical values? Objects cannot have multiple identical keys. I'm not sure what should happen in this case. You could test for it with
Suggested change
|
||||||
].forEach(({input,expected}) => | ||||||
it(`return inverted key / value object, for [${input}]`,()=> | ||||||
expect(invert(input)).toEqual(expected)) | ||||||
) | ||||||
}); |
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.
This is correct, another correct option is
address.houseNumber