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")}`);
6 changes: 5 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function contains() {}
function contains(obj, key) {
if (!obj) return false;

return Object.prototype.hasOwnProperty.call(obj, key);
}

module.exports = contains;
39 changes: 20 additions & 19 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,36 @@
const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
particular property

E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'

E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

// Acceptance criteria:

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

// Given an empty object
// When passed to contains
// When passed to contains with any key
// Then it should return false
test.todo("contains on empty object returns false");
test("returns false for an empty object", () => {
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 for 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 for a non-existent 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 for an array", () => {
expect(contains([], "a")).toBe(false);
});

// Given invalid parameters like null
// When passed to contains
// Then it should return false or throw an error
test("returns false for null", () => {
expect(contains(null, "a")).toBe(false);
});
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const result = {};
for (let i = 0; i < pairs.length; i++) {
const [country, currency] = pairs[i];

result[country] = currency;
}
return result;
}

module.exports = createLookup;
54 changes: 21 additions & 33 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,23 @@
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

Acceptance Criteria:

Given
- An array of arrays representing country code and currency code pairs
e.g. [['US', 'USD'], ['CA', 'CAD']]

When
- createLookup function is called with the country-currency array as an argument

Then
- It should return an object where:
- The keys are the country codes
- The values are the corresponding currency codes

Example
Given: [['US', 'USD'], ['CA', 'CAD']]

When
createLookup(countryCurrencyPairs) is called

Then
It should return:
{
'US': 'USD',
'CA': 'CAD'
}
*/
test("creates a country currency code lookup for multiple codes", () => {
const input = [
["US", "USD"],
["CA", "CAD"],
["GB", "GBP"],
];
const expected = {
US: "USD",
CA: "CAD",
GB: "GBP",
};

expect(createLookup(input)).toEqual(expected);
});

test("creates a country currency code lookup for an empty array", () => {
const input = [];
const expected = {};

expect(createLookup(input)).toEqual(expected);
});
28 changes: 18 additions & 10 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const result = {};
if (!queryString) return result;

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

return queryParams;
pairs.forEach((pair) => {
if (!pair) return;

const index = pair.indexOf("=");

if (index === -1) {
result[pair] = "";
} else {
const key = pair.slice(0, index);
const value = pair.slice(index + 1);
result[key] = value;
}
});

return result;
}

module.exports = parseQueryString;
44 changes: 42 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,50 @@
// 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 multiple key-value pairs", () => {
expect(parseQueryString("name=Alice&age=30&city=NY")).toEqual({
name: "Alice",
age: "30",
city: "NY",
});
});

test("returns an empty object for an empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

test("handles key without equals sign", () => {
expect(parseQueryString("key1=value1&key2&key3=value3")).toEqual({
key1: "value1",
key2: "",
key3: "value3",
});
});

test("handles multiple '=' in value", () => {
expect(parseQueryString("data=a=b=c")).toEqual({
data: "a=b=c",
});
});

test("handles empty key", () => {
expect(parseQueryString("=value")).toEqual({
"": "value",
});
});

test("handles mixed querystring", () => {
expect(parseQueryString("a=1&b&c=3=d")).toEqual({
a: "1",
b: "",
c: "3=d",
});
});
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(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const result = {};

items.forEach((item) => {
if (result[item]) {
result[item]++;
} else {
result[item] = 1;
}
});

return result;
}

module.exports = tally;
10 changes: 9 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ 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 on an array with duplicate items returns correct counts", () => {
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 on an invalid input throws an error", () => {
expect(() => tally("not an array")).toThrow("Input must be an array");
});
26 changes: 25 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
Expand All @@ -27,3 +27,27 @@ function invert(obj) {
// d) Explain why the current return value is different from the target output

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
const invert = require("./invert");

test("inverts a simple object", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("inverts an object with multiple key-value pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({
1: "a",
2: "b",
});
});

test("returns empty object when given an empty object", () => {
expect(invert({})).toEqual({});
});

test("handles non-string values as keys in the inverted object", () => {
expect(invert({ a: 1, b: true, c: null })).toEqual({
1: "a",
true: "b",
null: "c",
});
});
Loading