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: 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...

// this code will throw an error. because the text `My house number is ${address[0]}` is not inside backticks.
// so JavaScript doesn’t see it as a string.
// Instead, it thinks `My` is a variable name, but that variable doesn’t exist.
// 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}`);
6 changes: 4 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// This code will throw an error.`author` is an object.
// and `for…of` only works on arrays or strings.
// Use `Object.values(author)` or `for…in` to loop over object values.
// 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) {
for (const value of Object.values(author)) {
console.log(value);
}
7 changes: 4 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// `recipe` is an object. `${recipe}` prints [object Object], not the ingredients.
// To log ingredients, loop over `recipe.ingredients` or join them into a string.
// 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 +12,5 @@ const recipe = {
};

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

module.exports = contains;
16 changes: 14 additions & 2 deletions 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")).toBe(false);
});

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

test("contains returns true for existing property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(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 for non-existent property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains returns false for invalid input like an array", () => {
expect(contains([1, 2, 3], "0")).toBe(false);
expect(contains(null, "a")).toBe(false);
expect(contains("string", "0")).toBe(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(pairs) {
const result = {};

for (const [key, value] of pairs) {
result[key] = value;
}

return result;
}

module.exports = createLookup;
14 changes: 13 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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"],
["GB", "GBP"],
];

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

/*

Expand Down
30 changes: 26 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
function parseQueryString(queryString) {
const queryParams = {};

if (queryString.startsWith("?")) {
queryString = queryString.slice(1);
}

if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

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

for (const pair of pairs) {
const index = pair.indexOf("=");
let key, value;

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

const decodedKey = decodeURIComponent(key);

if (value === undefined || value === "") {
queryParams[decodedKey] = undefined;
} else {
queryParams[decodedKey] = decodeURIComponent(value);
}
}

return queryParams;
Expand Down
18 changes: 16 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
// 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("handles keys with empty values as undefined", () => {
expect(parseQueryString("key1=&key2=value2")).toEqual({
key1: undefined,
key2: "value2",
});
});

test("handles keys without = as undefined", () => {
expect(parseQueryString("key1&key2=value2")).toEqual({
key1: undefined,
key2: "value2",
});
});
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(array) {
if (!Array.isArray(array)) {
throw new Error("Input must be an array");
}

const counts = Object.create(null);

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

return counts;
}

module.exports = tally;
16 changes: 15 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,30 @@ 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 single item correctly", () => {
expect(tally(["a"])).toEqual({ a: 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("returns empty object for empty array", () => {
expect(tally([])).toEqual({});
});

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

test("counts each unique item correctly", () => {
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});
// 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("not an array")).toThrow("Input must be an array");
expect(() => tally(123)).toThrow("Input must be an array");
expect(() => tally({})).toThrow("Input must be an array");
});
20 changes: 17 additions & 3 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ 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 }
// { "1": "a" }

// b) What is the current return value when invert is called with { a: 1, b: 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?
// Returns an array of [key, value] pairs. It lets us loop over keys and values easily.

// d) Explain why the current return value is different from the target output
// The original function used "key" literally, so the object’s keys weren’t swapped correctly.
// The fixed version uses `invertedObj[value] = key` to swap keys and values.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)

const invert = require("./invert.js");

test("inverts single key-value object", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("inverts multiple key-value pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});