Skip to content

ITP-Jan | NW | Jovy So | Module-Data-Groups | WEEK 2 #508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
Expand Up @@ -12,4 +12,7 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
// console.log(`My house number is ${address[0]}`);
// Predict result: My house number is undefined. As address[0] is undefined.

console.log(`My house number is ${address["houseNumber"]}`);
8 changes: 7 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ const author = {
alive: true,
};

for (const value of author) {
// for (const value of author) {
// console.log(value);
// }

// Predict: Will get error because for..of loop works only on Iterables

for (const value of Object.values(author)) {
console.log(value);
}
17 changes: 14 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,17 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
// console.log(`${recipe.title} serves ${recipe.serves}
// ingredients:
// ${recipe}`);

// console.log(`${recipe}`);
//Predict result: bruschetta serves 2 ingredients: [object Object]

console.log(`${recipe.title}
serves ${recipe.serves}
ingredients:`);

for (let ingredient of recipe.ingredients) {
console.log(ingredient);
}
15 changes: 14 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function contains() {}
function contains(object, propertyName) {
// console.log(propertyName, '<-- property name');
// if (!propertyName) return false;
// if (!object) return false;
if (typeof object !== "object" || object === null || Array.isArray(object) || typeof object === "string") return false;
Copy link

Choose a reason for hiding this comment

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

This check is, typeof object === "string", is not needed because a string is not an object; the first condition would already be true.

if (object.hasOwnProperty(propertyName)) return true;
else return false;
};

module.exports = contains;

console.log(contains({a: 1, b: 2}, 'a'));
console.log(contains({a: 1, b: 2}));
console.log(contains());
console.log(contains({a: 1, b: 2}, 'c'));
console.log(contains([1,2,3]));
22 changes: 21 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,40 @@ 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 object and property arg returns true", () => {
expect(contains({a: 1, b: 2}, 'a')).toEqual(true);
});

test("given an object, but no property arg then returns false", () => {
expect(contains({a: 1, b: 2})).toEqual(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")).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("contains the property in the object returns true", () => {
expect(contains({a: 1, b: 2}, 'a')).toEqual(true);
});

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

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains invalid parameters return false", () => {
expect(contains([1,2,3], '0')).toEqual(false);
expect(contains("ABC", '0')).toEqual(false);
expect(contains(null, 'a')).toEqual(false);
});
7 changes: 5 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
function createLookup() {
function createLookup(countryCurrencyPairs) {
// implementation here
}
if (countryCurrencyPairs !== undefined) return Object.fromEntries(countryCurrencyPairs);
};

module.exports = createLookup;

console.log(createLookup([['US', 'USD'], ['CA', 'CAD'], ["HK", 'HKD']]));
7 changes: 6 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
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");

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


/*

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
const [key, value] = pair.split(/=(.*)/s);
// queryParams[key] = value;
queryParams[decodeURIComponent(key)] = decodeURIComponent(value);
}

return queryParams;
}

module.exports = parseQueryString;

console.log(parseQueryString("equation=x=y+1"));
console.log(parseQueryString("5%25"));
1 change: 1 addition & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function tally() {}
function tally(items) {
let countLetter = {};
if (Array.isArray(items) === false) throw new Error("You must provide an item");
else if (items.length === 0) return countLetter;
else {
items.forEach(letter => countLetter[letter] ? countLetter[letter]++ : countLetter[letter] = 1)
return countLetter;
};
};

module.exports = tally;

console.log(tally(['a', 'a', 'a'])); // output: { a: 3 }
console.log(tally(['a', 'b', 'c'])); // output: { a: 1, b: 1, c: 1 }
console.log(tally([])); // output: {}
// console.log(tally('Hello')); // output: Throw Error "You must provide an item"
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,31 @@ 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 on each unique item on an array returns the count", () => {
expect(tally(['a', 'b', 'c'])).toEqual({ a: 1, b: 1, c: 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("tally on each unique item on an array with duplicate items returns the count for each unique item", () => {
expect(tally(['a', 'a', 'a'])).toEqual({ a: 3 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("Given an invalid input throw an error", () => {
expect( () => { tally('Hello') } ).toThrow("You must provide an item");
});

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;
invertedObj[value] = key;
}

return invertedObj;
}

module.exports = invert;

console.log(invert({x : 10, y : 20})); //output: {"10": "x", "20": "y"}
console.log(invert({ a: 1, b: 2 })); // output: {"1": "a", "2": "b"}

// 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 returns an array of the key/pairs of an object, it makes it simple to use objects in loops.
// In this case, if Object.entries is removed, it would occur error showing obj is not iterable.
// Therefore, Object.entries is needed as it changed obj to array so that it can be looped over.

// d) Explain why the current return value is different from the target output
// There are two problems in the current function
// 1. key and value haven't been swap
// 2. invertedObj.key is unable to return the key of the object
Copy link

Choose a reason for hiding this comment

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

The statement invertedObj.key is unable to return the key of the object is not quite clear.
May I suggest asking ChatGPT the difference between obj.key and obj[key] ?

Copy link
Author

Choose a reason for hiding this comment

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

Of course, let me try to explain after asking ChatGPT.
When I know the key name, I can use obj.key, it treats key as a literal property name ("key")
When key name is dynamic or in a variable, I need to use obj[key], it treats key as a variable.
In this case, I don't know the actual name of the key (because it can be 'x' / 'y' / 'a' or something else), so I have to use obj[key]


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

test("Given an object, then swap the keys and values in the object", () => {
expect(invert({x : 10, y : 20})).toEqual({"10": "x", "20": "y"});
});