Skip to content
Closed
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
745ed77
Predict and explain the code output; fix code where needed
Iswanna Nov 10, 2025
b63e8a8
Predict and explain the output, and modify the loop from for...in to …
Iswanna Nov 10, 2025
554168f
Predict output and fix code to display each ingredient value on a new…
Iswanna Nov 11, 2025
cf6dae4
Add 'Answer:' clarification comments for code behavior
Iswanna Nov 12, 2025
0601e1a
Add test cases for 'contains' function covering valid, empty, and inv…
Iswanna Nov 13, 2025
d5f1bae
Implement 'contains' function:
Iswanna Nov 13, 2025
fbc10ec
Add additional tests to each scenario to verify 'contains' function b…
Iswanna Nov 14, 2025
70dc26c
Fix indentation inconsistencies in 'contains' function
Iswanna Nov 14, 2025
829f63d
Remove .todo and add unit test for createLookup function
Iswanna Nov 14, 2025
0ffc147
Implement createLookup function to convert array of pairs into lookup…
Iswanna Nov 14, 2025
99df5b6
Fix parseQueryString so key-value pairs with '=' in values are parsed…
Iswanna Nov 14, 2025
60f6d21
Fix parseQueryString to throw TypeError for non-string input
Iswanna Nov 14, 2025
7bb2074
Add test cases covering edge cases for parseQueryString
Iswanna Nov 14, 2025
a2efcdf
Add test cases for tally function to cover empty input, duplicates, a…
Iswanna Nov 14, 2025
156dfd9
Add function implementation to count array items and handle invalid i…
Iswanna Nov 14, 2025
1a47dc3
Add answers to questions and fix invert() implementation to produce c…
Iswanna Nov 15, 2025
9f60b8b
Add test file for invert and test that empty object returns empty obj…
Iswanna Nov 15, 2025
acf14dd
Add the module.exports statement so the invert function can be import…
Iswanna Nov 15, 2025
92e7434
Add test to verify invert returns an empty object for empty input
Iswanna Nov 16, 2025
7ab4f2d
Add test to verify invert correctly swaps a single key-value pair
Iswanna Nov 16, 2025
c2f3fd2
Add test to verify invert swaps multiple key-value pairs
Iswanna Nov 16, 2025
a6a37dc
Add test to verify invert throws a TypeError when given invalid input
Iswanna Nov 16, 2025
abaa0f8
Fix invert to throw an error for non-plain object input
Iswanna Nov 16, 2025
bcdb270
Add additional tests for empty, single, and multiple key-value pairs,…
Iswanna Nov 16, 2025
399fbc8
Use Object.create(null) in tally to avoid inherited properties like t…
Iswanna Nov 21, 2025
3685652
Add test case demonstrating tally must handle items like "toString" w…
Iswanna Nov 21, 2025
5fcad91
Add conditional decode step for safely handling encoded query strings
Iswanna Nov 21, 2025
224597c
Add tests to ensure parseQueryString handles encoded spaces and speci…
Iswanna Nov 21, 2025
3578991
Refactor contains test to remove repetition using a loop over invalid…
Iswanna Nov 21, 2025
0f518e2
Replace loop with Array.prototype.join() to print ingredients in one …
Iswanna Nov 21, 2025
3f29b47
Fix parseQueryString to decode keys and values safely after splitting…
Iswanna Nov 21, 2025
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
3 changes: 2 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// Answer: when the console.log runs, it will return `My house number is undefined` because `0` is not a key in the object so its value will be undefined

// 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"]}`);
6 changes: 4 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// Answer: When the code runs, the console.log will output an error in the terminal because you can not use for...of loop for an object,
// instead, you use for...in loop

// 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) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}
3 changes: 2 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// Answer: When the code runs, `${recipe}` part of the console.log expression will output the string `[object Object]`

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

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
8 changes: 7 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(obj, propertyName) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
throw new TypeError("Invalid input: obj must be a plain object");
}

return obj.hasOwnProperty(propertyName);
}

module.exports = contains;
35 changes: 34 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,53 @@ as the object doesn't contains a key of 'c'
// 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("Given an input of an object with a property name, returns true if the object contains the property", () => {
const objInput = { a: 1 };
expect(contains(objInput, "a")).toBe(true);
expect(contains(objInput, "t")).toBe(false);
});

// 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, when passed to contains, returns false", () => {
const objInput = {};
expect(contains(objInput, "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, when passed to contains with an existing property name, returns true", () => {
const objInput = { name: "Alice", age: 25 };
expect(contains(objInput, "age")).toBe(true);

const edgeCaseInput = { 1: "one", "": "empty" };
expect(contains(edgeCaseInput, "")).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 properties, when passed to contains with a non-existent property name, returns false", () => {
const objInput = { name: "Peter", age: 48 };
expect(contains(objInput, "gender")).toBe(false);

const objInput1= { name: "Tayo", age: 10 };
expect(contains(objInput1, "location")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("Given invalid parameters, contains throws a TypeError", () => {
const invalidInputs = ["hello", null, undefined, 123, true, [], () => {}];

invalidInputs.forEach(input => {
expect(() => contains(input, "a")).toThrow(
new TypeError("Invalid input: obj must be a plain object")
);
});
});


8 changes: 7 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
function createLookup() {
function createLookup(arrayLookUp) {
const objLookUp = {};

for (const [country, currency] of arrayLookUp) {
objLookUp[country] = currency;
}
return objLookUp;
// implementation here
}

Expand Down
12 changes: 11 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
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 currencyArray = [
["US", "USD"],
["CA", "CAD"],
];
const expectedResult = {
US: "USD",
CA: "CAD",
};
expect(createLookup(currencyArray)).toEqual(expectedResult);
});

/*

Expand Down
23 changes: 22 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
function parseQueryString(queryString) {
if (typeof queryString !== "string") {
throw new TypeError("Invalid input: Input must be a string");
}
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}

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

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

if (index === -1) {
key = pair;
value = "";
} else {
key = pair.slice(0, index);
value = pair.slice(index + 1);
}

try {
key = decodeURIComponent(key);
value = decodeURIComponent(value);
} catch (err) {
throw new Error("Invalid URL-encoded string");
}

queryParams[key] = value;
}

Expand Down
76 changes: 74 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,82 @@
// 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",
equation: "x=y+1",
});
});

test("handles empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

test("handles empty key", () => {
expect(parseQueryString("=value")).toEqual({
"": "value",
});
});

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

test("parses values with spaces or special characters", () => {
expect(parseQueryString("name=John%20Doe&city=New+York")).toEqual({
name: "John Doe",
city: "New+York",
});
expect(parseQueryString("tags%5B%5D=hello%20world")).toEqual({
"tags[]": "hello world",
});
});

test("throws TypeError for non-string input", () => {
expect(() => parseQueryString(null)).toThrow(TypeError);
expect(() => parseQueryString(null)).toThrow(
"Invalid input: Input must be a string"
);

expect(() => parseQueryString(undefined)).toThrow(TypeError);
expect(() => parseQueryString(undefined)).toThrow(
"Invalid input: Input must be a string"
);

expect(() => parseQueryString(123)).toThrow(TypeError);
expect(() => parseQueryString(123)).toThrow(
"Invalid input: Input must be a string"
);

expect(() => parseQueryString({})).toThrow(TypeError);
expect(() => parseQueryString({})).toThrow(
"Invalid input: Input must be a string"
);
});

test("handles repeated keys, last value wins", () => {
expect(parseQueryString("a=1&a=2")).toEqual({
a: "2",
});
});

test("handles trailing '&'", () => {
expect(parseQueryString("a=1&b=2&")).toEqual({
a: "1",
b: "2",
"": "", // empty pair after trailing &
});
});

test("handles leading '&'", () => {
expect(parseQueryString("&a=1&b=2")).toEqual({
"": "", // empty pair before leading &
a: "1",
b: "2",
});
});
18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new TypeError("Invalid input: Input must be an array");
}

const result = Object.create(null);

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

return result;
}

module.exports = tally;
19 changes: 18 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,33 @@ 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("tally when passed an array of items, returns an object containing counts for each unique item", () => {
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("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("when an array with duplicate items is passed to tally", () => {
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("when an invalid input is passed to tally, throws an error", () => {
expect(() => tally("house")).toThrow(
new TypeError("Invalid input: Input must be an array")
);
});

test("tally handles items that match inherited object properties", () => {
expect(tally(["toString", "toString"])).toEqual({ toString: 2 });
});
26 changes: 16 additions & 10 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,28 @@

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

if (Object.prototype.toString.call(obj) !== "[object Object]") {
throw new TypeError("Invalid input: Input must be a plain object");
}
for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
invertedObj[value] = key;
}

return invertedObj;
}

// a) What is the current return value when invert is called with { a : 1 }
module.exports = invert;

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

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

// c) What does Object.entries return? Why is it needed in this program?

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

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// Answer: { '1': 'a', '2': 'b' }
// d) What does Object.entries return? Why is it needed in this program?
// Object.entries(obj) returns an array of [key, value] pairs, like: [ [ 'a', 1 ], [ 'b', 2 ] ]
// e) Explain why the current return value is different from the target output
// Answer: The current return value is different from the target output because `invertedObj.key`
// uses the literal string "key" instead of the variable key. To use the variable's value as the property name,
// we must use bracket notation: invertedObj[value] = key.
// f) Fix the implementation of invert (and write tests to prove it's fixed!)
47 changes: 47 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const invert = require("./invert.js");

test("Given an empty object, return an empty object", () => {
expect(invert({})).toEqual({});
});

test("Given a single key-value pair object, returns a swap value", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
expect(invert({ 1: 1 })).toEqual({ 1: "1" });
expect(invert({ key: true })).toEqual({ true: "key" });
});

test("Given multiple key-value pairs object, returns a swap value", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
expect(invert({ 1: "a", 2: "b" })).toEqual({ a: "1", b: "2" });
expect(invert({ a: 1, b: "x", c: true })).toEqual({
1: "a",
x: "b",
true: "c",
});
expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" });
expect(invert({ a: 1, b: 1, c: 1 })).toEqual({ 1: "c" });
});

test("Given an input that is not a plain object, throws an error", () => {
expect(() => invert(null)).toThrowError(
new TypeError("Invalid input: Input must be a plain object")
);
expect(() => invert(undefined)).toThrowError(
new TypeError("Invalid input: Input must be a plain object")
);
expect(() => invert(true)).toThrowError(
new TypeError("Invalid input: Input must be a plain object")
);
expect(() => invert(null)).toThrowError(
new TypeError("Invalid input: Input must be a plain object")
);
});

test("handles nested objects and arrays as values", () => {
const nestedObj = { a: { x: 1 } };
const nestedArray = { b: [1, 2, 3] };
// Nested object key becomes "[object Object]"
expect(invert(nestedObj)).toEqual({ "[object Object]": "a" });
// Array key becomes "1,2,3"
expect(invert(nestedArray)).toEqual({ "1,2,3": "b" });
});