Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
5d54c31
fix house number access in address object
marscancode Nov 12, 2026
8d0e38a
fixed loop to print author object values
marscancode Nov 12, 2026
1f4ed4b
fixed recipe and loop output
marscancode Nov 12, 2026
7faefe4
Add empty object test for contains
marscancode Nov 15, 2026
0c0415d
implemented check for empty object in contains
marscancode Nov 15, 2026
e0d3618
added test to check wether object contains property
marscancode Nov 15, 2026
cd8545b
implemented property check in contains
marscancode Nov 15, 2026
92710cd
added missing property test for contains
marscancode Nov 15, 2026
123e142
added test to check wether contains throws an error for arrays
marscancode Nov 15, 2026
a770518
implemented contains to handle arrays
marscancode Nov 15, 2026
1c0e82d
added test to check contains throws an error for strings
marscancode Nov 15, 2026
6a55470
implemented contains to handle strings
marscancode Nov 15, 2026
6befbcd
added test to check wether contains throws an error when given a number
marscancode Nov 15, 2026
e9bc9c6
implemented contains to handle numbers
marscancode Nov 15, 2026
4866df9
test to check wether contains throws an error for null
marscancode Nov 15, 2026
9e57410
implemented contains to handle null
marscancode Nov 15, 2026
8d4c1cc
added to test to check wether contains throws an error with no argum…
marscancode Nov 15, 2026
ab6963f
implemented contains to handle undefined
marscancode Nov 15, 2026
7e908fb
added test for lookup with multiple currency pairs
marscancode Nov 15, 2026
aadb7e6
update
marscancode Nov 15, 2026
aeef626
Implemented country currency lookup
marscancode Nov 15, 2026
c362f4e
test to check lookup returns an empty object when given an empty array
marscancode Nov 15, 2026
f50f02a
added test to check that lookup handles duplicate country codes
marscancode Nov 15, 2026
9731b4d
Removed optional stretch test for duplicate keys
marscancode Nov 15, 2026
f7ffc4d
Fixed query string handling for edge cases
marscancode Nov 15, 2026
7a823ce
added test for tally with an empty array
marscancode Nov 15, 2026
21d9972
Test to check if tally counts items in an array
marscancode Nov 16, 2026
590c736
Test to check if tally rejects invalid inputs
marscancode Nov 16, 2026
a5f091f
invert exercise answers
marscancode Nov 16, 2026
13a4171
implemented invert to swap object keys and values
marscancode Nov 16, 2026
f1cbb62
Tests to check invert handles multiple key-value pairs
marscancode Nov 16, 2026
ca51a9f
test to check if invert handles empty objects
marscancode Nov 16, 2026
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);
}
6 changes: 4 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);
for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
22 changes: 21 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
function contains() {}
function contains(obj, propertyName) {
if (Array.isArray(obj)) {
throw new Error("contains requires an object");
}
if (typeof obj === "string") {
throw new Error("contains requires an object");
}
if (typeof obj === "number") {
throw new Error("contains requires an object");
Comment on lines +5 to +9

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does contains(false, "a") do with these type checks, given the requirement that non-object inputs throw?

To "like" or "dislike" this comment, please follow this link

}
if (obj === null) {
throw new Error("contains requires an object");
}
if (obj === undefined) {
throw new Error("contains requires an object");
}
if (Object.keys(obj).length === 0) {
return false;
}

return Object.keys(obj).includes(propertyName);
}
module.exports = contains;
34 changes: 26 additions & 8 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,40 @@ as an array isn't an object
// 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("returns true when given an object and property name that contains property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
});

test("returns false when given an object and property name that doesn't contain the property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");

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

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("returns false when given an empty object", () => {
expect(contains({})).toBe(false);
});

// Given a value that isn't an object - an array, a string, a number,
// null, or no argument at all
// When passed to contains
// Then it should throw Error("contains requires an object")
// (careful: typeof [] and typeof null are both "object")
test("throws an error when given an array", () => {
expect(() => contains([1, 2, 3], "a")).toThrow("contains requires an object");
});

test("throws an error when given a string", () => {
expect(() => contains("hello", "a")).toThrow("contains requires an object");
});
test("throws an error when given a number", () => {
expect(() => contains(42, "a")).toThrow("contains requires an object");
});
test("throws an error when given null", () => {
expect(() => contains(null, "a")).toThrow("contains requires an object");
});

test("throws an error when given no argument", () => {
expect(() => contains()).toThrow("contains requires an object");
});
8 changes: 6 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = {};
for (const pair of countryCurrencyPairs) {
lookup[pair[0]] = pair[1];
}
return lookup;
}

module.exports = createLookup;
23 changes: 21 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
const createLookup = require("./lookup.js");

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

test("given currency code pairs returns an object with the country and its currency code", () => {
expect(
createLookup([
["JP", "JPY"],
["GB", "GBP"],
])
).toEqual({ JP: "JPY", GB: "GBP" });
});

test("given an empty array returns an empty object", () => {
expect(createLookup([])).toEqual({});
});

test("reassigns the previous currency when a country code is duplicated", () => {
expect(
createLookup([
["GB", "GBP"],
["GB", "XXX"],
])
).toEqual({ GB: "XXX" });
});
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down
8 changes: 6 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const keyValuePairs = queryString.split("&").filter((pair) => pair !== "");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const parts = pair.split("=");
const key = decodeURIComponent(parts[0].replace(/\+/g, " "));
const value = decodeURIComponent(
parts.slice(1).join("=").replace(/\+/g, " ")
);
queryParams[key] = value;
}

Expand Down
12 changes: 1 addition & 11 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Below are some test cases the implementation doesn't handle well.
// Fix the implementation for these tests, 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("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
Expand Down Expand Up @@ -36,13 +36,3 @@ test("should replace '+' by ' '", () => {
"full name": "John Doe",
});
});

// Stretch exercise: Handling query strings that contain identical keys

// Delete this test if you are not working on this optional case
test("should store values of a key in an array when the key has 2 or more values", () => {
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
key: ["value1", "value2", "value3"],
foo: "bar",
});
});
19 changes: 18 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function tally() {}
function tally(items) {
const counts = {};
if (!Array.isArray(items)) {
throw new Error("tally requires an array");
}

for (let i = 0; i < items.length; i++) {
const item = items[i];

if (Object.keys(counts).includes(item)) {
counts[item] = counts[item] + 1;
} else {
counts[item] = 1;
}
}

return counts;
}

module.exports = tally;
26 changes: 20 additions & 6 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,30 @@ 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

// 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");

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

// Given an invalid input like a string, a number, or no argument at all
// When passed to tally
// Then it should throw Error("tally requires an array")
test("given invalid input like a string, throw an error message", () => {
expect(() => tally("hello")).toThrow("tally requires an array");
});

test("given invalid input like a number, throw an error message", () => {
expect(() => tally(7)).toThrow("tally requires an array");
});

test("given invalid input like no argument, throw an error message", () => {
expect(() => tally()).toThrow("tally requires an array");
});
13 changes: 8 additions & 5 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,23 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
invertedObj[value] = key;
}

return invertedObj;
}

module.exports = invert;

// a) What is the current return value when invert is called with { a : 1 }
//When invert({ a: 1 }) is called, the loop assigns the value 1 to a property literally named key. Therefore, the function returns { 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" }
// c) What does Object.entries return? Why is it needed in this program?

// It returns the key value pairs in separate arrays. It's needed in this program so we can access the keys and values separately.
// d) Explain why the current return value is different from the target output

// It uses dot notation (invertedObj.key), which creates a property called key. It doesn't use the original value as the new property name, so the keys and values aren't swapped correctly.
// 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("given a key-value pair, should return it inverted", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("given multiple key-value pairs, should return them inverted", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});

test("given an empty object, should return an empty object", () => {
expect(invert({})).toEqual({});
});
Loading