Skip to content
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}`);
2 changes: 1 addition & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
4 changes: 2 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);
11 changes: 9 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function contains() {}
function contains(obj, prop) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}

module.exports = contains;
// Use the static Object.hasOwn() method
return Object.hasOwn(obj, prop);
}

module.exports = contains;
16 changes: 14 additions & 2 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ 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("contains on empty object returns false", () => {
expect(contains({}, "a")).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("contains an object with propery exist, returns true", () => {
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("return false when the property does not exist", () => {
expect(contains({ a: 1, b: 3 }, "c")).toEqual(false);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("returns false when input is an array, even if the index exists", () => {
// Index 0 exists in this array, but contains() should return false
// because the requirement is for plain objects only.
expect(contains(["apple", "banana"], 0)).toEqual(false);
});
12 changes: 10 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = {};

for (const [countryCode, countryCurrency] of countryCurrencyPairs) {
lookup[countryCode] = countryCurrency;
}

return lookup;
}

module.exports = createLookup;


9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
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",() =>{
expect(createLookup([["US", "USD"], ["CA", "CAD"]])).toEqual(
{"US":"USD",
"CA": "CAD"

})

})

/*

Expand Down
13 changes: 11 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
function parseQueryString(queryString) {
if (queryString.startsWith("?")) {
queryString = queryString.substring(1);
}

const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}

const keyValuePairs = queryString.split("&");

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

const [key, ...valueParts] = pair.split("=");
const value = valueParts.join("=");

queryParams[decodeURIComponent(key)] = decodeURIComponent(value);
}

return queryParams;
Expand Down
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(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array");
}

const result = Object.create(null);

for (const item of arr) {
if (result[item] === undefined) {
result[item] = 1;
} else {
result[item] = result[item] + 1;
}
}

return result;
}

module.exports = tally;
35 changes: 34 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,45 @@ 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("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally counts duplicate items correctly", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 1,
});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws an error for invalid input", () => {
expect(() => tally("hello")).toThrow();
});

// Given an array containing keys that match built-in object properties
// When passed to tally
// Then it should correctly count them without conflicts from inherited properties
test("tally works correctly with reserved words like toString", () => {
const input = ["toString", "toString", "hasOwnProperty"];
const expected = {
toString: 2,
hasOwnProperty: 1,
};
expect(tally(input)).toEqual(expected);
});

// Extra verification test to confirm pure object behavior
test("tally accurately counts prototype method names as strings", () => {
const input = ["toString", "toString"];
const expected = {
toString: 2,
};
expect(tally(input)).toEqual(expected);
});
47 changes: 34 additions & 13 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
// Let's define how invert should work
// a) What is the current return value when invert is called with { a : 1 }
// { key: 1 }

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

// c) What is the target return value when invert is called with { a: 1, b: 2 }
// { "1": "a", "2": "b" }

// Given an object
// When invert is passed this object
// Then it should swap the keys and values in the object
// d) What does Object.entries return? Why is it needed in this program?
// It returns an array of [key, value] pairs.
// It is needed so we can loop through both keys and values at the same time when inverting the object.

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
// e) Explain why the current return value is different from the target output
// The original code used 'invertedObj.key = value', which creates a property literally named "key"
// instead of using the value dynamically.
// This causes each iteration to overwrite the previous one.
// It also does not handle collisions (multiple keys with the same value).

// f) 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.key = value;
if (invertedObj[value] === undefined) {
invertedObj[value] = key;
} else if (Array.isArray(invertedObj[value])) {
invertedObj[value].push(key);
} else {
invertedObj[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 }
// --- CONSOLE TESTS TO PROVE IT IS FIXED ---
console.log("Test 1 (Simple):", invert({ a: 1 }));
// Expected: { '1': 'a' }

// c) What is the target return value when invert is called with {a : 1, b: 2}
console.log("Test 2 (No duplicates):", invert({ x: 10, y: 20 }));
// Expected: { '10': 'x', '20': 'y' }

// c) What does Object.entries return? Why is it needed in this program?
console.log("Test 3 (Collision):", invert({ a: 1, b: 1 }));
// Expected: { '1': ['a', 'b'] }

// d) Explain why the current return value is different from the target output
console.log("Test 4 (Triple collision):", invert({ a: 1, b: 1, c: 1 }));
// Expected: { '1': ['a', 'b', 'c'] }

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You deleted your answers to these questions.

Anyway, do you recognise what this statement (in the original code) does?

invertedObj.key = value;

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Restored missing answers (a-e) and corrected the logic. Replaced literal .key with dynamic [value] notation to prevent overwriting and ensure keys are swapped correctly.

module.exports = invert;