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

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
7 changes: 5 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

// 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
// To loop over object keys we use for in loop.In this case we declared a variable `value`
// to hold the property name. In this case we use bracket notation instead of dot notation to
// access property value.

const author = {
firstName: "Zadie",
Expand All @@ -11,6 +14,6 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}
4 changes: 2 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);
6 changes: 5 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
function contains() {}
function contains(object, propertyName) {
if (Object.hasOwn(object, propertyName)) {
return true;
} else return false;
}

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
test("given an object with property, should return true if contains the property otherwise false", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
expect(contains({ a: 1, b: 2 }, "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({})).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("given an object with property, should return true if contains the property", () => {
expect(
contains({ firstName: "Chris", surname: "Marin", age: 21 }, "firstName")
).toBe(true);
});

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

test("given an object with property, should return false if does not contains the property", () => {
expect(
contains({ firstName: "Chris", surname: "Marin", age: 21 }, "job")
).toBe(false);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error

test("given an array, should return false", () => {
expect(contains([1, 2, 3], "a")).toBe(false);
});
7 changes: 6 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function createLookup() {
function createLookup(data) {
// implementation here
const newObject = {};
for (const [country, currency] of data) {
newObject[country] = currency;
}
return newObject;
}

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", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({
US: "USD",
CA: "CAD",
});
});

/*

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
if (pair === "") {
continue;
}
const equalIndex = pair.indexOf("=");
let key;
let value;

if (equalIndex === -1) {
key = pair.slice(0);
value = "";
} else {
key = pair.slice(0, equalIndex);
value = pair.slice(equalIndex + 1);
}
key = key.replace(/\+/g, " ");
value = value.replace(/\+/g, " ");
key = decodeURIComponent(key);
value = decodeURIComponent(value);
queryParams[key] = value;
}

return queryParams;
}

Expand Down
16 changes: 8 additions & 8 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Below are some test cases the implementation doesn't handle well.
// Fix the implementation for these tests, 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("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
Expand Down Expand Up @@ -39,10 +39,10 @@ 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",
});
});
// // 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",
// });
// });
13 changes: 12 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Invalid input");
}
const itemsObj = {};
for (const item of items) {
if (itemsObj[item] === undefined) {
itemsObj[item] = 1;
} else itemsObj[item]++;
}
return itemsObj;
}

module.exports = tally;
15 changes: 13 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,35 @@ const tally = require("./tally.js");
*
* tally(['a']), target output: { a: 1 }
* tally(['a', 'a', 'a']), target output: { a: 3 }
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
* tally([c]), target output: { a : 2, b: 1, c: 1 }
*/

// Acceptance criteria:

// 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("Given an array of items, should return an object containing the count for each unique item", () => {
expect(tally(["a", "b", "c", "d"])).toEqual({ a: 1, b: 1, c: 1, d: 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("Given an array with duplicate items, should return counts for each unique item", () => {
expect(tally(["a", "a", "a", "b", "b", "c"])).toEqual({ a: 3, b: 2, c: 1 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("Given an invalid input like a string, it should throw an error", () => {
expect(() => tally("hello")).toThrow();
});
12 changes: 9 additions & 3 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,26 @@ 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 }
// The current return value when inverted is called with { a: 1 } is{ key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// The current return value when inverted is called with { a: 1, b: 2 } is { key: 2 }

// c) What is the target return value when invert is called with {a : 1, b: 2}
// The target return value when invert is called with { a : 1, b: 2 } is { 1 : a, 2: b }

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries return an array where each element of array is a pair key-value. It is needed in this program because an object can not be looped with for...of.

// d) Explain why the current return value is different from the target output
// d) Explain why the current return value is different from the target output.
// The current return value is different from the target output because when we apply
//invertedObj.key = value we are declaring a new key and assigning the value

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

// Given an empty object
// When invert is passed this object
// Then it should return an empty object.
test("Given an empty object,should return an empty object", () => {
expect(invert({})).toEqual({});
});

// Given an object
// When invert is passed this object
// Then it should swap the keys and values in the object
test("Given an object, should swap the keys and values in the object", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
expect(invert({ a: 1, b: 2, c: 3, d: 4 })).toEqual({
1: "a",
2: "b",
3: "c",
4: "d",
});
});

test("Given an empty object, should swap the keys and values in the object", () => {});

test("Given an object, should swap the keys and values in the object", () => {});
Loading