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
5 changes: 4 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Predict and explain first...
//I guess that the code will log "My house number is undefined" because address[0] isn't a valid way
// to access the houseNumber property. When we do address[0], JS looks for a property literally
// named "0", because bracket notation treats 0 as the string "0"

// This code should log out the houseNumber from the address object
// but it 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"]}`);
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...
// for...of only works on arrays. An object isn’t an array, so we can’t loop over it directly.
// To find values from the object we need to convert it to an array of values first.

// 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);
}
4 changes: 3 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// I think it incorrectly tries to log ingredients - this should be an array,
// but there's missed .ingredients after the object name, so it will likely log something like "[object Object]" instead of the actual ingredients.

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -12,4 +14,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join(", ")}`);
5 changes: 4 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
function contains() {}
function contains(obj, prop) {
if (Object.keys(obj).length === 0) return false;
return Object.prototype.hasOwnProperty.call(obj, prop);
}

module.exports = contains;
45 changes: 29 additions & 16 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,32 @@ as the object doesn't contains a key of 'c'
// 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", () => {
// Given an empty object
// When passed to contains
// Then it should return 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 on object with existing property returns true", () => {
expect(contains({ a: 1, d: 4 }, "d")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains on object with non-existent property returns false", () => {
expect(contains({ a: 1, b: 4 }, "c")).toBe(false);
});

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

module.exports = createLookup;
41 changes: 39 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,42 @@ It should return:
'CA': 'CAD'
}
*/

describe("createLookup", () => {
test("should create a lookup object from country-currency pairs", () => {
const countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
["GB", "GBP"],
["FR", "EUR"],
];

const expectedLookup = {
US: "USD",
CA: "CAD",
GB: "GBP",
FR: "EUR",
};

const lookup = createLookup(countryCurrencyPairs);
expect(lookup).toEqual(expectedLookup);
});

test("should return an empty object when given an empty array", () => {
const countryCurrencyPairs = [];
const expectedLookup = {};

const lookup = createLookup(countryCurrencyPairs);
expect(lookup).toEqual(expectedLookup);
});

test("should handle single pair correctly", () => {
const countryCurrencyPairs = [["CA", "CAD"]];
const expectedLookup = {
CA: "CAD",
};

const lookup = createLookup(countryCurrencyPairs);
expect(lookup).toEqual(expectedLookup);
});
});
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, ...rest] = pair.split("=");
queryParams[key] = rest.join("=");
}

return queryParams;
Expand Down
35 changes: 31 additions & 4 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@
// 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",
describe("parseQueryString", () => {
test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
equation: "x=y+1",
});
});

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

test("parses multiple key-value pairs", () => {
expect(parseQueryString("name=Jane&age=37&city=New+York")).toEqual({
name: "Jane",
age: "37",
city: "New+York",
});
});

test("parses key with empty value", () => {
expect(parseQueryString("key1=&key2=value2")).toEqual({
key1: "",
key2: "value2",
});
});

test("parses value with special characters", () => {
expect(parseQueryString("greeting=Hello%2C+World%21")).toEqual({
greeting: "Hello%2C+World%21",
});
});
});
12 changes: 11 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array");
}

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

module.exports = tally;
40 changes: 27 additions & 13 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,33 @@ const tally = require("./tally.js");

// 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
describe("tally", () => {
// 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");
// Given an empty array
// When passed to tally
// Then it should return 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
// 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", "g", "b", "a", "b", "a"])).toEqual({
a: 4,
b: 2,
g: 1,
});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws an error for invalid input", () => {
expect(() => tally({})).toThrow("Input must be an array");
});
});
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;
}
module.exports = invert;

// a) What is the current return value when invert is called with { a : 1 }
// I think its { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// I think it should be { key: 2 }

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

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries returns an array of a given object's own enumerable string-keyed property [key, value] pairs.
// It is needed in this program to iterate over the key-value pairs of the object to swap them.

// d) Explain why the current return value is different from the target output
// I think its different because in the line invertedObj.key = value;
// the key is being set as the string "key" instead of using the variable key.

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

describe("invert", () => {
// Given an object with one key-value pair
// When invert is called with this object
// Then it should return an object with the key and value swapped
test("invert swaps key and value for single pair", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

// Given an object with multiple key-value pairs
// When invert is called with this object
// Then it should return an object with the keys and values swapped
test("invert swaps keys and values for multiple pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});

// Given an object with string values
// When invert is called with this object
// Then it should return an object with the keys and values swapped
test("invert swaps keys and values for string values", () => {
expect(invert({ x: "apple", y: "banana" })).toEqual({
apple: "x",
banana: "y",
});
});

// Given an object with mixed value types
// When invert is called with this object
// Then it should return an object with the keys and values swapped
test("invert swaps keys and values for mixed value types", () => {
expect(invert({ a: 1, b: "two", c: 3 })).toEqual({
1: "a",
two: "b",
3: "c",
});
});
});