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
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);
}
4 changes: 2 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ const recipe = {
};

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

module.exports = contains;
return Object.hasOwn(obj, propertyName);
}
15 changes: 11 additions & 4 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function createLookup() {
// implementation here
}
function createLookup(countryCurrencyPairs) {
const lookup = {};

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

module.exports = createLookup;
lookup[countryCode] = currencyCode;
}

return lookup;
}
14 changes: 11 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
if (!queryString || queryString.length === 0) {
return queryParams;
}

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

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

if (firstEqualsIndex !== -1) {
const key = pair.slice(0, firstEqualsIndex);
const value = pair.slice(firstEqualsIndex + 1);
queryParams[key] = value;
} else {
queryParams[pair] = "";
}
}

return queryParams;
Expand Down
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] += 1;
} else {
result[item] = 1;
}
}

return result;
}

module.exports = tally;
25 changes: 19 additions & 6 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@ 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 }
module.exports = invert;

// a) What is the current return value when invert is called with { a : 1 }
// Current return value for { a : 1 } { key: "a" }
// b) What is the current return value when invert is called with { a: 1, b: 2 }

// Current return value for { a: 1, b: 2 } { key: "b" } (The second iteration overwrites the first).
// c) What is the target return value when invert is called with {a : 1, b: 2}

// Target return value for { a: 1, b: 2 } { "1": "a", "2": "b" }
// c) What does Object.entries return? Why is it needed in this program?

// t returns an array of [key, value] pairs (e.g., [["a", 1]]). It is needed to easily iterate over both keys and values using a for...of loop.
// d) Explain why the current return value is different from the target output

// The code uses dot notation (.key), which creates a literal property named "key". To use the content of a variable as a property name, you must use bracket notation ([value]).
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
test("inverts a simple object", () => {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
});

test("inverts an object with multiple properties", () => {
expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" });
});

test("returns empty object when passed empty object", () => {
expect(invert({})).toEqual({});
});
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.