From f8f0510325af4dca1fa6e44148798a15e7b4ce64 Mon Sep 17 00:00:00 2001 From: Abrsh100 Date: Fri, 7 Nov 2025 12:16:30 +0000 Subject: [PATCH] fixing the errors encountered in the code.,and creatining a new function to pass the test --- Sprint-2/debug/address.js | 6 +++++- Sprint-2/debug/author.js | 7 ++++--- Sprint-2/debug/recipe.js | 2 +- Sprint-2/implement/contains.js | 10 +++++++-- Sprint-2/implement/contains.test.js | 23 +++++++++++++++++++- Sprint-2/implement/lookup.js | 4 +++- Sprint-2/implement/lookup.test.js | 17 ++++++++++++++- Sprint-2/implement/querystring.js | 17 ++++++++++----- Sprint-2/implement/querystring.test.js | 4 ++++ Sprint-2/implement/tally.js | 13 +++++++++++- Sprint-2/implement/tally.test.js | 17 +++++++++++++++ Sprint-2/interpret/invert.js | 29 +++++++++++++++++++++++++- Sprint-2/stretch/count-words.js | 17 +++++++++++++++ Sprint-2/stretch/mode.js | 27 ++++++++++++++---------- Sprint-2/stretch/mode.test.js | 1 + Sprint-2/stretch/till.js | 16 +++++++++++--- 16 files changed, 179 insertions(+), 31 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..8be2d7456 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -3,6 +3,10 @@ // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working +function postalAddress(postcode){ + const address = address.postcode + return address +} const address = { houseNumber: 42, @@ -12,4 +16,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.postcode}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..730243923 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -10,7 +10,8 @@ const author = { age: 40, alive: true, }; +console.log(author) -for (const value of author) { - console.log(value); -} +// for (const value of author) { +// console.log(author); +// } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..a413b3332 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -12,4 +12,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..090f629c9 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,9 @@ -function contains() {} - +function contains(object, properityValue){ + if (typeof object !== 'object' || object === null || Array.isArray(object)) { + return false; + } + + return properityValue in object; + +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..ec4b13bc2 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,37 @@ 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 an empty object returns false", () => { + const input = {}; + const result = contains(input, 'a'); + expect(result).toBe(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains an object with existing property returns true", () => { + const input = { a: 1, b: 2, c: 3 }; + const result = contains(input, 'b'); + expect(result).toBe(true); +}) // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains an object with non-existent property returns false", () => { + const input = { x: 10, y: 20, z: 30 }; + const result = contains(input, 'a'); + expect(result).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", () => { + const input = [1, 2, 3]; + const result = contains(input, '0'); + expect(result).toBe(false); +}) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..2007a332a 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,7 @@ -function createLookup() { +function createLookup(pairsArray) { // implementation here +const lookup = Object.fromEntries(pairsArray); +return lookup; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..1d9cce329 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,21 @@ 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 = [["UK","Pound"], ["EU", "Euro"], ["Kenya", "Ksh"], ["Norway", "NK"], ["Eritrea", "ENKF"], ["USA", "USD"], ["Japan", "JPY"]]; + const result = createLookup(input); + expect(result).toEqual({ + 'UK': 'Pound', + 'EU': 'Euro', + 'Kenya': 'Ksh', + 'Norway': 'NK', + 'Eritrea': 'ENKF', + 'USA': 'USD', + 'Japan': 'JPY' + }) +}); + /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..2eee82d7d 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +1,23 @@ function parseQueryString(queryString) { + const queryParams = {}; - if (queryString.length === 0) { - return queryParams; - } + if (!queryString) return queryParams; + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + const index = pair.indexOf("="); + if (index === -1) { + queryParams[pair] = undefined; + } else { + const key = pair.slice(0, index); + const value = pair.slice(index + 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..a31e379ca 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,7 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); + + test ("parses empty querystring", () => { + expect(parseQueryString("")).toEqual({}); + }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..d9d670cc9 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,14 @@ -function tally() {} +function tally(input) { + if (!Array.isArray(input)) { + throw new Error("Input must be an array"); + } + + const counts = {}; + for (let item of input) { + counts[item] = (counts[item] || 0) + 1; + } + + return counts; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..b75a3ec53 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -24,11 +24,28 @@ const tally = require("./tally.js"); // 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", () => { + const input = []; + const result = tally(input); + expect(result).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("tally on an array with duplicate items returns correct counts", () => { + const input = ['apple', 'banana', 'apple', 'orange', 'banana', 'apple']; + const result = tally(input); + expect(result).toEqual({apple: 3, banana: 2, orange: 1}); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error + +test("tally with invalid input throws an error", () => { + const input = "not an array"; + expect(() => { + tally(input); + }).toThrow("Input must be an array"); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..553f41cd6 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -17,13 +17,40 @@ function invert(obj) { } // a) What is the current return value when invert is called with { a : 1 } +console.log(invert({ a: 1 })); +// The current return value is { key: 1 } + // b) What is the current return value when invert is called with { a: 1, b: 2 } +console.log(invert({ a: 1, b: 2 })); +// The current return value is { key: 2 } + + +// c) What is the target return value when invert is called with {a : 1, b: 2} +// The target return value is { "1": "a", "2": "b" } -// c) What is the target return value when invert is called with {a : 1, b: 2} // c) What does Object.entries return? Why is it needed in this program? +// Object.entries returns an array of a key, value pairs, and also an object to array of arrays +// It is needed in this program to access and iterate over the key value pairs of the object,allowing us to process both the property names and their corresponding values. + // d) Explain why the current return value is different from the target output +/*The current return value is different from the target output +because in line 13 the invertedObj.key is assigning the string "key" as a property name, Not the actual value of the key variable.*/ + // e) Fix the implementation of invert (and write tests to prove it's fixed!) +function invert(obj) { + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + + return invertedObj; +} + +// Tests +console.log(invert({ a: 1 })); +console.log(invert({ a: 1, b: 2 })); diff --git a/Sprint-2/stretch/count-words.js b/Sprint-2/stretch/count-words.js index 8e85d19d7..561e65cfe 100644 --- a/Sprint-2/stretch/count-words.js +++ b/Sprint-2/stretch/count-words.js @@ -26,3 +26,20 @@ 3. Order the results to find out which word is the most common in the input */ +function countWords(input) { + + const wordCounts = {}; + const words = input.split(/\s+/); + + for (let word of words) { + + const cleanedWord = word.replace(/[.,!?;:()"]/g, '').toLowerCase(); + if (cleanedWord) { + wordCounts[cleanedWord] = (wordCounts[cleanedWord] || 0) + 1; + } + } + + return wordCounts; +} +console.log(countWords("Hello, hello, World, feed him or keep him world! Hello.")); +console.log(countWords("You and me and you and Me again.")); \ No newline at end of file diff --git a/Sprint-2/stretch/mode.js b/Sprint-2/stretch/mode.js index 3f7609d79..588e3d382 100644 --- a/Sprint-2/stretch/mode.js +++ b/Sprint-2/stretch/mode.js @@ -8,29 +8,34 @@ // 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) { - if (typeof num !== "number") { - continue; +function getFrequencies(list) { + // Track frequency of each value + const freqs = new Map(); + for (const num of list) { + if (typeof num === "number") { + freqs.set(num, (freqs.get(num) || 0) + 1); } - - freqs.set(num, (freqs.get(num) || 0) + 1); } + return freqs; +} +function findHighestFrequency(freqs) { // Find the value with the highest frequency let maxFreq = 0; let mode; - for (let [num, freq] of freqs) { + for (const [num, freq] of freqs) { if (freq > maxFreq) { mode = num; maxFreq = freq; } } - return maxFreq === 0 ? NaN : mode; } +function calculateMode(list) { + // Combine both stages + const freqs = getFrequencies(list); + return findHighestFrequency(freqs); +} + module.exports = calculateMode; diff --git a/Sprint-2/stretch/mode.test.js b/Sprint-2/stretch/mode.test.js index ca33c28a3..2fd6ace13 100644 --- a/Sprint-2/stretch/mode.test.js +++ b/Sprint-2/stretch/mode.test.js @@ -1,3 +1,4 @@ +// testing the mode.js const calculateMode = require("./mode.js"); // Acceptance criteria for calculateMode function diff --git a/Sprint-2/stretch/till.js b/Sprint-2/stretch/till.js index 6a08532e7..9c2c6d145 100644 --- a/Sprint-2/stretch/till.js +++ b/Sprint-2/stretch/till.js @@ -8,7 +8,9 @@ function totalTill(till) { let total = 0; for (const [coin, quantity] of Object.entries(till)) { - total += coin * quantity; + const numericCoin = parseInt(coin.replace('p', '')); + total += numericCoin * quantity; + } return `£${total / 100}`; @@ -23,9 +25,17 @@ const till = { const totalAmount = totalTill(till); // a) What is the target output when totalTill is called with the till object +// Target output: £4.10 -// b) Why do we need to use Object.entries inside the for...of loop in this function? +// b) Why do we need to use Object.entries inside the for...of loop in this function? +// We use Object.entries to convert the till object into an array of [key, value] pairs, allowing us to iterate over each coin type and its corresponding quantity easily. // c) What does coin * quantity evaluate to inside the for...of loop? +// Inside the for...of loop, coin * quantity evaluates to NaN (Not a Number) because coin is a string (e.g., "1p") and multiplying a string by a number results in NaN. + +// d) Write a test for this function to check it works and then fix the implementation of totalTill + -// d) Write a test for this function to check it works and then fix the implementation of totalTill +// Tests +console.log(totalTill(till)); +console.log(totalTill({ "2p": 5, "10p": 3 }));