Skip to content
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);

//address is an object without a key, therefore address[0] is undefined. To fix this we write "address.houseNumber".
3 changes: 2 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value in author) {
console.log(value);
}
// for...of works only with iterable objects like arrays, strings, maps..etc. In order to fix this problem we can use "for...in" to loop through the keys of the object.
9 changes: 6 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ 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:`);
for(let ingredient of recipe.ingredients) {
console.log(ingredient)
}
//I used a for...of loop to list each ingredient on a new line.
7 changes: 6 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}
function contains(obj, propName) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}
return propName in obj;
}

module.exports = contains;
23 changes: 21 additions & 2 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,39 @@ 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
describe("contains", () => {
test("returns true if the object contains the property, false otherwise", () => {
expect(contains({CYF:3,ITP:5}, "ITP")).toBe(true);
})

// 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("returns true for an existing property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
expect(contains({ x: 10, y: 20 }, "y")).toBe(true);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("returns false for a non-existent property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
expect(contains({ x: 10, y: 20 }, "z")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("returns false for invalid parameters like an array", () => {
expect(contains([], "a")).toBe(false);
expect(contains(null, "a")).toBe(false);
expect(contains("string", "a")).toBe(false);
});
});
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(pairs) {
const lookup = {};
for (const [country, currency] of pairs) {
lookup[country] = currency
}
return lookup;
}

module.exports = createLookup;
12 changes: 11 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
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'], ['JP', 'JPY']];
const expected = {
US: 'USD',
CA: 'CAD',
JP: 'JPY'
};

expect(createLookup(input)).toEqual(expected);
});


/*

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const index = pair.indexOf("=");
const key = pair.slice(0, index);
const value = pair.slice(index + 1);
queryParams[key] = value;
}

Expand Down
22 changes: 18 additions & 4 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,22 @@

const parseQueryString = require("./querystring.js")

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
describe("querystring parser", () => {
test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
});
});
});
test("returns empty object for empty query string", () => {
expect(parseQueryString("")).toEqual({});
});
test("handles key with missing value", () => {
expect(parseQueryString("key=")).toEqual({ key: "" });
});
test("handles missing key with value", () => {
expect(parseQueryString("=value")).toEqual({ "": "value" });
});
test("handles multiple key-value pairs", () => {
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
});
})
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(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array");
}

const result = {};
for (const item of arr) {
if (result[item] === undefined) {
result[item] = 1;
} else {
result[item] += 1;
}
}
return result;
}

module.exports = tally;
16 changes: 14 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,28 @@ 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

describe("tally test", () => {
test("tally counts each unique item correctly", () => {
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 counts each unique item 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("CYF")).toThrow("Input must be an array");
});
});
24 changes: 19 additions & 5 deletions 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;
}

// 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 gives an array of [key, value] pairs.
// d) Explain why the current return value is different from the target output

// invertedObj.key sets a property literally called "key". We need to use the variable key as the dynamic property name. and we need to change "key" to "value" and "value" to "key".
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
console.assert(
JSON.stringify(invert({ a: 1, b: 2 })) === JSON.stringify({ "1": "a", "2": "b" }),
"multiple keys test failed"
);
console.assert(
JSON.stringify(invert({ x: "hello", y: "world" })) === JSON.stringify({ "hello": "x", "world": "y" }),
"string cases test failed"
);
console.assert(
JSON.stringify(invert({})) === JSON.stringify({}),
"empty test failed"
);
console.log("All tests passed!");