diff --git a/.gitignore b/.gitignore index 8ee70353fa..d62df10be6 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ node_modules **/.DS_Store .idea package-lock.json +mean.test.js +mean.js diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..36d2f865db 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -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}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977d..b0cfc8d159 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,5 @@ // Predict and explain first... +// it won't loop over it as if it was an array // 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 @@ -11,6 +12,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (let prop in author) { + console.log(author[prop]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cdc..20d1a84ed2 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,5 @@ // Predict and explain first... +// it will not print ingredients at all, as the values aren't accessed // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -11,5 +12,5 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..6e0ee30074 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} +function contains(obj, key) { + if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { + throw new Error("contains requires an object"); + } + + return Object.hasOwn(obj, key); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..27bf6abf93 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -23,18 +23,53 @@ as an array isn't an object // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("should return false given an empty object", () => { + 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("should return true when the object has the property", () => { + expect(contains({ age: 8, birthYear: 2019 }, "age")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("should return false given a non-existent property", () => { + expect(contains({ year: 2026, month: "July" }, "a")).toBe(false); +}); // Given a value that isn't an object - an array, a string, a number, // null, or no argument at all // When passed to contains // Then it should throw Error("contains requires an object") // (careful: typeof [] and typeof null are both "object") +test("should throw an error when passed an array", () => { + expect(() => contains([2, 5, 9], "a")).toThrow( + new Error("contains requires an object") + ); +}); + +test("should throw an error when passed a string", () => { + expect(() => contains("sun", "a")).toThrow( + new Error("contains requires an object") + ); +}); + +test("should throw an error when passed a number", () => { + expect(() => contains(9, "a")).toThrow( + new Error("contains requires an object") + ); +}); + +test("should throw an error when passed a null", () => { + expect(() => contains(null, "a")).toThrow( + new Error("contains requires an object") + ); +}); + +test("should throw an error when passed no argument", () => { + expect(() => contains()).toThrow(new Error("contains requires an object")); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..729973052f 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { - // implementation here -} +function createLookup(countryCurrencyPairs) { + const lookup = {}; + + countryCurrencyPairs.forEach(function (pair) { + const country = pair[0]; + const currency = pair[1]; + lookup[country] = currency; + }); + return lookup; +} module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..9e3b4a4f9e 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,13 @@ 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", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ US: "USD", CA: "CAD" }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..e884d1d088 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,13 +1,29 @@ 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; + if (pair === "") { + continue; + } + + const parts = pair.split("="); + const key = parts[0]; + const value = parts.slice(1).join("="); + + const decodeKey = decodeURIComponent(key.replace(/\+/g, " ")); + const decodeValue = decodeURIComponent(value.replace(/\+/g, " ")); + + queryParams[decodeKey] = decodeValue; } return queryParams; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df617..0d8a3598ad 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, 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("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ @@ -37,12 +37,6 @@ 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", - }); +test("should accept a leading question mark", () => { + expect(parseQueryString("?name=Sam")).toEqual({ name: "Sam" }); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..e5dbcbcbf5 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("tally requires an array"); + } + + const counts = {}; + + for (const item of items) { + if (Object.hasOwn(counts, item)) { + counts[item] += 1; + } else { + counts[item] = 1; + } + } + return counts; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c2965..9529e31a25 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,35 @@ 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("tally returns an object containing the count for each unique item", () => { + expect(tally(["a", "b", "c"])).toEqual({ a: 1, 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("tally returns counts for each unique item", () => { + expect(tally(["o", "o", "l", "m", "m", "m"])).toEqual({ o: 2, l: 1, m: 3 }); +}); // Given an invalid input like a string, a number, or no argument at all // When passed to tally // Then it should throw Error("tally requires an array") +test("tally throws an error if passed a string", () => { + expect(() => tally("nothing")).toThrow(new Error("tally requires an array")); +}); + +test("tally throws an error if passed a number", () => { + expect(() => tally(7)).toThrow(new Error("tally requires an array")); +}); + +test("tally throws an error if not passed anything", () => { + expect(() => tally()).toThrow(new Error("tally requires an array")); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..0f3444568a 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,22 @@ 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 } - +// { 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 the object's properties as pairs where each of them contains a key and it's value. +// It's needed here to make key and value pairs accessible (to eventually build an inverted version of the object). // d) Explain why the current return value is different from the target output - +// In invertedObj.key = value the dot means 'use the property literary named 'key'', not the variable key. +// So each loop pass overwrites that same property and the returned object contains only the last value, like {key: 'blue}. // e) Fix the implementation of invert (and write tests to prove it's fixed!) diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 0000000000..c45de9fabc --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,12 @@ +const invert = require("./invert.js"); + +test("invert should swap each key and value when passed an object", () => { + expect(invert({ a: "cherry", b: "kiwi" })).toEqual({ + cherry: "a", + kiwi: "b", + }); +}); + +test("invert should return an empty object when passed an empty object", () => { + expect(invert({})).toEqual({}); +});