Skip to content
5 changes: 4 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
// Predict and explain first...

// I predict that the console will log "My house number is undefined"
// because it is using array index notation whereas address is an object.

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

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

// I predict that the console will log "undefined" because
// it is looping through elements in a collection (e.g. arrays and sets)
// whereas author is an object.

// 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

// The loop is trying to loop through an array

const author = {
firstName: "Zadie",
lastName: "Smith",
Expand All @@ -11,6 +17,6 @@ const author = {
alive: true,
};

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

// The console will print the recipe title, serves and the recipe object itself rather than the actual ingredient

// 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 @@ -10,6 +12,8 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(
`titie: ${recipe.title}, serves: ${
recipe.serves
}, ingredients: ${recipe.ingredients.join(", ")}`
Copy link
Contributor

Choose a reason for hiding this comment

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

The requirement is "Each ingredient should be logged on a new line". :D

Copy link
Author

Choose a reason for hiding this comment

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

Oops! Then why did you suggest Array.prototype.join()? My original implementation did exactly that.

Copy link
Contributor

Choose a reason for hiding this comment

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

You could join with new line character.

);
8 changes: 7 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(object, property) {
if (object === null || typeof object !== "object" || Array.isArray(object)) {
throw new Error("object is not an object type!");
}

return Object.prototype.hasOwnProperty.call(object, property);
}

module.exports = contains;
26 changes: 25 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,44 @@ 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("object contains the property returns true", () => {
const object = { forename: "John" };
expect(contains(object, "forename")).toEqual(true);
expect(contains(object, "surname")).toEqual(false);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("empty object returns false", () => {
const object = {};
const result = contains(object);
expect(result).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("object contains properties returns true for a property", () => {
const object = { forename: "John", surname: "Appleseed" };
const property = "surname";
const result = contains(object, property);
expect(result).toEqual(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("object returns false for a non-existent property", () => {
const object = { forename: "John", surname: "Appleseed" };
const property = "middlename";
const result = contains(object, property);
expect(result).toEqual(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("object throws an error for a non-object type", () => {
expect(() => contains([])).toThrow("object is not an object type!");
});
4 changes: 2 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function createLookup() {
// implementation here
function createLookup(arrayPairs) {
return Object.fromEntries(arrayPairs);
}

module.exports = createLookup;
16 changes: 15 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
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", () => {
const arrayPairs = [
["US", "USD"],
["CA", "CAD"],
];

const object = {
US: "USD",
CA: "CAD",
};

const result = createLookup(arrayPairs);

expect(result).toEqual(object);
});

/*

Expand Down
11 changes: 5 additions & 6 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

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

const searchParameters = new URLSearchParams(queryString);

for (const [key, value] of searchParameters.entries()) {
queryParams[key] = value;
}

Expand Down
100 changes: 95 additions & 5 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,100 @@
// 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",
});
// Verifies that raw '+' characters are interpreted as spaces by URLSearchParams
test("parses query string values with unencoded '+' as spaces", () => {
const result = parseQueryString("equation=x=y+1");
const output = { equation: "x=y 1" }; // because URLSearchParams decodes '+' as a space according to URL encoding rules
expect(result).toEqual(output);
});

// Verifies that properly URL-encoded '=' and '+' characters are preserved correctly
test("parses query string values with encoded '=' and '+' correctly", () => {
const result = parseQueryString("equation=x%3Dy%2B1"); // '%3D' => '=', '%2B' => '+'
const output = { equation: "x=y+1" }; // because URLSearchParams decodes percent-encoded characters but preserves literal '+' when encoded as '%2B'
expect(result).toEqual(output);
});

test("returns empty object for empty string", () => {
const result = parseQueryString();
const output = {};
expect(result).toEqual(output);
});

test("handles undefined input", () => {
const result = parseQueryString(undefined);
const output = {};
expect(result).toEqual(output);
});

test("handles null input", () => {
const result = parseQueryString(null);
const output = {};
expect(result).toEqual(output);
});

test("handles key without value", () => {
const result = parseQueryString("name=");
const output = { name: "" };
expect(result).toEqual(output);
});

test("handles missing key", () => {
const result = parseQueryString("=John");
const output = { "": "John" };
expect(result).toEqual(output);
});

test("handles multiple key-value pairs", () => {
const result = parseQueryString("name=John&age=30&city=London");
const output = {
name: "John",
age: "30",
city: "London",
};
expect(result).toEqual(output);
});

test("handles duplicate keys by overwriting previous value", () => {
const result = parseQueryString("tag=js&tag=web");
const output = { tag: "web" };
expect(result).toEqual(output);
});

test("handles URL-encoded characters", () => {
const result = parseQueryString("city=New%20York");
const output = { city: "New York" };
expect(result).toEqual(output);
});

test("handles unencoded special characters", () => {
const result = parseQueryString("greeting=hello world");
const output = { greeting: "hello world" };
expect(result).toEqual(output);
});

test("handles keys containing '='", () => {
const result = parseQueryString("a=b=c=d");
const output = { a: "b=c=d" };
expect(result).toEqual(output);
});

test("ignores leading and trailing ampersands", () => {
const result = parseQueryString("&name=John&age=30&");
const output = { name: "John", age: "30" };
expect(result).toEqual(output);
});

test("ignores empty pairs", () => {
const result = parseQueryString("name=John&&age=30&");
const output = { name: "John", age: "30" };
expect(result).toEqual(output);
});

test("handles equals sign only", () => {
const result = parseQueryString("=");
const output = { "": "" };
expect(result).toEqual(output);
});
10 changes: 9 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function tally() {}
function tally(array) {
if (!Array.isArray(array)) throw new Error("array is not an array!");

return array.reduce((accumulator, currentValue) => {
const value = accumulator[currentValue];
accumulator[currentValue] = value ? value + 1 : 1;
return accumulator;
}, Object.create(null));
}

module.exports = tally;
21 changes: 20 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,35 @@ 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("output containing the count for each unique item", () => {
const array = ["a", "b", "c"];
const result = tally(array);
const output = { a: 1, b: 1, c: 1 };
expect(result).toEqual(output);
});

// 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", () => {
const array = [];
const result = tally(array);
expect(result).toEqual({});
});

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

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("invalid input", () => {
expect(() => tally("Hello, World!")).toThrow("array is not an array!");
});
23 changes: 22 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,44 @@
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}

function invert(obj) {
if (obj === null || typeof obj !== "object") {
throw new Error("Parameter `obj` must be an object type");
}

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 }

// { 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?

// Returns an array of key/values of the enumerable own properties of an object
// It is a convenient way to map the object properties and values into key-value pairs

// d) Explain why the current return value is different from the target output

// Because of this line:
// invertedObj.key = value;
// The property "key" is declared and the value is assigned to the "key" property

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

// Refer to invert.test.js
Loading