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
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
26 changes: 23 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
if (
queryString === null ||
queryString === undefined ||
queryString.length === 0
) {
return queryParams;
}
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;
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
21 changes: 13 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 @@ -37,12 +37,17 @@ test("should replace '+' by ' '", () => {
});
});

test("should return an empty object for null or undefined", () => {
expect(parseQueryString(null)).toEqual({});
expect(parseQueryString(undefined)).toEqual({});
});

// 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",
// });
// });
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function tally() {}
function tally(items) {

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 will happen if I pass the following input to the fn?

["apple", "banana", "apple", "orange", "banana", undefined]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It treated undefined as a key and we receive undefined:1. Added if condition to ignore undefined and null

if (!Array.isArray(items)) {
throw new Error("Invalid input");
}
const itemsObj = {};
for (const item of items) {
if (item === undefined || item === null) continue;
if (itemsObj[item] === undefined) {
itemsObj[item] = 1;
} else itemsObj[item]++;
}
return itemsObj;
}

module.exports = tally;
32 changes: 30 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,52 @@ 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();
});

// Given undefined or null
// When passed to tally
// Then it should ignore these values
test("Given undefined or null, should ignore these values", () => {
expect(() => {
tally([
"apple",
"banana",
"apple",
"orange",
"banana",
undefined,
null,
]).toEqual(["apple", "banana", "apple", "orange", "banana"]);
});
});
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!)
24 changes: 24 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
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", () => {});
Loading