Skip to content
Open
Show file tree
Hide file tree
Changes from 3 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}`);
2 changes: 1 addition & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
2 changes: 1 addition & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
10 changes: 8 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(obj, key) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}

module.exports = contains;
return Object.keys(obj).includes(key);
}

module.exports = contains;
14 changes: 13 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ 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({}, "a")).toEqual(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 has the property", () => {
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("returns false when object doesn't have the property", () => {
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("returns false for invalid input like an array", () => {
expect(contains([1, 2, 3], "a")).toEqual(false);
});

10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = {};

for (const [countryCode, currencyCode] of countryCurrencyPairs) {
lookup[countryCode] = currencyCode;
}

return lookup;
}

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", () => {
const countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
];

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

/*

Expand Down
20 changes: 19 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,29 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

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

const separatorIndex = pair.indexOf("=");
let key;
let value;

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

key = decodeURIComponent(key.replace(/\+/g, " "));
value = decodeURIComponent(value.replace(/\+/g, " "));
queryParams[key] = value;
}

return queryParams;
}

module.exports = parseQueryString;

9 changes: 0 additions & 9 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,3 @@ test("should replace '+' by ' '", () => {
});
});

// 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",
});
});
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) {
if (typeof items === "string" || !Array.isArray(items)) {
throw new Error("tally expects an array");
}

const counts = {};

for (const item of items) {
counts[item] = (counts[item] || 0) + 1;
}

return counts;
}

module.exports = tally;
5 changes: 3 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ 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
Comment thread
eyob-tech marked this conversation as resolved.
Resolved
Expand Down
22 changes: 21 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,40 @@ 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 }
// Answer: { key: 1 } — the loop runs once, and the old buggy line
// invertedObj.key = value set a literal property called "key" to 1.

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// Answer: { key: 2 } — the loop runs twice, and each time it overwrote the
// same literal "key" property, so only the last value (2) survived.

// c) What is the target return value when invert is called with {a : 1, b: 2}
// Answer: { 1: "a", 2: "b" } — each original value becomes a key, and each
// original key becomes the value at that new key.

// c) What does Object.entries return? Why is it needed in this program?
// Answer: Object.entries(obj) returns an array of [key, value] pairs for
// every property in obj, e.g. Object.entries({a: 1, b: 2}) returns
// [["a", 1], ["b", 2]]. It's needed because inverting requires both the
// key and value together at the same time, so each can be swapped into
// the other's position.

// d) Explain why the current return value is different from the target output
// Answer: the original code used invertedObj.key = value, where .key is
// a literal property name (the string "key"), not the loop variable key.
// So every iteration overwrote the same fixed property instead of creating
// a new one, and it also assigned in the wrong direction (value into a key
// slot named "key", instead of swapping key and value with each other).

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
console.log(invert({ x: 10, y: 20 })); // should print { '10': 'x', '20': 'y' }
console.log(invert({ a: 1, b: 2 })); // should print { '1': 'a', '2': 'b' }

module.exports = invert;
Loading