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
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// This code will fail since we have put Index 0 instead of the key houseNumber

// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working
Expand All @@ -12,4 +14,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
4 changes: 3 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

//for...of cannot iterate plain objects directly, so it will display an error

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

Expand All @@ -11,6 +13,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
6 changes: 4 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// It will print the whole object instead of the ingredients list. To get it one below the other, we need to join the array and then \n to get it on separate lines

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
// How can you fix it?
Expand All @@ -11,5 +13,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);
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(object, propertyName) {
if (typeof object !== "object" || object === null || Array.isArray(object)) {
return false;
}
return Object.prototype.hasOwnProperty.call(object, propertyName);
}

module.exports = contains;
27 changes: 26 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,41 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("returns false when object is empty", () => {
expect(contains({}, "anyProp")).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 object with existing property name", () => {
expect(contains({ name: "Brad" }, "name")).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 object with non-existent property name", () => {
expect(contains({ name: "Brad" }, "age")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("returns false when input is an array", () => {
expect(contains(["Brad"], "0")).toBe(false);
});

// Given null inputs
// When passed to contains
// Then it should return false
test("returns false when input is null", () => {
expect(contains(null, "name")).toBe(false);
});

// Given non object inputs like string or number
// When passed to contains
// Then it should return false
test("returns false when input is a string", () => {
expect(contains("Brad", "name")).toBe(false);
});
12 changes: 10 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = {};

for (const pair of countryCurrencyPairs) {
const countryCode = pair[0];
const currencyCode = pair[1];
lookup[countryCode] = currencyCode;
}

return lookup;
}

module.exports = createLookup;
34 changes: 33 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,39 @@
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"],
];
const result = createLookup(input);
expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});

test("creates a lookup for 1 pair", () => {
const input = [["IN", "INR"]];
const result = createLookup(input);
expect(result).toEqual({
IN: "INR",
});
});

test("returns an empty object when input array is empty", () => {
const input = [];
const result = createLookup(input);
expect(result).toEqual({});
});

test("ignores cases where it is invalid within other pairs", () => {
const input = [["US", "USD"], ["Invalid"], ["CA", "CAD"]];
const result = createLookup(input);
expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});
/*

Create a lookup object of key value pairs from an array of code pairs
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
16 changes: 14 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@
// 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",
});
});

test("parses with missing value", () => {
expect(parseQueryString("a=")).toEqual({
a: "",
});
});

test("parses with missing key", () => {
expect(parseQueryString("=value")).toEqual({
"": "value",
});
});
18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const result = {};

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

return result;
}

module.exports = tally;
20 changes: 19 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,30 @@ 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", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally returns count for each unique item", () => {
const input = ["a", "a", "b", "b", "c", "a"];
expect(tally(input)).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("tally throws an error for invalid input", () => {
expect(() => {
tally("abc");
}).toThrow();
});

// Given a single item
// When passed to tally
// Then it should return count for the item
test("tally returns count for single item", () => {
expect(tally(["d"])).toEqual({ d: 1 });
});
11 changes: 10 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ 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 }
// The current value before fix is {key: 1}

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// The current value before fix is {key: 2} since it overrides the previous value

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

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries returns the array of [key, value] pairs
// For eg: Object.entries({a: 5, b: 7})
// returns: [["a", 5], ["b", 7]]
// It is needed since it allows to Loop through both key and value at the same time.

// d) Explain why the current return value is different from the target output
// The current return value is different from the target output since "key" is treated as a literal property name. We need to use variable value as the key

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// Done
Loading