diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..7ea2ffa88 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,6 @@ -// Predict and explain first... +// The original code used address[0], which doesn't work because +// address is an object, not an array. Objects have named keys, not numeric indexes. +// So address[0] is undefined. // This code should log out the houseNumber from the address object // but it isn't working... @@ -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}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..5f2778f0b 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,7 +1,7 @@ -// Predict and explain first... - -// 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 +// Prediction and explanation: +// The original code uses `for (const value of author)`, but `author` is an object, +// not an array or other iterable. This will throw a TypeError. +// To fix it, we need to loop over the object's values using Object.values(). const author = { firstName: "Zadie", @@ -11,6 +11,8 @@ const author = { alive: true, }; -for (const value of author) { +// Fix: use Object.values() to get an array of the values +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..478ab5e8c 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,8 +1,8 @@ -// Predict and explain first... - -// 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? +// Prediction and explanation: +// The current code tries to log the whole `recipe` object with `${recipe}`, +// but objects are not automatically formatted in a readable way in a template string. +// This will print something like "[object Object]" instead of the ingredients on separate lines. +// To fix it, we need to access the `ingredients` array and log each ingredient individually. const recipe = { title: "bruschetta", @@ -10,6 +10,10 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; +// Fix: log title and serves, then loop over ingredients to print each on a new line 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..8d7ff5e4a 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,12 @@ -function contains() {} +function contains(obj, prop) { + // Check if the input is a valid object and prop is a string + if (obj === null || typeof obj !== "object" || Array.isArray(obj)) { + return false; + } + + // Use Object.hasOwn to check if property exists + return Object.hasOwn(obj, prop); +} module.exports = contains; + diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..5defb9f55 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,35 +1,31 @@ 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 when object has the 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 when object does not have the 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 +// Then it should return false +test("contains returns false for invalid input like arrays", () => { + expect(contains([], "a")).toBe(false); + expect(contains(null, "a")).toBe(false); +}); + diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..1b3d6c25a 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,15 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + // Validate input: must be an array of arrays + if (!Array.isArray(pairs)) { + return {}; + } + + const lookup = {}; + for (const [country, currency] of pairs) { + 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 547e06c5a..aea5e7217 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,35 +1,26 @@ 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 input = [ + ["US", "USD"], + ["CA", "CAD"], + ["GB", "GBP"], + ]; + const result = createLookup(input); + + expect(result).toEqual({ + US: "USD", + CA: "CAD", + GB: "GBP", + }); +}); + +test("returns empty object when given an empty array", () => { + expect(createLookup([])).toEqual({}); +}); + +test("returns empty object when given invalid input", () => { + expect(createLookup(null)).toEqual({}); + expect(createLookup("not an array")).toEqual({}); +}); -/* - -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: - { - 'US': 'USD', - 'CA': 'CAD' - } -*/ diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..b164b36f5 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +1,60 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { - return queryParams; + + // Guard: empty or falsy input + if (!queryString) return queryParams; + + // Remove leading '?' + 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("&").filter((p) => p.length > 0); + + const safeDecode = (s) => { + // Replace + with space before decoding + const replaced = s.replace(/\+/g, " "); + try { + return decodeURIComponent(replaced); + } catch (e) { + // If decoding fails, return the replaced string as-is + return replaced; + } + }; + + for (const pair of pairs) { + const idx = pair.indexOf("="); + + let key; + let value; + if (idx === -1) { + // No '=' present: treat as key with empty string value + key = pair; + value = ""; + } else { + // Split on first '=' only + key = pair.slice(0, idx); + value = pair.slice(idx + 1); + } + + const decodedKey = safeDecode(key); + const decodedValue = safeDecode(value); + + if (Object.prototype.hasOwnProperty.call(queryParams, decodedKey)) { + const existing = queryParams[decodedKey]; + if (Array.isArray(existing)) { + existing.push(decodedValue); + } else { + queryParams[decodedKey] = [existing, decodedValue]; + } + } else { + queryParams[decodedKey] = decodedValue; + } } return queryParams; } module.exports = parseQueryString; + diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..62faf1b2d 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -1,12 +1,33 @@ -// In the prep, we implemented a function to parse query strings. -// Unfortunately, it contains several bugs! -// 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("parses basic key/value pairs", () => { + expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" }); +}); + +test("handles empty query string", () => { + expect(parseQueryString("")).toEqual({}); + expect(parseQueryString("?")).toEqual({}); +}); + +test("handles keys without value and keys with empty value", () => { + expect(parseQueryString("noValue")).toEqual({ noValue: "" }); + expect(parseQueryString("empty=")).toEqual({ empty: "" }); +}); + +test("decodes percent-encoding and plus signs", () => { + expect(parseQueryString("name=John%20Doe&query=hello+world")).toEqual({ + name: "John Doe", + query: "hello world", + }); +}); + +test("collects duplicate keys into arrays in order", () => { + expect(parseQueryString("a=1&a=2&a=3")).toEqual({ a: ["1", "2", "3"] }); +}); + diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..0e7c78c49 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("Input must be an array"); + } + +const result = Object.create(null); + for (const item of items) { + if (result[item]) { + result[item] ++; + } else { + result[item] = 1; + } + } + return result; +} module.exports = tally; + diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..1acb0ef2b 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -1,34 +1,27 @@ const tally = require("./tally.js"); -/** - * tally array - * - * In this task, you'll need to implement a function called tally - * that will take a list of items and count the frequency of each item - * in an array - * - * For example: - * - * tally(['a']), target output: { a: 1 } - * tally(['a', 'a', 'a']), target output: { a: 3 } - * 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 items correctly", () => { + 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 error for invalid input", () => { + expect(() => tally("not an array")).toThrow("Input must be an array"); + expect(() => tally(null)).toThrow("Input must be an array"); + expect(() => tally(123)).toThrow("Input must be an array"); +}); + diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..d38c98b3b 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -1,5 +1,3 @@ -// 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 @@ -10,20 +8,32 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; // fix: use computed property instead of "key" } return invertedObj; } +module.exports = invert; + // a) What is the current return value when invert is called with { a : 1 } +// Answer: { key: 1 } // b) What is the current return value when invert is called with { a: 1, b: 2 } +// Answer: { key: 2 } (the second assignment overwrites the first) // 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. +// Example: Object.entries({a:1, b:2}) → [["a",1],["b",2]]. +// It’s needed because for...of can iterate over these pairs easily. // d) Explain why the current return value is different from the target output +// Answer: The code used invertedObj.key = value, which creates a property literally +// called "key". We need invertedObj[value] = key to swap properly. // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// Answer: Fixed above by changing to invertedObj[value] = key. + diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..e4aaaa675 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,20 @@ +const invert = require("./invert.js"); + +test("inverts a 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("works with string values", () => { + expect(invert({ x: "hello", y: "world" })).toEqual({ + hello: "x", + world: "y", + }); +}); + +test("returns empty object when input is empty", () => { + expect(invert({})).toEqual({}); +}); diff --git a/Sprint-2/stretch/count-words.js b/Sprint-2/stretch/count-words.js index 8e85d19d7..9e3816fe2 100644 --- a/Sprint-2/stretch/count-words.js +++ b/Sprint-2/stretch/count-words.js @@ -1,28 +1,70 @@ -/* - Count the number of times a word appears in a given string. +// Basic implementation of word counting + + +// Given a string of words +// When the string is passed to countWords +// Then it should return an object with the count of each word + +// Example: +// Given "you and me and you" +// When countWords is called on this string +// Then it should return { you: 2, and: 2, me: 1 } - 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 +function countWords(str) { + const counts = {}; - Example - If we call countWords like this: + // ✅ Basic: split on spaces + const words = str.split(" "); - countWords("you and me and you") then the target output is { you: 2, and: 2, me: 1 } + for (const word of words) { + if (!counts[word]) { + counts[word] = 1; + } else { + counts[word]++; + } + } - To complete this exercise you should understand - - Strings and string manipulation - - Loops - - Comparison inside if statements - - Setting values on an object + return counts; +} + +module.exports = countWords; + + +// Advanced challenges are implemented in countWordsAdvanced +/* +### Advanced challenges (implemented separately) -## Advanced challenges +1. Remove punctuation + → Use regex to strip punctuation before splitting. -1. Remove all of the punctuation (e.g. ".", ",", "!", "?") to tidy up the results +2. Ignore case + → Convert the string to lowercase before counting. -2. Ignore the case of the words to find more unique words. e.g. (A === a, Hello === hello) +3. Order results + → Convert the counts object into an array, sort it, and return it in order. -3. Order the results to find out which word is the most common in the input */ + +function countWordsAdvanced(str) { + const counts = Object.create(null); + + // 1. Remove punctuation + const noPunctuation = str.replace(/[.,!?]/g, ""); + + // 2. Ignore case + const words = noPunctuation.toLowerCase().split(/\s+/); + + for (const word of words) { + if (!word) continue; // skip empty strings + counts[word] = (counts[word] || 0) + 1; + } + + // 3. Order results (most common first) + const sortedEntries = Object.entries(counts).sort((a, b) => b[1] - a[1]); + + return Object.fromEntries(sortedEntries); +} + +// Example usage +// console.log(countWords("you and me and you")); +// console.log(countWordsAdvanced("You, you! and me? And YOU.")); \ No newline at end of file diff --git a/Sprint-2/stretch/mode.js b/Sprint-2/stretch/mode.js index 3f7609d79..b16a42a95 100644 --- a/Sprint-2/stretch/mode.js +++ b/Sprint-2/stretch/mode.js @@ -1,28 +1,22 @@ -// 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 +// Stage 1. Track frequency of each value +function buildFrequencyMap(list) { let freqs = new Map(); for (let num of list) { if (typeof num !== "number") { - continue; + continue; // skip non-number values } - freqs.set(num, (freqs.get(num) || 0) + 1); } - // Find the value with the highest frequency + return freqs; +} + +// Stage 2. Find the value with the highest frequency +function findMode(freqs) { let maxFreq = 0; let mode; + for (let [num, freq] of freqs) { if (freq > maxFreq) { mode = num; @@ -33,4 +27,11 @@ function calculateMode(list) { return maxFreq === 0 ? NaN : mode; } +// Main function +function calculateMode(list) { + const freqs = buildFrequencyMap(list); + return findMode(freqs); +} + module.exports = calculateMode; + diff --git a/Sprint-2/stretch/till.js b/Sprint-2/stretch/till.js index 6a08532e7..1b051be9b 100644 --- a/Sprint-2/stretch/till.js +++ b/Sprint-2/stretch/till.js @@ -8,12 +8,17 @@ function totalTill(till) { let total = 0; for (const [coin, quantity] of Object.entries(till)) { - total += coin * quantity; + // coin is a string like "1p", "20p" + // We need to strip the "p" and convert it into a number of pennies + const coinValue = parseInt(coin, 10); + total += coinValue * quantity; } - return `£${total / 100}`; + return `£${(total / 100).toFixed(2)}`; } +module.exports = totalTill; + const till = { "1p": 10, "5p": 6, @@ -23,9 +28,17 @@ const till = { const totalAmount = totalTill(till); // a) What is the target output when totalTill is called with the till object +// Answer: £3.60 (10x1p + 6x5p + 4x50p + 10x20p = 360p = £3.60) // b) Why do we need to use Object.entries inside the for...of loop in this function? +// Answer: Object.entries(till) returns an array of [coin, quantity] pairs, +// allowing us to iterate over both the coin name (e.g. "50p") and its count. // c) What does coin * quantity evaluate to inside the for...of loop? +// Answer (before fix): NaN, because coin is a string like "50p". +// After fix: coinValue * quantity gives the total pennies for that coin type. // d) Write a test for this function to check it works and then fix the implementation of totalTill +// Answer: Fix shown above to parse coin values correctly. + + diff --git a/Sprint-2/stretch/till.test.js b/Sprint-2/stretch/till.test.js new file mode 100644 index 000000000..255a0db88 --- /dev/null +++ b/Sprint-2/stretch/till.test.js @@ -0,0 +1,14 @@ +// Example test (till.test.js): + +const totalTill = require("./till.js"); + +describe("totalTill()", () => { + test("calculates the total amount in pounds", () => { + const till = { "1p": 10, "5p": 6, "50p": 4, "20p": 10 }; + expect(totalTill(till)).toBe("£3.60"); + }); + + test("returns £0.00 for an empty till", () => { + expect(totalTill({})).toBe("£0.00"); + }); +});