Skip to content
Closed
Show file tree
Hide file tree
Changes from 8 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
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// Trying to access the address object using array notation `address[0]` instead of the property name `address.houseNumber`.

// 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 +14,4 @@ const address = {
postcode: "XYZ 123",
};

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

// For..of loop only supports iterable objects (arrays), not objects
// Should use For..in instead
// Value needs to be accessed from the object using bracket notation

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

Expand All @@ -11,6 +15,6 @@ const author = {
alive: true,
};

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

// Logging the entire recipe object instead of iterating through each element in the ingredients array

// 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 @@ -12,4 +14,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
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(obj, property) {
if (obj === null || obj === undefined) {
return false;
}

return property in obj;
Comment thread
jakr-s marked this conversation as resolved.
Outdated
}

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

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
describe("contains", () => {
test("contains returns true for existing property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

test("contains on empty object returns false", () => {
expect(contains({}, "a")).toBe(false);
});

test("contains returns true for multiple properties", () => {
expect(contains({ name: "John", age: 30, city: "NY" }, "age")).toBe(true);
});

test("contains returns false for non-existent property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

test("contains returns false for non-object types", () => {
expect(contains(null, "a")).toBe(false);
expect(contains(undefined, "a")).toBe(false);
});
});
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(arr) {
return Object.fromEntries(arr);
}

module.exports = createLookup;
55 changes: 22 additions & 33 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,24 @@
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'
}
*/
describe("createLookup", () => {
test("creates a country currency code lookup for multiple codes", () => {
const input = [
["US", "USD"],
["CA", "CAD"],
];
const result = createLookup(input);
expect(result).toEqual({ US: "USD", CA: "CAD" });
});

test("creates a lookup with a single code pair", () => {
const input = [["GB", "GBP"]];
const result = createLookup(input);
expect(result).toEqual({ GB: "GBP" });
});

test("creates an empty lookup with an empty array", () => {
const input = [];
const result = createLookup(input);
expect(result).toEqual({});
});
});
4 changes: 2 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
const [key, ...valueParts] = pair.split("=");
queryParams[key] = valueParts.join("=");
Comment thread
jakr-s marked this conversation as resolved.
Outdated
}

return queryParams;
Expand Down
16 changes: 14 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
// 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("handles empty string", () => {
expect(parseQueryString("")).toEqual({});
});

test("handles key without value", () => {
expect(parseQueryString("key1=value1&key2=&key3=value3")).toEqual({
key1: "value1",
key2: "",
key3: "value3",
});
});
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error();
}

let result = {};
Comment thread
jakr-s marked this conversation as resolved.
Outdated

for (const element of arr) {
if (element in result) {
result[element] += 1;
} else {
result[element] = 1;
}
}
return result;
}

module.exports = tally;
47 changes: 18 additions & 29 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,23 @@
const tally = require("./tally.js");

/**
* tally array
*
* In this task, you'll need to implement a function called tally
* that will take a list of items and count the frequency of each item
* in an array
*
* For example:
*
* tally(['a']), target output: { a: 1 }
* tally(['a', 'a', 'a']), target output: { a: 3 }
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
*/
describe("tally", () => {
test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

// Acceptance criteria:
test("tally with single item returns object with count of 1", () => {
expect(tally(["a"])).toEqual({ a: 1 });
});

// 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("tally with multiple items returns counts for each", () => {
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
expect(tally(["x", "y", "x", "z", "y", "x"])).toEqual({ x: 3, y: 2, z: 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");

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally with invalid input throws an error", () => {
expect(() => tally("invalid")).toThrow();
expect(() => tally(null)).toThrow();
expect(() => tally(undefined)).toThrow();
});
});
10 changes: 9 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

// 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 given object
// It is needed so that both the key and value can be destructured from the object and reassigned in the for..of loop

// d) Explain why the current return value is different from the target output
// Dot notation is being used in the for..of loop, bracket notation should be used instead as it will become a computed property
// value and key should also be swapped inside the for..of loop

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
module.exports = invert;
27 changes: 27 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const invert = require("./invert.js");

describe("invert", () => {
test("inverts an empty object", () => {
const input = {};
const result = invert(input);
expect(result).toEqual({});
});

test("inverts a single key-value pair", () => {
const input = { a: 1 };
const result = invert(input);
expect(result).toEqual({ 1: "a" });
});

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

test("inverts an object with string values", () => {
const input = { x: "hello", y: "world" };
const result = invert(input);
expect(result).toEqual({ hello: "x", world: "y" });
});
});