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

// The address variable is an object literal containing key–value pairs separated by commas. The code originally tries to access address[0], which would only work if the data were in an array. Since address is an object and not an array, there is no index 0, so it returns undefined. To fix the problem we must access the property using its key, for example address.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 +12,4 @@ const address = {
postcode: "XYZ 123",
};

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

// The program does not work because for...of can only be used on iterable objects such as arrays or strings. The author variable is an object, which is not iterable by default. To fix the problem we can use Object.values(author) to convert the object values into an iterable array and then loop through them.
// 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 +11,6 @@ const author = {
alive: true,
};

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

// The code does not work because ${recipe} prints the entire object rather than the ingredients array. To display each ingredient on a new line, we need to loop through recipe.ingredients, which is an array. Using a for...of loop allows us to print each ingredient individually.
// 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 +11,9 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
("ingredients:")
${recipe}`);

for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
12 changes: 10 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function contains() {}
function contains(object, property) {

module.exports = contains;
// check if parameter is a valid object and not an array
if (typeof object !== "object" || Array.isArray(object) || object === null) {
return false;
}

return Object.prototype.hasOwnProperty.call(object, property);
}

module.exports = contains;
34 changes: 15 additions & 19 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,31 @@
const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
particular property

E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'

E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

// Acceptance criteria:

// 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");
test("contains on empty object returns false", () => {
expect(contains({}, "a")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("returns true when object contains the property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).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 when object does not contain the property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
// Then it should return false
test("returns false when passed invalid parameters like an array", () => {
expect(contains([1, 2, 3], "a")).toBe(false);
});
15 changes: 12 additions & 3 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
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;
module.exports = createLookup;
18 changes: 16 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
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 countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"]
];

const result = createLookup(countryCurrencyPairs);

expect(result).toEqual({
US: "USD",
CA: "CAD"
});

});

/*

Expand Down Expand Up @@ -32,4 +46,4 @@ It should return:
'US': 'USD',
'CA': 'CAD'
}
*/
*/
18 changes: 16 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
function parseQueryString(queryString) {
const queryParams = {};

// If the query string is empty return empty object
if (queryString.length === 0) {
return queryParams;
}

const keyValuePairs = queryString.split("&");

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

const index = pair.indexOf("=");

// If no "=" exists treat the value as empty
if (index === -1) {
queryParams[pair] = "";
continue;
}

const key = pair.slice(0, index);
const value = pair.slice(index + 1);

queryParams[key] = value;
}

return queryParams;
}

module.exports = parseQueryString;
module.exports = parseQueryString;
25 changes: 24 additions & 1 deletion Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ 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("returns empty object for empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses multiple parameters", () => {
expect(parseQueryString("name=John&age=30")).toEqual({
name: "John",
age: "30",
});
});

test("handles parameter with empty value", () => {
expect(parseQueryString("name=")).toEqual({
name: "",
});
});

test("handles parameter without equals sign", () => {
expect(parseQueryString("name")).toEqual({
name: "",
});
});
24 changes: 22 additions & 2 deletions Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
function tally() {}
function tally(items) {

module.exports = tally;
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const result = {};

for (const item of items) {

if (result[item]) {
result[item] += 1;
} else {
result[item] = 1;
}

}

return result;

}

module.exports = tally;
19 changes: 18 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,33 @@ 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
test("counts frequency of items in an array", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 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("counts duplicates correctly", () => {
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("throws error if input is not an array", () => {
expect(() => tally("hello")).toThrow();
});
19 changes: 15 additions & 4 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

// a) What is the current return value when invert is called with { a : 1 }
// Before fixing the code it returned: { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// Before fixing the code it returned: { 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?
// d) What does Object.entries return? Why is it needed in this program?
// Object.entries(obj) returns an array of [key, value] pairs.
// Example: Object.entries({a:1, b:2})
// returns: [["a",1], ["b",2]]
// It allows us to loop through both keys and values of an object.

// d) Explain why the current return value is different from the target output
// e) Explain why the current return value is different from the target output
// The bug was that the code used invertedObj.key which creates a property
// literally called "key". Instead we need to use the variable value as the key,
// so we use bracket notation: invertedObj[value] = key.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
module.exports = invert;
2 changes: 2 additions & 0 deletions Sprint-2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading