Skip to content

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);

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

5 changes: 3 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An object is made up of keys and values:

{
<key>: <value>
}

The keys in this example are firstName, lastName, etc.
The values are "Zadie", "Smith", etc.

The comment at the top asks you to log the values. Is that what your code does?

// An object is not iterable.
5 changes: 3 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};


console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join('\n')}`); //\n
7 changes: 5 additions & 2 deletions Sprint-2/implement/contains.js
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;
14 changes: 13 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
//test.todo("contains on empty object returns false");

// Given an object with properties
// When passed to contains with an existing property name
Expand All @@ -33,3 +33,15 @@ test.todo("contains on empty object returns false");
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
describe("contains", () => {
[
{ input: [{a: 1, b: 2}, 'b'], expected: true},
{ input: [{a: 1, b: 2}, 'c'], expected: false},
{ input: [{},'a'], expected: false},
{ input: [[],'a'], expected: false},

].forEach(({input, expected}) =>
it(`Expected to check the object contains a particular property, for: [${input}]`,() => expect(contains(...input)).toEqual(expected))

);
});
9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.js
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

Choose a reason for hiding this comment

The 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
// 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
return Object.fromEntries(countryCurrencyPairs);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/fromEntries

}

module.exports = createLookup;
12 changes: 11 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
//test.todo("creates a country currency code lookup for multiple codes");

/*
Expand Down Expand Up @@ -33,3 +33,13 @@ It should return:
'CA': 'CAD'
}
*/

describe("createLookup", () => {
[
{ input: [['US', 'USD'], ['CA', 'CAD']], expected: {'US': 'USD', 'CA': 'CAD'}},

].forEach(({input, expected}) =>
it(`It should Create a lookup object of key value pairs from an array of code pairs [${input}]`,() => expect(createLookup(input)).toEqual(expected))

);
});
8 changes: 4 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)];
Copy link

@MorganDavid MorganDavid Jul 12, 2025

Choose a reason for hiding this comment

The 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
const [key, value] = [pair.slice(0,pair.indexOf('=')),pair.slice(pair.indexOf('=')+1)];
const equalsIndex = pair.indexOf('=');
const [key, value] = [pair.slice(0,equalsIndex),pair.slice(pair.indexOf('=')+1)];

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;
18 changes: 17 additions & 1 deletion Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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. a=b=c=c&b=a,

What happens if the query string is null, e.g. a in this example: a=&b=hello



18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
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;
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Choose a reason for hiding this comment

The 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!

});
18 changes: 10 additions & 8 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ 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 }
}

//19 a) What is the current return value when invert is called with { a : 1 }
// After the function invert() is called with the parameter { a : 1 }, it returns: { key : 1 }
// b) What is the current return value when invert is called with { a: 1, b: 2 }

// After the function invert() is called with the parameter { a: 1, b: 2 }, it returns: { key : 2 }
// c) What is the target return value when invert is called with {a : 1, b: 2}

// After the function invert() with the parameter { a: 1, b: 2 }, the target return value is: { 1 : a , 2 : b }
// c) What does Object.entries return? Why is it needed in this program?

// Object.entries return an Array of key-value pair arrays from obj. It is useful but not strictly necessary.
// d) Explain why the current return value is different from the target output

// Because in the line 13 the invertedObj.key statement is not using the value of the key variable.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// Done!
module.exports = invert;
12 changes: 12 additions & 0 deletions Sprint-2/interpret/invert.test.js
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" }},

Choose a reason for hiding this comment

The 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
{input:{a : 1, b: 2}, expected:{ 1 : "a" , 2 : "b" }},
{input:{a : 1, b: 1}, expected:/* What happens here? */ },

].forEach(({input,expected}) =>
it(`return inverted key / value object, for [${input}]`,()=>
expect(invert(input)).toEqual(expected))
)
});