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: 3 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// I predict it will cause an error at console.log because it is wanting to print address[0] but
// address is not an array it is an object...

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +13,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
5 changes: 3 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// the for ... of only works when iterating through data and this is not what is required
// it will cause an error

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

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
4 changes: 2 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
// Didn't put recipe.ingredients in console.log so will cause error because cannot access recipe

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

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
7 changes: 6 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}
function contains(obj, key) {
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
throw new Error("Input must be a non-null object and not an array");
}
return key in obj; // simply checks if the key exists in the object
}

module.exports = contains;
59 changes: 39 additions & 20 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,42 @@ 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.each([
// 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

{ input: [{}, "a"], expected: false },

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

{ input: [{ a: 1, b: 2 }, "a"], expected: true },

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

{ input: [{ a: 1, b: 2 }, "c"], expected: false },

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
])("finds object contains properties", ({ input, expected }) => {
expect(contains(...input)).toBe(expected);
});
test("throws error when input is null or an array", () => {
expect(() => contains(null, "a")).toThrow(
"Input must be a non-null object and not an array"
);
expect(() => contains(["a", "b"], "a")).toThrow(
"Input must be a non-null object and not an array"
);
});
});
11 changes: 8 additions & 3 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function createLookup() {
// implementation here
}
function createLookup(CountryCurrencyPairs) {
const result = {};

for (const pair of CountryCurrencyPairs) {
const [country, currency] = pair;
result[country] = currency;
}
return result;
}
module.exports = createLookup;
16 changes: 13 additions & 3 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
const createLookup = require("./lookup.js");

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

/*
const result = createLookup(input);

Create a lookup object of key value pairs from an array of code pairs
expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});

/*Create a lookup object of key value pairs from an array of code pairs

Acceptance Criteria:

Expand Down
3 changes: 2 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

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

Expand Down
18 changes: 18 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

const parseQueryString = require("./querystring.js")


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

test("parses a single key-value pair", () => {
expect(parseQueryString("name=John")).toEqual({
name: "John",
});
});

test("parses multiple key-value pairs", () => {
expect(parseQueryString("name=John&age=30")).toEqual({
name: "John",
age: "30",
});
});

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
Expand Down
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("Input must be an array"); //checks input is an array if not throws error
}

const result = Object.create(null); //creates an empty object without a prototype to avoid issues with inherited properties

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

return result;
}
module.exports = tally;
38 changes: 26 additions & 12 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,29 @@ const tally = require("./tally.js");
// 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 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
describe("tally", () => {
test.each([
// Given an empty array
// When passed to tally
// Then it should return an empty object

{ input: [], expected: {} },

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

{ input: ["a", "b", "a"], expected: { a: 2, b: 1 } },

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

{ input: ["toString", "toString"], expected: { toString: 2 } }, // new test
])("counts items correctly", ({ input, expected }) => {
expect(tally(input)).toEqual(expected);
});
test("throws error for invalid input", () => {
expect(() => tally("not an array")).toThrow("Input must be an array");
});
});
41 changes: 34 additions & 7 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,51 @@

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

function invert(obj) {
const invertedObj = {};
//function invert(obj) {
// const invertedObj = {};

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

return invertedObj;
}
// 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?
/* Object.entries returns an array of the object in pairs [key, value]. It is needed in this program
to iterate through the object and access both the key and value at the same time, which is necessary
for swapping them in the inverted object. */

// d) Explain why the current return value is different from the target output

/*The current return value is different from the target output because the implementation of the invert
function is incorrect. Instead of using the variable key to set the property in the inverted object,
it is using the literal string "key". This means that every time a key-value pair is processed,
it overwrites the same property "key" in the inverted object, resulting in only the last key-value
pair being stored. To fix this, we need to use bracket notation to set the property in the inverted object
using the value as the key and the key as the value.*/

// e) Fix the implementation of invert (and write tests to prove it's fixed!)

function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

console.log(invert({ a: 1 })); // { "1": "a" }
console.log(invert({ a: 1, b: 2 })); // { "1": "a", "2": "b" }
console.log(invert({ x: 10, y: 20 })); // { "10": "x", "20": "y" }
Loading
Loading