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);
}
2 changes: 1 addition & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
4 changes: 3 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function contains() {}
function contains(obj, property_name) {
return property_name in obj;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Does this consider the difference between defined properties and built-in properties?

}

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,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")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("returns true when object contains an existing property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(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 object does not contain the property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("returns false or throws an error when passed invalid parameters like an array", () => {
expect(() => contains([], "a")).not.toThrow();
expect(contains([], "a")).toBe(false);
});
8 changes: 7 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
function createLookup(country_currency_pairs) {
// implementation here
const lookup = {};
for (const [country, currency] of country_currency_pairs) {
lookup[country] = currency;
}

return lookup;
}

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

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

/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down Expand Up @@ -33,3 +31,37 @@ It should return:
'CA': 'CAD'
}
*/

test("creates a country currency code lookup for multiple codes", () => {
const countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
];

const result = createLookup(countryCurrencyPairs);

expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});

test("creates a country currency code lookup for a single code", () => {
const countryCurrencyPairs = [
["GB", "GBP"],
];

const result = createLookup(countryCurrencyPairs);

expect(result).toEqual({
GB: "GBP",
});
});

test("returns an empty object when given an empty array", () => {
const countryCurrencyPairs = [];

const result = createLookup(countryCurrencyPairs);

expect(result).toEqual({});
});
29 changes: 19 additions & 10 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
if (queryString === "") {
return {};
} else {
queryString = decodeURIComponent(queryString)
.replace(/\+/g, " ")
.replace(/&&/g, "&")
.replace(/&+$/, "");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
}
const splitByAnds = queryString.split("&");

const splitByEquals = splitByAnds.map(x => {
const match = x.match(/^([^=]*)(?:=(.*))?$/);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Can you explain what this regular expression is doing?


return queryParams;
const key = match[1];
const value = match[2] ?? "";

return [key, value];
});

return Object.fromEntries(splitByEquals);
}
}

module.exports = parseQueryString;
62 changes: 56 additions & 6 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,62 @@ test("should replace '+' by ' '", () => {
});
});

// Stretch exercise: Handling query strings that contain identical keys
test("should parse basic key-value pairs", () => {
expect(parseQueryString("name=John&age=30")).toEqual({
name: "John",
age: "30",
});
});

test("should replace '+' by ' '", () => {
expect(parseQueryString("name=John+Doe&city=New+York")).toEqual({
name: "John Doe",
city: "New York",
});

expect(parseQueryString("full+name=John+Doe")).toEqual({
"full name": "John Doe",
});
});

test("should decode percent-encoded characters", () => {
expect(parseQueryString("message=Hello%20World")).toEqual({
message: "Hello World",
});

// 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",
expect(parseQueryString("%24half=1%2F2")).toEqual({
$half: "1/2",
});
});

test("should handle keys without values", () => {
expect(parseQueryString("debug")).toEqual({
debug: "",
});

expect(parseQueryString("key")).toEqual({
key: "",
});
});

test("should handle empty values", () => {
expect(parseQueryString("name=&age=25")).toEqual({
name: "",
age: "25",
});

expect(parseQueryString("key=")).toEqual({
key: "",
});
});

test("should return empty object for empty input", () => {
expect(parseQueryString("")).toEqual({});
});

test("should use the last value when duplicate keys exist", () => {
expect(parseQueryString("color=red&color=blue")).toEqual({
color: "blue",
});
});

15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new TypeError("Input must be an array");
}

const uniqueItems = [...new Set(items)].sort();

const counts = {};
for (const item of uniqueItems) {
counts[item] = items.filter(x => x === item).length;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Imagine I had the input of ['a', 'a', 'a', ...] - How many times will .filter run? Is that an optimal solution?

}

return counts;
}

module.exports = tally;
23 changes: 22 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,36 @@ const tally = require("./tally.js");
// When passed an array of items
// Then it should return an object containing the count for each unique item

test("tally counts the frequency of each unique 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.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", "a"])).toEqual({
a: 3,
});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error

test("tally throws an error when passed invalid input", () => {
expect(() => tally("a")).toThrow();
});
4 changes: 2 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
function invert(obj) {
const invertedObj = {};

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

return invertedObj;
Expand Down
46 changes: 46 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const invert = require("./invert.js");

/*
Implement a function called invert that swaps the keys and values of an object.

E.g. invert({ x: 10, y: 20 })
// returns { "10": "x", "20": "y" }
*/

// Acceptance criteria:

// Given an empty object
// When passed to invert
// Then it should return an empty object
test("returns an empty object when given an empty object", () => {
expect(invert({})).toEqual({});
});

// Given an object with one property
// When passed to invert
// Then it should swap the key and value
test("inverts an object with one key-value pair", () => {
expect(invert({ a: 1 })).toEqual({
"1": "a",
});
});

// Given an object with multiple properties
// When passed to invert
// Then it should swap all keys and values
test("inverts an object with multiple key-value pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({
"1": "a",
"2": "b",
});
});

// Given an object with string values
// When passed to invert
// Then it should swap the keys and values
test("inverts string values correctly", () => {
expect(invert({ first: "apple", second: "banana" })).toEqual({
apple: "first",
banana: "second",
});
});
Loading