Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
653f018
Added in prediction.
HM-127BTY Nov 11, 2026
4a888c9
fixed the error of [0] to ["houseNumber"], i think I can also use ${a…
HM-127BTY Nov 11, 2026
809ccc7
done the prediction
HM-127BTY Nov 11, 2026
b8b4cc0
fixed the for...loop
HM-127BTY Nov 11, 2026
fe145bf
done predictions
HM-127BTY Nov 11, 2026
3910437
Fixed the code and using \n for the new lines.
HM-127BTY Nov 11, 2026
69ec2cb
building code, start with thrown error.
HM-127BTY Nov 11, 2026
a1b225a
fixing errors
HM-127BTY Nov 11, 2026
3ae2c2a
built test for checking true for object contains property
HM-127BTY Nov 11, 2026
8231232
added in a true or false using "in"
HM-127BTY Nov 11, 2026
cfaf2b5
removed unneeded comments
HM-127BTY Nov 11, 2026
5a1735e
added test for non-existent property name
HM-127BTY Nov 11, 2026
f411077
test for empty object
HM-127BTY Nov 11, 2026
e0082d8
added in final test for throwing error for not containing object
HM-127BTY Nov 11, 2026
e07a535
Added in test required and fixed errors in code
HM-127BTY Nov 11, 2026
781d48e
done code for tally, need to look into it more.
HM-127BTY Nov 11, 2026
47dcb5d
started with testing, done the first on with just one a
HM-127BTY Nov 11, 2026
0cd4ffe
tested empty array
HM-127BTY Nov 11, 2026
7747e05
test for duplicate items.
HM-127BTY Nov 11, 2026
fdd63d0
added in code and test for thrown error.
HM-127BTY Nov 11, 2026
ea97ab9
fixed the code to run the first test .
HM-127BTY Nov 12, 2026
72da3ae
added in code for 2nd test and cleaned up some code.
HM-127BTY Nov 12, 2026
fc2ec59
edited code to have decodeURIComponent
HM-127BTY Nov 12, 2026
ec001df
all tests are working
HM-127BTY Nov 12, 2026
192ed35
answered questions now working on code and tests for the tasks.
HM-127BTY Nov 12, 2026
3467946
fixed code and added in first test for correct invert
HM-127BTY Nov 12, 2026
8ee667e
added test for 2 objects and 2 properties
HM-127BTY Nov 12, 2026
0066a76
test for empty object
HM-127BTY Nov 12, 2026
9a3ac64
added tests for empty strings and objects
HM-127BTY Nov 12, 2026
80a82ec
fixed typing errors and make sure test discriptions are correct to tests
HM-127BTY Nov 12, 2026
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: 2 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

// it returns undefined as it can't find 0 because this is an object its iterating with and not an array
// 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 +12,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,5 +1,6 @@
// Predict and explain first...

// value is undefined, its not seen in the code at all.
// Ah error said it is not iterable, i didnt spot the for...of loop there, so its not seeing the value as an option in the object.
// 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);
}
5 changes: 3 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// it does not make a new line as just pressing enter wont work for that.
// I was wrong, ingredients is showing [object Object], as it wont log out a full object like this.
// 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 +13,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(object, property) {
if (Array.isArray(object)) {
throw new Error("contains requires an object");
}
return property in object;
}

module.exports = contains;
16 changes: 15 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,36 @@ as an array isn't an object
// 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
test("When passed an object and a property name, returns true", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("Given an 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("Given an object with properties, returns true", () => {
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("Given an object with a non-existent property name, returns false", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// Given a value that isn't an object - an array, a string, a number,
// null, or no argument at all
// When passed to contains
// Then it should throw Error("contains requires an object")
// (careful: typeof [] and typeof null are both "object")
test("Given an array, throws an error 'contains requires an object'", () => {
expect(() => contains([])).toThrow("contains requires an object");
});
5 changes: 3 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = Object.fromEntries(countryCurrencyPairs);
return lookup;
}

module.exports = createLookup;
13 changes: 12 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
// test.todo("creates a country currency code lookup for multiple codes");

/*

Expand Down Expand Up @@ -33,3 +33,14 @@ It should return:
'CA': 'CAD'
}
*/
test("Given an array of arrays representing country code and currency code pairs should return in object", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({
US: "USD",
CA: "CAD",
});
});
8 changes: 7 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,13 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

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

const key = decodeURIComponent(parts[0]);
const value = decodeURIComponent(parts.slice(1).join("="));
queryParams[key] = value;
}

Expand Down
2 changes: 1 addition & 1 deletion Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Below are some test cases the implementation doesn't handle well.
// Fix the implementation for these tests, 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("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
Expand Down
16 changes: 15 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
function tally() {}
function tally(array) {
const lookup = {};
if (!Array.isArray(array)) {
throw new Error("tally requires an array");
}

for (const item of array) {
if (lookup[item]) {
lookup[item] += 1;
} else {
lookup[item] = 1;
}
}
return lookup;
}

module.exports = tally;
12 changes: 12 additions & 0 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,28 @@ const tally = require("./tally.js");
// 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("When passed an array of items should return object containing count for each", () => {
expect(tally(["a"])).toEqual({ a: 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");
test("When passed an empty array should return 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("When passed an array of duplicate items should return object containing correct count", () => {
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
});

// Given an invalid input like a string, a number, or no argument at all
// When passed to tally
// Then it should throw Error("tally requires an array")
test("WGiven an invalid input should throw error 'tally requires an array'", () => {
expect(() => tally("im a string")).toThrow("tally requires an array");
});
13 changes: 11 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ 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 }
// console.log(invert({ a: 1 }));
// It is: { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// console.log(invert({ a: 1, b: 2 }));
// it is: { key: 2 }

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

// c) What does Object.entries return? Why is it needed in this program?
// it is containing each key/value pair
// it takes an object and turns it into an array

// d) Explain why the current return value is different from the target output
// its because of "invertedObj.key = value;" each time it is running its writes
// to the same property of "key" basically overwriting the previous one.

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

// inverting original input
test("Inverting original input of 1 property", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

// inverting original input of 2 objects and 2 properties
test("Inverting original input of 2 properties", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});

// inverting an empty object
test("Inverting original input correctly", () => {
expect(invert({})).toEqual({});
});

// Inverting an object with an empty string as a value
test("Inverting an object with an empty string as a value", () => {
expect(invert({ a: "" })).toEqual({ "": "a" });
});

// Inverting an object with an empty string as a key
test("Inverting an object with an empty string as a key", () => {
expect(invert({ "": 1 })).toEqual({ 1: "" });
});

// Inverting an object with multiple properties sharing the same value
test("Inverting an object with multiple properties sharing the same value", () => {
expect(invert({ a: 2, b: 2, c: 2 })).toEqual({ 2: "a", 2: "b", 2: "c" });
});
Loading