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
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...

// ${address[0]} is trying to access a property that does not exist
// 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"]}`);
6 changes: 3 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

// for...of is trying to iterate a non iterable 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 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}
Comment on lines +14 to 16
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this code output the property values such as "Zadie", "Smith", ...?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, i have update to output the values rather the keys

4 changes: 2 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Predict and explain first...

// ${recipe} is trying to log an object as string and javascript does not have a default conversion of object to string it logs as [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
// How can you fix it?
Expand All @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join('\n')}`);
12 changes: 11 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function contains() {}
function contains(obj, targetKey) {
if (
obj === null ||
typeof obj !== "object" ||
Array.isArray(obj) ||
Object.getPrototypeOf(obj) !== Object.prototype
) {
throw new Error("Invalid Parameter");
}
return Object.hasOwn(obj, targetKey);
}

module.exports = contains;
29 changes: 28 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,43 @@ 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("contains on empty object returns false", () => {
const input = {};
const target = 'a'
const result = contains(input, target);
expect(result).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,it should return true", () => {
const input = { a: 1, b: 2 };
const target = 'a'
const result = contains(input, target)
expect(result).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, it should return false", () => {
const input = { a: 1, b: 2 };
const target = 'c'
const result = contains(input, target);
expect(result).toBe(false);
});


// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("given an invalid parameters, it should return false or throw an error", () => {
const input = ['a', 'b', 'c'];
const target = 0;
expect(() => contains(input, target)).toThrow();

});
Comment on lines 53 to +61
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test does not yet confirm that the function correctly returns false when the first argument is an array.
This is because contains(['a', 'b', 'c'], "a") could also return false simply because "a" is not a key of the array.

Arrays are objects, with their indices acting as keys. A proper test should use an array along with a valid key to ensure the function returns false specifically because the input is an array, not because the key is missing.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback. Test case now updated to correctly fail, given an invalid parameter


10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(codeCurrencyArrays) {
const result = {};

for (const [code, currency] of codeCurrencyArrays) {
result[code] = currency;
}

return result;
}

module.exports = createLookup;
20 changes: 19 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,21 @@ It should return:
'CA': 'CAD'
}
*/
test(
"given An array of arrays representing country code and currency code pairs, it should return an object where the keys are the country code", () => {
const input = [
["US", "USD"],
["CA", "CAD"],
["NG", "NGN"],
["GP", "GBP"],
];
const result = createLookup(input);
expect(result).toStrictEqual({
US: "USD",
CA: "CAD",
NG: "NGN",
GP: "GBP"
})
}
);

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

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

if (index === -1) {
queryParams[pair] = "";
continue;
}
const key = pair.slice(0, index);
const value = pair.slice(index + 1);
queryParams[key] = value;
}
Comment on lines 8 to 19
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the following function call, does your function return the value you expect?

parseQueryString("key1=value1&&key2=value2")

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback, function updated to skip empty entries


return queryParams;
}

module.exports = parseQueryString;

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


test("given an empty querystring, it should return an empty object", () => {
expect(parseQueryString("")).toEqual({})
})

test("given a querystring with missing value or no '=' found after the key, it should return empty string value", () => {
expect(parseQueryString("a=1&b=&c")).toEqual({ a: "1", b: "", c: "" });
});

14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function tally() {}
function tally(list) {
if (!Array.isArray(list)) {
throw new Error("Invalid Parameter");
}

const uniqueItems = Object.create(null);

for (const item of list) {
uniqueItems[item] = (uniqueItems[item] || 0) + 1;
}

return uniqueItems;
}

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("given an array of items, it should return an object containing count for each unique item", () => {
const input = ["a", "a", "a"];
const result = tally(input);
const targetOutput = { a: 3 };
expect(result).toEqual(targetOutput);
});

// 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("given an array of items, it should return an object containing count for each unique item", () => {
const input = ["a", "a", "b", "c"];
const result = tally(input);
const targetOutput = { a: 2, b: 1, c: 1 };
expect(result).toEqual(targetOutput);
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("", () => {
expect(() => tally("City Dreams")).toThrow();
});
11 changes: 10 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,30 @@
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 }
// returns { key: 1 }

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

// c) What is the target return value when invert is called with {a : 1, b: 2}
// should return {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 to loop through entries in 'obj'

// d) Explain why the current return value is different from the target output
// invertedObj.key = value; is creating a property named 'key' , to access the value stored in 'key' we use the bracket notation without quotes like this invertedObj[key]= value; If the property name comes from a variable → we always use bracket notation without quotes

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

//Given a function invert()
//When passed an object
//Then it should return a swapped key and value in the object
// example: Given => {a: 1, b: 2, c: 3}, should return {1: a, 2: b, 3: c}

test("Given an object with at least one key value pair, it should return an inverted key value", () => {
const input = { a: 1, b: 2, c: 3 };
const output = invert(input);
const target = { "1": "a", "2": "b", "3": "c" };
expect(output).toEqual(target);
})
Loading