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: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// The code is trying to log the house number from the address object, but it is using the wrong syntax to access the property. The code is currently using address[0], which is incorrect because it is trying to access the first element of an array, but address is an object, 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 +14,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,5 +1,7 @@
// Predict and explain first...

// This program attempt to log out all the property values in the object, It isnt working because we are trying to iterate over an object using a for... of loop, which is not valid. To fix this, we can use a for... in loop to iterate over the keys of the object and then access the corresponding values.

// 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 key in author) {
console.log(author[key]);
}
10 changes: 7 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// The code is trying to log out the title, how many it serves and the ingredients of a recipe. However, it is not working because when we try to log the recipe object directly, it will not format the output as intended. Instead, we should access each property of the recipe object separately and format the output accordingly.

// 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 @@ -10,6 +12,8 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`);
console.log("Ingredients:");
for (const ingredient of recipe.ingredients) {
console.log(`- ${ingredient}`);
}
4 changes: 3 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function contains() {}
function contains(object, property) {
return Object.prototype.hasOwnProperty.call(object, property);
Copy link
Contributor

Choose a reason for hiding this comment

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

  • The function is expected to throw an error or return false when object is not an object or is an array.

  • Could also explore Object.hasOwn().

}

module.exports = contains;
34 changes: 26 additions & 8 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,53 @@
const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
particular property
Implement a function called contains that checks an object contains a
particular property

E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'
E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'

E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/
E.g. contains({a: 1, b: 2}, 'c') // returns false
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
test("contains checks if an object contains a particular property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).toBe(true);
expect(contains(obj, "c")).toBe(false);
});

// 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", () => {
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("contains an object with existing property returns true", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains an object with non-existent property returns false", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains with invalid parameters returns false", () => {
expect(contains([], "a")).toBe(false);
});
Comment on lines 48 to +53
Copy link
Contributor

Choose a reason for hiding this comment

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

expect(contains([], 'a')).toBe(false) may fail to verify whether
contains() can detect its first argument is an array
.

contains([], "a") could also return false because "a" is not a property (or key) of an array.
However, "0", "1", "2" are keys of [1, 2, 3], so it is better to specify the test as
expect(contains([1, 2, 3], "1")).toBe(false); (to ensure you are checking what you describe).

You could also add a few more samples to ensure the function can properly handle arguments of different types.

8 changes: 6 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function createLookup() {
// implementation here
function createLookup(countryCurrency) {
const lookup = {};
for (const [country, currency] of countryCurrency) {
lookup[country] = currency;
}
return lookup;
}

module.exports = createLookup;
16 changes: 15 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
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"],
];

const expectedOutput = {
US: "USD",
CA: "CAD",
};

const result = createLookup(input);

expect(result).toEqual(expectedOutput);
});

/*

Expand Down
13 changes: 11 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +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("=");
queryParams[key] = value;
const indexOfEquals = pair.indexOf("=");

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

Choose a reason for hiding this comment

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

For each of the following function calls, does your function return the value you expect?

parseQueryString("a=b&=&c=d")
parseQueryString("a=")
parseQueryString("=b")
parseQueryString("a=b&&c=d")

Note: (You don't have to implement this)

  • In real query string, both key and value are percent-encoded or URL encoded in the URL.
    For example, the string "5%" is encoded as "5%25". So to get the actual value of "5%25"
    (whether it is a key or value in the query string), we need to call a function to decode it.

  • You can also explore the URLSearchParams API.

}

return queryParams;
Expand Down
27 changes: 25 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,33 @@
// 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("returns an empty object for an empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

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

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

test("handles a key with no equals sign", () => {
expect(parseQueryString("name")).toEqual({
name: "",
});
});
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(items) {
if (!Array.isArray(items)) {
throw new Error("Invalid input");
}

const result = {};

for (const item of items) {
if (result[item]) {
result[item]++;
} else {
result[item] = 1;
}
}
Comment on lines +6 to +14
Copy link
Contributor

Choose a reason for hiding this comment

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

Does the following function call returns the value you expect?

tally(["toString", "toString"]);

Suggestion: Look up an approach to create an empty object with no inherited properties.


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("tally counts duplicate items correctly", () => {
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("tally throws an error for invalid input", () => {
expect(() => tally("not an array")).toThrow();
});
20 changes: 19 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,38 @@ 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 }

//{ 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(obj) returns an array of key-value pairs from the object.
// Object.entries({ a: 1, b: 2 });
// returns: [["a", 1], ["b", 2]]
//It is needed so we can loop through both the keys and values of the object at the same time.

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

//The current implementation is incorrect because it uses:
// invertedObj.key = value;
// This creates a property literally called "key" instead of using the actual key/value dynamically.
//So it overwrites the same property each time instead of building the correct object.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
18 changes: 18 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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({});
});
21 changes: 21 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,24 @@

3. Order the results to find out which word is the most common in the input
*/
function countWords(str) {
const result = {};

if (str.length === 0) {
return result;
}

const words = str.split(" ");

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

return result;
}
Comment on lines +29 to +47
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you check if your function returns what you expect in the following function calls?

countWords("Hello,World! Hello World!");
countWords("constructor constructor");
countWords("          Hello World      ");

Note: The spec is not clear about exactly what to expect from these function calls. This is just for self-check.


module.exports = countWords;
13 changes: 13 additions & 0 deletions Sprint-2/stretch/count-words.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const countWords = require("./count-words.js");

test("counts words correctly", () => {
expect(countWords("you and me and you")).toEqual({
you: 2,
and: 2,
me: 1,
});
});

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