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
6 changes: 5 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ const address = {
postcode: "XYZ 123",
};

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

// Code returns undefined
// because address is an object but it is accessed like an array
// in an object values can be accessed by using their proper keys
8 changes: 6 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const key in author) {
console.log(author[key]);
}

// for iterating in an object for...in can be used
// and each iteration it returns the key
// then each value can be accessed
10 changes: 7 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}\ningredients:`);
recipe.ingredients.forEach((ingredient) => console.log(ingredient));

// everything is logged on one line
// console.log is logging the recipe object array as a whole
// instead of each value of ingredients array
// the ingredients array must be logged using a loop
9 changes: 8 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function contains() {}
function contains(obj, property) {
try {
const keys = Object.keys(obj);
return keys.includes(property);
} catch (error) {
throw new Error("The parameter given is not a plain JS object.");
}
}

module.exports = contains;
18 changes: 17 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,32 @@ 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("contains on empty object returns false", () =>
expect(contains({}, "key1")).toEqual(false));

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("contains returns true when object contains the given property", () =>
expect(contains({ key1: "value1", key2: "value2" }, "key1")).toEqual(true));

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains returns false when object does not contain the given property", () =>
expect(contains({ key1: "value1", key2: "value2" }, "key4")).toEqual(false));

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
it("contains returns false or throws an error if given parameter is not a valid object", () => {
expect(contains([], "key1")).toEqual(false);
expect(contains("key1:value1", "key1")).toEqual(false);
expect(contains(5235, "key1")).toEqual(false);
expect(() => contains(undefined, "key1")).toThrow(
"The parameter given is not a plain JS object."
);
expect(() => contains(null, "key1")).toThrow(
"The parameter given is not a plain JS object."
);
});
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(countryCurrencyPairs) {
if (!Array.isArray(countryCurrencyPairs)) return {};
return countryCurrencyPairs.reduce((acc, curr) => {
acc[curr[0]] = curr[1];
return acc;
}, {});
}

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

test.todo("creates a country currency code lookup for multiple codes");

/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down Expand Up @@ -33,3 +31,24 @@ It should return:
'CA': 'CAD'
}
*/

describe("createLookup", () => {
it("returns empty object if the parameter passed is not an array", () => {
expect(createLookup("US:USD")).toEqual({});
expect(createLookup(undefined)).toEqual({});
});

it("returns an object of country initials and currency code", () =>
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({
US: "USD",
CA: "CAD",
}));

it("returns an empty object if passed an empty array", () =>
expect(createLookup([])).toEqual({}));
});
25 changes: 23 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,29 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
let key = "",
value = "";
let equalSignIndex = pair.indexOf("=");

if (equalSignIndex === -1) {
key = pair;
value = "";
} else {
key = pair.slice(0, equalSignIndex);
value = pair.slice(equalSignIndex + 1);
}

const existingKeys = Object.keys(queryParams);
if (key === "" && value === "") continue;
if (existingKeys.includes(key)) {
if (queryParams[key] === value) continue;
if (Array.isArray(queryParams[key])) {
queryParams[key].push(value);
} else {
const temp = queryParams[key];
queryParams[key] = [temp, value];
}
} else queryParams[key] = value;
}

return queryParams;
Expand Down
33 changes: 31 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,39 @@
// 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 querystring values containing repetitive keys", () => {
expect(parseQueryString("a=1&b=6&a=2&a=3&b=7")).toEqual({
a: ["1", "2", "3"],
b: ["6", "7"],
});
});

test("parses querystring values containing repetitive keys with same values", () => {
expect(parseQueryString("a=1&b=6&a=2&a=3&b=6")).toEqual({
a: ["1", "2", "3"],
b: "6",
});
});

test("parses querystring values containing no key and value but only =", () => {
expect(parseQueryString("=&b=6&a=2&=&=")).toEqual({
a: "2",
b: "6",
});
});

test("parses querystring values missing = sign", () => {
expect(parseQueryString("id=5&name=mohsen&age")).toEqual({
id: "5",
name: "mohsen",
age: "",
});
});
8 changes: 7 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function tally() {}
function tally(list) {
if (!Array.isArray(list)) throw new Error("Not an array.");
return list.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});
}

module.exports = tally;
12 changes: 11 additions & 1 deletion 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", () =>
expect(tally([])).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 items returns an object with the count of items", () => {
expect(tally(["a"])).toEqual({ a: 1 });
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws an error if passed not an array", () => {
expect(() => tally("not an array")).toThrow("Not an array.");
expect(() => tally({})).toThrow("Not an array.");
});
10 changes: 9 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ 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 }
// { 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}
// { key: 2 }

// c) What does Object.entries return? Why is it needed in this program?
// it returns each pair of key and value as array.
// it is needed for iteration through an object

// d) Explain why the current return value is different from the target output
// because the place of key and value are not swapped

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

describe("invert", () => {
it("returns empty object if passed empty object", () =>
expect(invert({})).toEqual({}));

it("returns inverted object of the passed object", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
expect(invert({ a: 1, b: 2, c: 3, d: 4, e: 5 })).toEqual({
1: "a",
2: "b",
3: "c",
4: "d",
5: "e",
});
});
});
11 changes: 11 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,14 @@

3. Order the results to find out which word is the most common in the input
*/

function countWords(string) {
const noPunctuationStr = string.replace(/[.,!?]/g, "");
const wordArray = noPunctuationStr.split(" ");
let wordCount = new Map();
for (let word of wordArray) {
wordCount.set(word, (wordCount.get(word) || 0) + 1);
}
const sortedWordCount = [...wordCount.entries()].sort((a, b) => b[1] - a[1]);
return sortedWordCount;
}
16 changes: 12 additions & 4 deletions Sprint-2/stretch/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// refactor calculateMode by splitting up the code
// into smaller functions using the stages above

function calculateMode(list) {
// track frequency of each value
// track frequency of each value
function calculateFreq(list) {
let freqs = new Map();

for (let num of list) {
Expand All @@ -19,18 +19,26 @@ function calculateMode(list) {

freqs.set(num, (freqs.get(num) || 0) + 1);
}
return freqs;
}

// Find the value with the highest frequency
// Find the value with the highest frequency
function findMaxFreq(freqs) {
let maxFreq = 0;
let mode;

for (let [num, freq] of freqs) {
if (freq > maxFreq) {
mode = num;
maxFreq = freq;
}
}
return [maxFreq, mode];
}

function calculateMode(list) {
const calculatedFrequency = calculateFreq(list);
const [maxFreq, mode] = findMaxFreq(calculatedFrequency);
return maxFreq === 0 ? NaN : mode;
}

module.exports = calculateMode;
16 changes: 14 additions & 2 deletions Sprint-2/stretch/till.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ function totalTill(till) {
let total = 0;

for (const [coin, quantity] of Object.entries(till)) {
total += coin * quantity;
const valueOfCoin = Number(coin.slice(0, -1));
total += valueOfCoin * quantity;
}

return `£${total / 100}`;
return `£${(total / 100).toFixed(2)}`;
}

const till = {
Expand All @@ -22,10 +23,21 @@ const till = {
};
const totalAmount = totalTill(till);

const assertEquals = (actual, expected) => {
console.assert(
actual === expected,
`Received ${actual} but expected ${expected}.`
);
};

// a) What is the target output when totalTill is called with the till object
// £4.4

// b) Why do we need to use Object.entries inside the for...of loop in this function?
// to iterate in an object and access the [key, value] pair

// c) What does coin * quantity evaluate to inside the for...of loop?
// it multiplies keys and values of till

// d) Write a test for this function to check it works and then fix the implementation of totalTill
assertEquals(totalAmount, "£4.40");