Skip to content
Open
4 changes: 2 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
// Fix anything that isn't working

const address = {
houseNumber: 42,
houseNumber: "42",
street: "Imaginary Road",
city: "Manchester",
country: "England",
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
Expand Up @@ -2,7 +2,7 @@

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

// this function have 2 bugs, one is that the since object is not iterable so by using for of loop is will not work. The second is that is only print out property name not property value
const author = {
firstName: "Zadie",
lastName: "Smith",
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 thread
cjyuan marked this conversation as resolved.
2 changes: 1 addition & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
17 changes: 16 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function contains() {}
function contains(object, key) {
if (object === null || typeof object !== "object" || Array.isArray(object)) {
return false;
}

if (Object.keys(object).length === 0) {
return false;
}

for (const item in object) {
if (item === key) {
return true;
}
}
return false;
}

module.exports = contains;
30 changes: 28 additions & 2 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,46 @@ 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("should return true if the input object contain the key value", () => {
expect(contains({ a: 1, b: 2, c: 3, d: 4 }, "a")).toBe(true);
expect(contains({ a: 1, b: 2, c: 3, d: 4 }, "f")).toBe(false);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("should return false if the object is empty", () => {
let emptyObject = {};
expect(contains(emptyObject, "a")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("should return true if the input object contain existing key value", () => {
let infoObject = { name: "John", age: "24", city: "Glasgow" };
expect(contains(infoObject, "name")).toBe(true);
expect(contains(infoObject, "age")).toBe(true);
expect(contains(infoObject, "city")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("should return false if the input object do not have non existing key value", () => {
let goodsObject = { fruit: "Apple", amount: "50", origin: "Glasgow" };
expect(contains(goodsObject, "tree")).toBe(false);
expect(contains(goodsObject, "color")).toBe(false);
expect(contains(goodsObject, "country")).toBe(false);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("should return false if the input parameters is an array or not an object", () => {
let mixObject = { a: 2, b: "hello", c: [] };
expect(contains(mixObject, "c")).toBe(true);
expect(contains(["A", "B"], "1")).toBe(false);
expect(contains(null, "1")).toBe(false);
expect(contains(undefined, "1")).toBe(false);
expect(contains("ABC", "1")).toBe(false);
});
8 changes: 7 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
function createLookup(pairs) {
// implementation here
let countryPair = {};
for (const [country, currency] of pairs) {
countryPair[country] = currency;
}

return countryPair;
}

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

test.todo("creates a country currency code lookup for multiple codes");
test("creates a country currency code in an lookup array for multiple codes, and return in object form", () => {
let worldCurrency = [
["US", "USD"],
["GB", "POUND"],
["FRC", "EURO"],
["JP", "YEN"],
["KR", "WON"],
["MX", "PESO"],
];
expect(createLookup(worldCurrency)).toEqual({
US: "USD",
GB: "POUND",
FRC: "EURO",
JP: "YEN",
KR: "WON",
MX: "PESO",
});
});

/*

Expand Down
16 changes: 12 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {

if (!queryString || queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const pairs = queryString.split("&");

for (const pair of pairs) {
if (!pair) continue;

const [key, ...rest] = pair.split("=");
const value = rest.join("=");

if (!key) continue;

queryParams[key] = value;
}

Expand Down
4 changes: 2 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// 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",
});
});
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}
function tally(sumArray) {
if (!Array.isArray(sumArray)) {
throw new Error("Input musk be an array");
}

if (sumArray.length === 0) return {};

let totalSum = {};
for (const item of sumArray) {
if (!Object.hasOwn(totalSum, item)) {
totalSum[item] = 1;
} else {
totalSum[item] += 1;
}
}
Comment thread
cjyuan marked this conversation as resolved.
return totalSum;
}
module.exports = tally;
14 changes: 12 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ 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", () => {
let emptyObject = [];
expect(tally(emptyObject)).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

test("tally on an array with duplicate returns with correct amount object", () => {
let firstObject = ["a", "a", "c", "b", "c", "b", "a", "c"];
expect(tally(firstObject)).toEqual({ a: 3, b: 2, c: 3 });
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally on a string should throw invalid input", () => {
let stringInput = "input";
expect(() => tally(stringInput)).toThrow("Input musk be an array");
});
17 changes: 15 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
invertedObj[value] = key;
}

return invertedObj;
}
console.log(invert({ a: 1 }));
console.log(invert({ a: 1, b: 2 }));
console.log(invert({ a: 1, b: 2 }));

// a) What is the current return value when invert is called with { a : 1 }
// the current output value of this invert is {key:1}.

// b) What is the current return value when invert is called with { a: 1, b: 2 }
/* the current output value of this invert is {key:2} because
instead of accessing the [key] variable inside the condition parameter for store key for each loop, the code in line 13 instead adding .key behind and the system
interpret as string literal so each loop is overwrite the previous key value.
.*/

// c) What is the target return value when invert is called with {a : 1, b: 2}
// is also the same with { a: 1, b: 2 } because is overwrite the previous key each loop.

// c) What does Object.entries return? Why is it needed in this program?
/* Object.entries return deconstruct object key and value pair into individual item in an array.
This is very important because since object is are not iterable, this help by deconstructing object into array when we try to loop each key and value pair*/

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

/*
Because inside the for of loop, the invertedObj output assign they keys first instead of value for each loop.
*/
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
Loading