diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..36d2f865d 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 8c2125977..419780a78 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,9 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(author[value]); } + +// The for of loop is trying to iterate through the object like an array. +// For in loop would work better. \ No newline at end of file diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..c84f86c58 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -11,5 +11,10 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients: +${recipe.ingredients[0]} +${recipe.ingredients[1]} +${recipe.ingredients[2]} +${recipe.ingredients[3]}`); + +// On line 15 we can add recipe.ingredients[] diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..d2b26ee30 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} +function contains(object, property) { + for (let key in object) { + if (key === property) { + return true; + } + } + return false; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..8303cc021 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,20 +16,37 @@ as the object doesn't contains a key of 'c' // 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 +test("contains on an object with property returns true", function () { + expect(contains({ a: 1, b: 2 }, "a")).toBe(true); +}); +test("contains on an object without property returns false", function () { + expect(contains({ a: 1, b: 2 }, "d")).toBe(false); +}); // 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", function () { + 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("contain on object with properties", function () { + expect(contains({ c: 4, d: 5 }, "c")).toBe(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains on object with non-existent property name returns false", function () { + expect(contains({ c: 4, d: 5 }, "x")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contain on invalid parameters returns false", function () { + expect(contains([], "0")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..2ff7eb12c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,9 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + let obj = {}; + for (let pair of pairs) { + obj[pair[0]] = pair[1] + } + return obj; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..5c1c46e29 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", function() { + 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 45ec4e5f3..6cbbd6c95 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,7 +6,12 @@ function parseQueryString(queryString) { const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + if (!pair) { + continue; + } + const everything = pair.split("="); + const key = decodeURIComponent(everything.shift().replace(/\+/g, " ")); + const value = decodeURIComponent(everything.join("=").replace(/\+/g, " ")); queryParams[key] = value; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df61..11e8df52b 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({ @@ -40,9 +40,11 @@ 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", }); }); +*/ \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..04969f933 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,12 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + throw new Error("Invalid input"); + } + const count = {}; + for (const item of arr) { + count[item] = (count[item] || 0) + 1; + } + return count; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..c5a0029fd 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,27 @@ 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 on an array returns an object with item", function() { + 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("tally on an empty array returns an empty object", function() { + expect(tally([])).toEqual({}) +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("tally on duplicate items returns count of items", function() { + 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("tally on invalid input throws an error", function() { + expect(() => tally("string")).toThrow("Invalid input") +}) \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..5c5c7c93b 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,28 @@ 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 } +// - Ony 1 is printed // b) What is the current return value when invert is called with { a: 1, b: 2 } +// - 1 and 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? +// - object.entries() returns an array of a given object's own enumerable string keyed property key-value pair // d) Explain why the current return value is different from the target output +// - invertedObj only referenced the swapped value to key. We should have another variable to swap and reference key to value // e) Fix the implementation of invert (and write tests to prove it's fixed!) diff --git a/Sprint-2/interpret/inverts.test.js b/Sprint-2/interpret/inverts.test.js new file mode 100644 index 000000000..bf5e850f1 --- /dev/null +++ b/Sprint-2/interpret/inverts.test.js @@ -0,0 +1,10 @@ +const invert = require("./invert.js"); + +test("invert on an object with key-value returns key-value swapped", function () { + expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); + expect(invert({ distance1: 105, distance2: 201 })) + .toEqual({ 105: "distance1", 201: "distance2" }); + expect(invert({ fruit: "apple", veg: "carrot" })) + .toEqual({ "apple": "fruit", "carrot": "veg" }); +});