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
8 changes: 7 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Predict and explain first...

// This will log "My house number is undefined" because
// address[0] is looking for index 0 like it's an array, but address
// is an object. To get a value from an object you need to use the
// key name, not a number. Changing address[0] to address.houseNumber
// will fix it and log "My house number is 42".

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

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
4 changes: 3 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// This code will throw a TypeError: "author is not iterable".
//

// 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 +13,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
12 changes: 10 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
// Predict and explain first...
// This will not log the ingredients correctly. It will print the whole
// recipe object as [object Object] instead of each ingredient on its own line,
// because you're interpolating the entire object (`${recipe}`) instead of
// its properties and the ingredients array items.

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


const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);

for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
10 changes: 9 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function contains() {}
function contains(obj, propName) {
// Return false for non-objects or null
if (obj === null || typeof obj !== "object" || Array.isArray(obj)) {
return false;
}

// Check if the object has the property as its own key
return Object.hasOwn(obj, propName);
}

module.exports = contains;
22 changes: 21 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,55 @@
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");
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("contains returns true for 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("contains returns false for 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("contains with invalid parameters returns false", () => {
expect(contains([], "a")).toBe(false);
});
9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function createLookup() {
function createLookup(countryCurrencyPairs) {
// implementation here
const lookup = {};

for (const [countryCode, currencyCode] of countryCurrencyPairs) {
lookup[countryCode] = currencyCode;
}

return lookup;
}

module.exports = createLookup;
25 changes: 24 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,54 @@
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 countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
["GB", "GBP"],
];

const result = createLookup(countryCurrencyPairs);

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


/*


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:
{
Expand Down
20 changes: 19 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,32 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}

const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
if (pair === "") {
continue;
}

const indexOfEquals = pair.indexOf("=");
let key;
let value;

if (indexOfEquals === -1) {
// no "=", treat whole pair as key with empty value
key = pair;
value = "";
} else {
key = pair.slice(0, indexOfEquals);
value = pair.slice(indexOfEquals + 1);
}

queryParams[key] = value;
}

return queryParams;
}


module.exports = parseQueryString;
24 changes: 24 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,34 @@
// 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")


test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
});
});

test("parses empty querystring as empty object", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses single key with empty value", () => {
expect(parseQueryString("flag=")).toEqual({ flag: "" });
});

test("parses key with no =", () => {
expect(parseQueryString("flag")).toEqual({ flag: "" });
});

test("parses multiple key value pairs", () => {
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
});

test("later duplicate keys overwrite earlier ones", () => {
expect(parseQueryString("a=1&a=2")).toEqual({ a: "2" });
});


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

module.exports = tally;
const counts = {};

for (const item of items) {
counts[item] = (counts[item] || 0) + 1;
}

return counts;
}

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
@@ -1,5 +1,6 @@
const tally = require("./tally.js");


/**
* tally array
*
Expand All @@ -14,21 +15,39 @@ const tally = require("./tally.js");
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
*/


// Acceptance criteria:


// 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");
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 each unique item", () => {
expect(tally(["a"])).toEqual({ a: 1 });
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
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 throws on invalid input", () => {
expect(() => tally("not an array")).toThrow();
});



22 changes: 21 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
// Let's define how invert should work


// Given an object
// When invert is passed this object
// Then it should swap the keys and values in the object


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


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 }
// Answer: { "1": "a" }


// b) What is the current return value when invert is called with { a: 1, b: 2 }
// Answer: { "1": "a", "2": "b" }


// c) What is the target return value when invert is called with {a : 1, b: 2}
// Answer: { "1": "a", "2": "b" }


// c) What does Object.entries return? Why is it needed in this program?
// Answer: Object.entries(obj) returns an array of [key, value] pairs,
// It is needed so we can loop over the object and easily get both key and value in the for...of loop using array destructuring: [key, value].


// d) Explain why the current return value is different from the target output
// Answer: In this implementation, the current return value already matches the target output, because we correctly use the value as the new key
// and the key as the new value: invertedObj[value] = key.


// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// Answer: No changes needed to the implementation; it already works as required.

Loading
Loading