Skip to content

Glasgow | May-2025 | Mansoor Munawar | Data Groups| Sprint 2 #753

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 9 commits into
base: main
Choose a base branch
from
Open
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// We create an object address which has 5 properties.In order to access the value within a property
// like house number we either need to use the dot notation such as address.houseNumber or square brackets such as address["houseNumber"]

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +14,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);
5 changes: 3 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// it doesnt work as objects are not iteraable and there for we cant use for loops.
// But by using object.values allows us to get the value within an object and store it in an array and then we can use for of loop.
// This program attempts to log out all the property values in the object.
// But it isn't working. Explain why first and then fix the problem

Expand All @@ -11,6 +12,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
9 changes: 6 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// there is a bug in this code, to access values of ingredients we need to use dot notation (recipe.ingredients) to access it values.
// In order to print out ingredients in seperate line we can then use for of loop.
// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
// How can you fix it?
Expand All @@ -11,5 +12,7 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);
for (value of recipe.ingredients) {
console.log(value);
}
5 changes: 4 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
function contains() {}
function contains(obj, key) {
return Object.hasOwn(obj, key);
}

console.log(contains([1, 2, 3, 4], "ingredients"));
module.exports = contains;
39 changes: 35 additions & 4 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,51 @@ as the object doesn't contains a key of 'c'
// Given a contains function
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

test("Given a contains function when passed an object and property name then it should false if the object doesnt contain the property", () => {
currentOutput = contains(
{ firstName: "Mansoor", lastName: "Munawar" },
"hello"
);
targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
// When passed an object and a property name")
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("contains on empty object returns false", () => {
const currentOutput = contains({});
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

test("given an object with properties when passed to contains with existing propery name, it returns true", () => {
const currentOutput = contains(
{ firstName: "Mansoor", organisation: "CYF" },
"firstName"
);
const targetOutput = true;
expect(currentOutput).toBe(targetOutput);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("Given an object with properties and when it passed to contains with non-existent property name then it should return false", () => {
const currentOutput = contains(
{ firstName: "Mansoor", organisation: "CYF" },
"Name"
);
const targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("given invalid parameters like an array when passed to contains, it returns false or throw an error", () => {
const currentOutput = contains([1, 2, 3, 4], "hello");
targetOutput = false;
expect(currentOutput).toBe(targetOutput);
});
14 changes: 12 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
function createLookup() {
function createLookup(twoDeminsionalArray) {
// implementation here
let result = {};
for (let [country, currency] of twoDeminsionalArray) {
result[country] = currency;
}
return result;
}

console.log(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
);
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,16 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
test("creates a country currency code lookup for multiple codes", () => {
let currentOutput = createLookup([
["US", "USD"],
["CA", "CAD"],
]);
let targetOutput = {
US: "USD",
CA: "CAD",
};
expect(currentOutput).toEqual(targetOutput);
});

/*

Expand Down
12 changes: 9 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
if (!pair.includes("=")) {
continue;
}
const [key, ...value] = pair.split("=");
if (key === "") {
continue;
}
queryParams[key] = value.join("=");
}

return queryParams;
}

console.log(parseQueryString("=sort&colour=yellow"));
module.exports = parseQueryString;
16 changes: 14 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
// Below is one test case for an edge case the implementation doesn't handle well.
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.

const parseQueryString = require("./querystring.js")
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 missing value", () => {
let currentOutput = parseQueryString("sort&colour=yellow");
let targetOutput = { colour: "yellow" };
expect(currentOutput).toEqual(targetOutput);
});

test("parses querystring with missing key", () => {
let currentOutput = parseQueryString("=dark&colour=yellow");
let targetOutput = { colour: "yellow" };
expect(currentOutput).toEqual(targetOutput);
});
16 changes: 15 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
function tally() {}
function tally(arrayOfItems) {
if (!Array.isArray(arrayOfItems)) {
throw new TypeError("parameter must be an array");
}
let result = {};
arrayOfItems.forEach((element) => {
if (element in result) {
result[element] += 1;
} else {
result[element] = 1;
}
});
return result;
}
console.log(tally([]));

module.exports = tally;
21 changes: 18 additions & 3 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ const tally = require("./tally.js");
// Given a function called tally
// When passed an array of items
// Then it should return an object containing the count for each unique item

test("when function tally called with an array of items, it returns object containing the count for each unique item", () => {
let currentOutput = tally(["a", "a", "a"]);
let targetOutput = { a: 3 };
expect(currentOutput).toEqual(targetOutput);
});
// 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("tally on an empty array returns an empty object", () => {
let currentOutput = tally([]);
let targetOutput = {};
expect(currentOutput).toEqual(targetOutput);
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

test("tally on array of duplicate items, it returns counts for each unique item", () => {
let currentOutput = tally(["a", "a", "b", "c"]);
let targetOutput = { a: 2, b: 1, c: 1 };
expect(currentOutput).toEqual(targetOutput);
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("Given an invalid input like a string passed to tally, it returns an error", () => {
expect(() => tally("hello")).toThrow(TypeError);
});
14 changes: 8 additions & 6 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;
}
console.log(invert({ x: 10, y: 20 }));
module.exports = invert;

// a) What is the current return value when invert is called with { a : 1 }

// it will be {"key" : 1}
// b) What is the current return value when invert is called with { a: 1, b: 2 }

// it will be ["key": 2]
// c) What is the target return value when invert is called with {a : 1, b: 2}

// the target return value needs to be {"1": "a", "2": "b"}
// c) What does Object.entries return? Why is it needed in this program?

// object.entries return an array of each [key] and [value] pair and in this case it is necessary as we need both the key and value from the object.
// d) Explain why the current return value is different from the target output

// there is an bug as we use .key instead of [value]. So instead of taking the variable [value] we are instead creating a property called key which isnt correct.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
13 changes: 13 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const invert = require("./invert.js");

test("When invert is passed an object it should swap the keys and values in the object and then return it", () => {
let currentOutput = invert({ x: 10, y: 20 });
let targetOutput = { 10: "x", 20: "y" };
expect(currentOutput).toEqual(targetOutput);
});

test("When invert is passed an object it should swap the keys and values in the object and then return it", () => {
let currentOutput = invert({ a: 1, b: 2 });
let targetOutput = { 1: "a", 2: "b" };
expect(currentOutput).toEqual(targetOutput);
});