diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..d5a1ab36b 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,4 @@ -// Predict and explain first... +// Predict and explain first...It should report the house number as 42 // This code should log out the houseNumber from the address object // but it isn't working... @@ -12,4 +12,6 @@ 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..d20db7fec 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,4 @@ -// Predict and explain first... +// Predict and explain first...It should report 40 as the value // 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 +11,7 @@ const author = { alive: true, }; -for (const value of author) { +//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..23dd933da 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,4 @@ -// Predict and explain first... +// Predict and explain first...It should report the title, serves & ingredient but ingredient might have issues. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -10,6 +10,11 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +// 1. Log the title and servings +console.log(`${recipe.title} serves ${recipe.serves}`); +console.log("Ingredients:"); + +// 2. Loop through ingredients to log each on a new line +recipe.ingredients.forEach(ingredient => { + console.log(`- ${ingredient}`); +}); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..155713002 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,19 @@ +/* function contains() {} module.exports = contains; +*/ + +function contains(obj, propName) { + if ( + typeof obj !== 'object' || + obj === null || + (typeof propName !== 'string' && typeof propName !== 'number' && typeof propName !== 'symbol') + ) { + return false; + } + return propName in obj; +} + +module.exports = contains; + diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..57db983ba 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,7 @@ -function createLookup() { - // implementation here -} + +const createLookup = (countryCurrencyPairs) => { + return Object.fromEntries(countryCurrencyPairs); +}; + module.exports = createLookup; diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..03052c502 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,3 +1,4 @@ +/* function parseQueryString(queryString) { const queryParams = {}; if (queryString.length === 0) { @@ -12,5 +13,43 @@ function parseQueryString(queryString) { return queryParams; } + */ + + + + + + +function parseQueryString(queryString) { + if (!queryString || queryString.trim() === "") { + return {}; + } + + const cleanedString = queryString.startsWith("?") // check if it has ? and remocve + ? queryString.substring(1) + : queryString; + + const pairs = cleanedString.split("&"); //Split + const result = {}; + + for (const pair of pairs) { + // split on the FIRST '=' + const [key, ...valueParts] = pair.split("="); + const value = valueParts.join("="); // Rejoin if value contained '=' + + // Decode key and value to handle special characters + const decodedKey = decodeURIComponent(key); + const decodedValue = value ? decodeURIComponent(value) : ""; + + if (decodedKey) { + result[decodedKey] = decodedValue; + } + } + + return result; + +} + + module.exports = parseQueryString; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..b726ca92d 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,18 @@ -function tally() {} + + +function tally(arr) { + // Input validation + + if (!Array.isArray(arr)) { + throw new Error("Input must be an array"); + } + + // The reduction method effectively builds the frequency map + return arr.reduce((acc, item) => { + acc[item] = (acc[item] || 0) + 1; + return acc; + }, {}); +} + module.exports = tally; diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..b20f412fa 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -6,6 +6,7 @@ // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} + function invert(obj) { const invertedObj = {}; @@ -16,14 +17,38 @@ function invert(obj) { return invertedObj; } -// a) What is the current return value when invert is called with { a : 1 } +console.log(invert({ a: 1, b: 2 })); + + +// 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 } +// b) What is the current return value when invert is called with { a: 1, b: 2 }. ANSWER = { key: 2 } -// c) What is the target return value when invert is called with {a : 1, b: 2} +// 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: +// c(i) Object.entries(obj) returns an array of [key, value] pairs e.g ({ a: 1, b: 2 }) → [["a", 1], ["b", 2]] +// c(ii) Object.entries allows you to loop through both keys and values . // d) Explain why the current return value is different from the target output +//ANSWER: It is different because the line is assigning to the literal property "key" instead of using the variable key or value // 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; +} + + + +console.log(invert({ a: 1 })) +console.log(invert({ a: 1, b: 2 })) + +