diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..ff917efcf 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,11 @@ // Predict and explain first... +// This will log "My house number is undefined" because +// address[0] is looking for index 0 like it's an array, but address +// is an object. To get a value from an object you need to use the +// key name, not a number. Changing address[0] to address.houseNumber +// will fix it and log "My house number is 42". + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -12,4 +18,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); \ No newline at end of file diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..1f17654e4 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,6 @@ // Predict and explain first... +// This code will throw a TypeError: "author is not iterable". +// // 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 +13,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..133b10bd1 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,9 +1,14 @@ // Predict and explain first... +// This will not log the ingredients correctly. It will print the whole +// recipe object as [object Object] instead of each ingredient on its own line, +// because you're interpolating the entire object (`${recipe}`) instead of +// its properties and the ingredients array items. // 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? + const recipe = { title: "bruschetta", serves: 2, @@ -11,5 +16,8 @@ const recipe = { }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +ingredients:`); + +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..5104e0abc 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,11 @@ -function contains() {} +function contains(obj, propName) { + // Return false for non-objects or null + if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { + return false; + } + + // Check if the object has the property as its own key + return Object.hasOwn(obj, propName); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..0309083ae 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,35 +1,55 @@ const contains = require("./contains.js"); + /* Implement a function called contains that checks an object contains a particular property + E.g. contains({a: 1, b: 2}, 'a') // returns true as the object contains a key of 'a' + E.g. contains({a: 1, b: 2}, 'c') // returns false as the object doesn't contains a key of 'c' */ + // Acceptance criteria: + // 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 + // 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 with invalid parameters returns false", () => { + expect(contains([], "a")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..45c28dc96 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { +function createLookup(countryCurrencyPairs) { // implementation here + const lookup = {}; + + for (const [countryCode, currencyCode] of countryCurrencyPairs) { + lookup[countryCode] = currencyCode; + } + + return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..0b404334a 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,31 +1,54 @@ 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"], + ]; + + const result = createLookup(countryCurrencyPairs); + + expect(result).toEqual({ + US: "USD", + CA: "CAD", + GB: "GBP", + }); +}); + /* + Create a lookup object of key value pairs from an array of code pairs + Acceptance Criteria: + Given - An array of arrays representing country code and currency code pairs e.g. [['US', 'USD'], ['CA', 'CAD']] + When - createLookup function is called with the country-currency array as an argument + Then - It should return an object where: - The keys are the country codes - The values are the corresponding currency codes + Example Given: [['US', 'USD'], ['CA', 'CAD']] + When createLookup(countryCurrencyPairs) is called + Then It should return: { diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..1d3ea920d 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,14 +3,32 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + if (pair === "") { + continue; + } + + const indexOfEquals = pair.indexOf("="); + let key; + let value; + + if (indexOfEquals === -1) { + // no "=", treat whole pair as key with empty value + key = pair; + value = ""; + } else { + key = pair.slice(0, indexOfEquals); + value = pair.slice(indexOfEquals + 1); + } + queryParams[key] = value; } return queryParams; } + module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..a1ca896fb 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,34 @@ // 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") + test("parses querystring values containing =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ "equation": "x=y+1", }); }); + +test("parses empty querystring as empty object", () => { + expect(parseQueryString("")).toEqual({}); +}); + +test("parses single key with empty value", () => { + expect(parseQueryString("flag=")).toEqual({ flag: "" }); +}); + +test("parses key with no =", () => { + expect(parseQueryString("flag")).toEqual({ flag: "" }); +}); + +test("parses multiple key value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); + +test("later duplicate keys overwrite earlier ones", () => { + expect(parseQueryString("a=1&a=2")).toEqual({ a: "2" }); +}); + + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..c666a88d6 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,15 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("tally expects an array"); + } -module.exports = tally; + const counts = {}; + + for (const item of items) { + counts[item] = (counts[item] || 0) + 1; + } + + return counts; +} + +module.exports = tally; \ No newline at end of file diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..00be55545 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -1,5 +1,6 @@ const tally = require("./tally.js"); + /** * tally array * @@ -14,21 +15,39 @@ const tally = require("./tally.js"); * tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 } */ + // Acceptance criteria: + // Given a function called tally // When passed an array of items // Then it should return an object containing the count for each unique item + // 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 counts each unique item", () => { + expect(tally(["a"])).toEqual({ a: 1 }); + 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("tally throws on invalid input", () => { + expect(() => tally("not an array")).toThrow(); +}); + + + diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..81f7ad271 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,29 +1,49 @@ // Let's define how invert should work + // Given an object // When invert is passed this object // Then it should swap the keys and values in the object + // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} + 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 } +// Answer: { "1": "a" } + // b) What is the current return value when invert is called with { a: 1, b: 2 } +// Answer: { "1": "a", "2": "b" } + // c) What is the target return value when invert is called with {a : 1, b: 2} +// Answer: { "1": "a", "2": "b" } + // c) What does Object.entries return? Why is it needed in this program? +// Answer: Object.entries(obj) returns an array of [key, value] pairs, +// It is needed so we can loop over the object and easily get both key and value in the for...of loop using array destructuring: [key, value]. + // d) Explain why the current return value is different from the target output +// Answer: In this implementation, the current return value already matches the target output, because we correctly use the value as the new key +// and the key as the new value: invertedObj[value] = key. + // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// Answer: No changes needed to the implementation; it already works as required. + diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..e705e65af --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,17 @@ +const invert = require("./invert.js"); + +// Given an object +// When invert is passed this object +// Then it should swap the keys and values in the object + +test("inverts single key-value pair", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); +}); + +test("inverts multiple key-value pairs", () => { + expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" }); +}); + +test("inverts empty object to empty object", () => { + expect(invert({})).toEqual({}); +}); diff --git a/Sprint-2/stretch/count-words.js b/Sprint-2/stretch/count-words.js index 8e85d19d7..25bbd3e84 100644 --- a/Sprint-2/stretch/count-words.js +++ b/Sprint-2/stretch/count-words.js @@ -1,28 +1,55 @@ /* Count the number of times a word appears in a given string. + Write a function called countWords that - takes a string as an argument - returns an object where - the keys are the words from the string and - the values are the number of times the word appears in the string + Example If we call countWords like this: + countWords("you and me and you") then the target output is { you: 2, and: 2, me: 1 } + To complete this exercise you should understand - Strings and string manipulation - Loops - Comparison inside if statements - Setting values on an object + ## Advanced challenges + 1. Remove all of the punctuation (e.g. ".", ",", "!", "?") to tidy up the results + 2. Ignore the case of the words to find more unique words. e.g. (A === a, Hello === hello) + 3. Order the results to find out which word is the most common in the input */ + + + +function countWords(str) { + const counts = {}; + + const words = str.split(" ").filter((w) => w !== ""); + + for (const word of words) { + if (counts[word]) { + counts[word] += 1; + } else { + counts[word] = 1; + } + } + + return counts; +} + diff --git a/Sprint-2/stretch/mode.js b/Sprint-2/stretch/mode.js index 3f7609d79..988355538 100644 --- a/Sprint-2/stretch/mode.js +++ b/Sprint-2/stretch/mode.js @@ -1,18 +1,21 @@ // You are given an implementation of calculateMode + // calculateMode's implementation can be broken down into two stages: + // Stage 1. One part of the code tracks the frequency of each value // Stage 2. The other part finds the value with the highest frequency + // refactor calculateMode by splitting up the code // into smaller functions using the stages above -function calculateMode(list) { - // track frequency of each value - let freqs = new Map(); - for (let num of list) { +function getFrequencies(list) { + const freqs = new Map(); + + for (const num of list) { if (typeof num !== "number") { continue; } @@ -20,10 +23,14 @@ function calculateMode(list) { freqs.set(num, (freqs.get(num) || 0) + 1); } - // Find the value with the highest frequency + return freqs; +} + +function getModeFromFrequencies(freqs) { let maxFreq = 0; let mode; - for (let [num, freq] of freqs) { + + for (const [num, freq] of freqs) { if (freq > maxFreq) { mode = num; maxFreq = freq; @@ -33,4 +40,9 @@ function calculateMode(list) { return maxFreq === 0 ? NaN : mode; } +function calculateMode(list) { + const freqs = getFrequencies(list); + return getModeFromFrequencies(freqs); +} + module.exports = calculateMode; diff --git a/Sprint-2/stretch/mode.test.js b/Sprint-2/stretch/mode.test.js index ca33c28a3..dba406876 100644 --- a/Sprint-2/stretch/mode.test.js +++ b/Sprint-2/stretch/mode.test.js @@ -1,16 +1,20 @@ const calculateMode = require("./mode.js"); + // Acceptance criteria for calculateMode function + // Given an array of numbers // When calculateMode is called on the array // Then it should return the number that appears most frequently in the array + // Example: // Given [2,4,1,2,3,2,1] // When calculateMode is called on [2,4,1,2,3,2,1] // Then it should return 2 */ + describe("calculateMode()", () => { test("returns the most frequent number in an array", () => { const nums = [2, 4, 1, 2, 3, 2, 1]; diff --git a/Sprint-2/stretch/till.js b/Sprint-2/stretch/till.js index 6a08532e7..10e5d73c5 100644 --- a/Sprint-2/stretch/till.js +++ b/Sprint-2/stretch/till.js @@ -1,19 +1,23 @@ // totalTill takes an object representing coins in a till + // Given an object of coins // When this till object is passed to totalTill // Then it should return the total amount in pounds + function totalTill(till) { let total = 0; for (const [coin, quantity] of Object.entries(till)) { - total += coin * quantity; + const valueInPence = Number(coin.replace("p", "")); + total += valueInPence * quantity; } return `£${total / 100}`; } + const till = { "1p": 10, "5p": 6, @@ -22,10 +26,17 @@ const till = { }; const totalAmount = totalTill(till); + // a) What is the target output when totalTill is called with the till object +// "£4.4" + // b) Why do we need to use Object.entries inside the for...of loop in this function? +// Object.entries returns [coin, quantity] pairs, allowing destructuring in the loop + // c) What does coin * quantity evaluate to inside the for...of loop? +// Original: "1p" * 10 = NaN. Fixed: 1 * 10 = 10 (pence for that coin type) + -// d) Write a test for this function to check it works and then fix the implementation of totalTill +module.exports = totalTill; diff --git a/Sprint-2/stretch/totalTill.test.js b/Sprint-2/stretch/totalTill.test.js new file mode 100644 index 000000000..bf1fc563a --- /dev/null +++ b/Sprint-2/stretch/totalTill.test.js @@ -0,0 +1,6 @@ +const totalTill = require("./totalTill.js"); + +test("calculates total till value in pounds", () => { + const till = { "1p": 10, "5p": 6, "50p": 4, "20p": 10 }; + expect(totalTill(till)).toBe("£4.4"); +});