Skip to content
Closed
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules
.DS_Store
.vscode
**/.DS_Store
**/.DS_Store
prep
6 changes: 4 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

/*
My prediction why code isn't working is that we can't access properties in object using index. We need to call the by key value
*/
// 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"]}`);
8 changes: 5 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

/*
I think the problem is because we try access property in object without saying what exactly property we need
*/
// 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) {
console.log(value);
for (const key in author) {
console.log(author[key]);
}
10 changes: 7 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

/*
I think we need to use iteration to access and log ingredients one by one
*/
// 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,7 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);
for (let ingredientIndex in recipe.ingredients) {
console.log(recipe.ingredients[ingredientIndex]);
}
4 changes: 3 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function contains() {}
function contains(object, property) {
return Object.keys(object).includes(property);
}

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

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

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

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
describe("contains", () => {
// Given an empty object
// When passed to contains
// Then it should return false
test("Given an empty object, return false", () => {
expect(contains({})).toEqual(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 properties, when passed an existing property name, return true", () => {
expect(contains({ a: 1, b: 2 }, "a")).toEqual(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 properties, when passed a non-existent property name, return false", () => {
expect(contains({ a: 1, b: 2 }, "c")).toEqual(false);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("Given invalid parameters like an array, return false or throw an error", () => {
expect(contains(["a", 1, "b", 2, "c"])).toEqual(false);
});
});
10 changes: 7 additions & 3 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(array) {
const countryCurrencyObject = {};
for (const index in array) {
countryCurrencyObject[array[index][0]] = array[index][1]
};
return countryCurrencyObject
};

module.exports = createLookup;
31 changes: 30 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,35 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
describe("function is called with the country-currency array as an argument", () => {
test("It should return an object", () => {
expect(
typeof createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual("object");
});
test("The keys are the country codes", () => {
expect(
Object.keys(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
)
).toEqual(["US", "CA"]);
});
test("The values are the corresponding currency codes", () => {
expect(
Object.values(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
)
).toEqual(["USD", "CAD"]);
});
});

/*

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 decodedPair = decodeURIComponent(pair);
const [key, value] = decodedPair.split(/=(.*)/s);

queryParams[key] = value;
}

Expand Down
8 changes: 7 additions & 1 deletion Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
// 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",
});
});

test("parses querystring encoded values containing =", () => {
expect(parseQueryString("a%25b=c%26d")).toEqual({
"a%b": "c&d",
});
});
10 changes: 9 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function tally() {}
function tally(arrayOfItems) {
object = {};
if (!Array.isArray(arrayOfItems)) {
throw new Error("Invalid input");
} else {
for (char of arrayOfItems) object[char] = (object[char] || 0) + 1;
return object;
}
}

module.exports = tally;
32 changes: 21 additions & 11 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,26 @@ 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("Given a function called tally, return an object containing the count for each unique item", () => {
// Given an empty array
// When passed to tally
// Then it should return an empty object
test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

// 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");
// 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, return counts for each unique item", () => {
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
// 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, throw an error", () => {
expect(() => tally("a,a,b,c")).toThrow("Invalid input");
});
});
37 changes: 29 additions & 8 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,45 @@

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}

function invert(obj) {
const invertedObj = {};
// function invert(obj) {
// const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
}

return invertedObj;
}
// for (const [key, value] of Object.entries(obj)) {
// invertedObj.key = value;
// }

// return invertedObj;
// }
// console.log(invert({ a: 1, b: 2 }));
// 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?
// It returns an array of string-keyed key-value pairs from the given object

// d) Explain why the current return value is different from the target output

// We want to add a property name to the object that is the value of the key variable and not the string "key".
// We can do this with square bracket notation

// current code creates an object property, where key is key and value is value.
// But we need to swap the key and value.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj[value] = key;
}

return invertedObj;
}

module.exports = invert;
8 changes: 8 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const invert = require("./invert.js");

describe("proof function is fixed", () => {
test("Given an object, it should swap the keys and values in the object", () => {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});
});
Loading