Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
d148137
Updating the file with my rediction
KayanatSuleman Mar 1, 2026
437f3a9
Updating a spelling error
KayanatSuleman Mar 1, 2026
2556a19
Updating the program to correctly display the house number
KayanatSuleman Mar 1, 2026
0503c00
Predicting why the program for author.js will not work
KayanatSuleman Mar 1, 2026
c5af2fc
Correcting the program to log all the object values of author
KayanatSuleman Mar 1, 2026
0155ca2
Ensuring that the correct valyes are logged using node
KayanatSuleman Mar 1, 2026
1f29432
Updating my prediction for recipe.js file
KayanatSuleman Mar 1, 2026
09433a5
Updating the program for reciple.js so that it runs as expected
KayanatSuleman Mar 1, 2026
08b12a9
Adding the implementaion of the program for contains.js
KayanatSuleman Mar 1, 2026
557e87b
Adding test for when object is empty
KayanatSuleman Mar 1, 2026
c79969b
Adding test for when object contains property
KayanatSuleman Mar 1, 2026
3747d91
Adding the other relevant tests and removing duplicated tests
KayanatSuleman Mar 1, 2026
f440646
Adding the program implementation for lookup.js
KayanatSuleman Mar 1, 2026
655fc11
Adding relevant tests
KayanatSuleman Mar 1, 2026
bd478a0
Correcting the program by testing the program in Node REPL
KayanatSuleman Mar 2, 2026
695ad75
Adding a test to cover the empty input condition
KayanatSuleman Mar 2, 2026
fb3189e
Adding a test case for single key/value pairs
KayanatSuleman Mar 2, 2026
3b6e462
Adding a test case for normal multiple pairs
KayanatSuleman Mar 2, 2026
c4ab525
Adding a test case for empty strings
KayanatSuleman Mar 2, 2026
e161802
Adding the implementation of the program for tally.js
KayanatSuleman Mar 2, 2026
d4d6ace
Adding a test case for tallying on an empty array
KayanatSuleman Mar 2, 2026
b6f691e
Adding a test case for counting the freqency of items within an array
KayanatSuleman Mar 2, 2026
34f680a
Adding a test case for a invalid input
KayanatSuleman Mar 2, 2026
efc7a77
Answering questeion A) using node REPL
KayanatSuleman Mar 2, 2026
afc21f6
Adding the answer to question B) using node REPL
KayanatSuleman Mar 2, 2026
e48cbff
Adding the answer for question C) using node REPL to debug and fix th…
KayanatSuleman Mar 2, 2026
ee94815
Correct the question letters for easy readability
KayanatSuleman Mar 2, 2026
bcc383c
Adding the answer for D)
KayanatSuleman Mar 2, 2026
899dadc
Adding the answer for question e)
KayanatSuleman Mar 2, 2026
444d8d9
Adding a file to seperate tests and program logic for clear readibili…
KayanatSuleman Mar 2, 2026
c885402
Adding more test to cover other instances
KayanatSuleman Mar 2, 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
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...
//The issue with this program is that console.log is logging the address but is an object. address[0] only works for arrays.
//As address is an object, we must use a key rather than numeric indexes.
//Also there is no property called "0" inside address.

// 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}`);
7 changes: 5 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Predict and explain first...
// The issue with this program is that for.. of only works on iterable objects.
// Plain JavaScript objects are not iterable, so this will throw a "TypeError".
// To fix this, we can use Object.values(author) which converts the object's value into an array.

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

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
}
9 changes: 7 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// Predict and explain first...
//The issue with this program is that ${recipe} inserts the entire object into the template string.
// When an object is converted to a string, it becomes "[object Object]".
//Instead we would need to access the ingredients array.
//We can loop through recipe.ingredients or use join("\n")
//This will print each ingredients on a new line.

// 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 @@ -11,5 +16,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);
11 changes: 9 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function contains() {}

function contains(object, propertyName) {
if (
typeof object !== "object" ||
object === null ||
Array.isArray(object)) {
return false;
}
return propertyName in object;
}
module.exports = contains;
13 changes: 12 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,27 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("returns false for empty object", () => {
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("returns true when object contains 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("returns false when object does not contain 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("returns false for invalid input (array)", () => {
expect(contains(["a", "b"], "a")).toBe(false);
});
11 changes: 9 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const result = {};
for (const pair of pairs) {
const countryCode = pair[0];
const currencyCode = pair[1];
result[countryCode] = currencyCode;
}

return result;
}

module.exports = createLookup;
8 changes: 7 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
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 input = [['US', 'USD'], ['CA', 'CAD']];
expect(createLookup(input)).toEqual({
US: 'USD',
CA: 'CAD'
});
});

/*

Expand Down
12 changes: 11 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
function parseQueryString(queryString) {
const queryParams = {};

if (queryString.length === 0) {
return queryParams;
}

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const equalsIndex = pair.indexOf("=");

if (equalsIndex === -1) {
queryParams[pair] = "";
continue;
}
const key = pair.slice(0, equalsIndex);
const value = pair.slice(equalsIndex + 1);

queryParams[key] = value;
}

Expand Down
20 changes: 20 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,23 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

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

// Normal single pair
test("parses a single key/value pair", () => {
expect(parseQueryString("a=1")).toEqual({ a: "1" });
});

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

// Empty value should be an empty string
test("handles a key with an empty value", () => {
expect(parseQueryString("a=")).toEqual({ a: "" });
});
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(items) {
if (!Array.isArray(items)){
throw new Error("Input must be an array");
}
const result = {};

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

module.exports = tally;
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ const tally = require("./tally.js");
// 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("counts frequency of items in array", () => {
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("throws error for invalid input", () => {
expect(() => tally("abc")).toThrow();
});
19 changes: 15 additions & 4 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@ 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 }
// The current return value when invert is called with { a : 1 } is { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// When calling invert({ a: 1, b: 2 }); the loop runs twice, the first iteration outputs invertedObj.key = 1l
// The second iterations overwrites the first, so the current value returned is { key: 2 }

// c) What is the target return value when invert is called with {a : 1, b: 2}
// The target output should swap keys and value, therefore the output (using Node REPL) after the fix is { '1': 'a', '2': 'b' }
// After fixing the bug in the code. The values become keys, and the keys become the values.
// Object keys are stored as strings.

// c) What does Object.entries return? Why is it needed in this program?
// d) What does Object.entries return? Why is it needed in this program?
// Object.entries({ a: 1, b: 2}) return [["a", 1]], ["b", 2]]
// It is needed because objects are not iterable with for...of, but the entries array is, so we can loop key/value pairs.

// d) Explain why the current return value is different from the target output
// e) Explain why the current return value is different from the target output
// In the code (before the bug fix) invertedObj.key = value creates a literal property called "key" each time, overwriting it.
// It does not use the variable key/value for dynamic property names, so it can't swap keys and values correctly.

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

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

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

test("returns empty object when given empty object", () => {
expect(invert({})).toEqual({});
});