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

//The function intent to return houseNumber. However, console.log should locate the key
//instead of index. function will return error before fix.
Comment on lines +3 to +4

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When the original address[0] expression runs, does it throw an error or produce a value?

To "like" or "dislike" this comment, please follow this link


// 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 +15,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,7 @@
// Predict and explain first...

//The function should return for all values in the object. But not sure the return before fix the problem

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

for (const value of author) {
console.log(value);
}
console.log(Object.values(author));
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...
// Ingredients will return the whole recipe before fix.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does JavaScript render for ${recipe} when recipe is an object?

To "like" or "dislike" this comment, please follow this link


// 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")}`);
11 changes: 10 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function contains() {}
function contains(testObject, keyword) {
if (
typeof testObject === "object" &&
testObject !== null &&
Array.isArray(testObject) === false
) {
return Object.hasOwn(testObject, keyword);
}
throw new Error("contains requires an object");
}

module.exports = contains;
66 changes: 48 additions & 18 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,51 @@ as an array isn't an object
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return 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")
describe("contains", () => {
// Given an empty object
// When passed to contains
// Then it should return false

test("return for {}", () => {
expect(contains({})).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("return for {a: 1, b: 2}, 'a'", () => {
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("return for {a: 1, b: 2}, 'c'", () => {
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("return for ['a', 'b', 'c']", () => {
expect(() => contains(["a", "b", "c"])).toThrow(
"contains requires an object"
);
});

test("return for null", () => {
expect(() => contains(null)).toThrow("contains requires an object");
});

test("return for 'It is a string.'", () => {
expect(() => contains("It is a string.")).toThrow(
"contains requires an object"
);
});

test("return for 10", () => {
expect(() => contains(10)).toThrow("contains requires an object");
});
});
3 changes: 2 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function createLookup() {
function createLookup(lookUpPairs) {
// implementation here
return Object.fromEntries(lookUpPairs);
}

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

test.todo("creates a country currency code lookup for multiple codes");
describe("createLookup", () => {
test("return a object for ([['US', 'USD'], ['CA', 'CAD']])", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({
US: "USD",
CA: "CAD",
});
});
});

/*

Expand Down
26 changes: 25 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,38 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
if (!pair.includes("=") && pair.length > 0) {
queryParams[pair] = "";
continue;
Comment on lines +10 to +12

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When a bare key such as %24half reaches this branch, is it decoded the same way as a key before =?

To "like" or "dislike" this comment, please follow this link

}

const key = decodeURIComponent(pair.slice(0, pair.indexOf("="))).replace(
"+",
" "
Comment on lines +16 to +17

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What does the second + become for an input like name=Ana+Maria+Garcia?

To "like" or "dislike" this comment, please follow this link

);
const value = decodeURIComponent(pair.slice(pair.indexOf("=") + 1)).replace(
"+",
" "
Comment on lines +19 to +21

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

How should an encoded literal plus such as %2B differ from a + used to represent a space? Trace how the value is decoded and then replaced here.

To "like" or "dislike" this comment, please follow this link

);

if (pair.indexOf("=") !== -1) {
queryParams[key] = value;
}

if (key.length === 0 && value.length === 0) {
continue;
}

queryParams[key] = value;
}

return queryParams;
}

console.log(parseQueryString("full+name=John+Doe"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What output appears when another module imports this parser?

To "like" or "dislike" this comment, please follow this link


module.exports = parseQueryString;
8 changes: 0 additions & 8 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,4 @@ test("should replace '+' by ' '", () => {
});
});

// Stretch exercise: Handling query strings that contain identical keys

// Delete this test if you are not working on this optional case
test("should store values of a key in an array when the key has 2 or more values", () => {
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
key: ["value1", "value2", "value3"],
foo: "bar",
});
});
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("tally requires an array");
}
const counter = {};
for (const item of items) {
if (Object.hasOwn(counter, item) === false) {
counter[item] = 1;
} else {
counter[item]++;
}
}

return counter;
}

module.exports = tally;
18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,28 @@ 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("return empty object for []", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("return a list of items and count the frequency of each item", () => {
expect(tally(["a", "a", "a", "b", "b"])).toEqual({ a: 3, b: 2 });
});

// 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("throw error for 'It is a string'", () => {
expect(() => tally().toThrow("tally requires an array"));
});

test("throw error for 10", () => {
expect(() => tally(10).toThrow("tally requires an array"));
});

test("throw error for ''", () => {
expect(() => tally().toThrow("tally requires an array"));
Comment on lines +41 to +49

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Where is .toThrow(...) invoked in these expectations, and does Jest run an assertion? The same pattern appears in all three invalid-input tests.

To "like" or "dislike" this comment, please follow this link

});
16 changes: 15 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
if (Object.hasOwn(invertedObj, value)) {
throw new Error(`Duplicate key found after inversion: "${value}"`);
}
invertedObj[value] = key;
}

return invertedObj;
}




// 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?
// [ [ 'a', 1 ], [ 'b', 2 ] ]. Object.entries convert object to array. Then allow to use
// array's method to achieve teh goal of function.

// d) Explain why the current return value is different from the target output
// invertedObj.key is not mean convert value to key. It just simply print string "key"

// e) Fix the implementation of invert (and write tests to prove it's fixed!)

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

describe("invert", () => {
test("inverting key and value of object", () => {
expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" });
});

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

test("inverting object with either key or value is empty", () => {
expect(invert({ "": 1, b: "" })).toEqual({ 1: "", "": "b" });
});

test("inverting object with duplicate value", () => {
expect(() => invert({ a: 1, b: 1 })).toThrow(
'Duplicate key found after inversion: "1"'
);
});
});
Loading