diff --git a/Sprint-1/fix/median.js b/Sprint-1/fix/median.js index f74d1dc0..43cf1936 100644 --- a/Sprint-1/fix/median.js +++ b/Sprint-1/fix/median.js @@ -3,9 +3,18 @@ // If you're in the Sprint-1 directory, you can run `npm test -- fix` to run the tests in the fix directory function calculateMedian(list) { - const middleIndex = Math.floor(list.length / 2); - const median = list.splice(middleIndex, 1)[0]; - return median; + if (!list || list.length === 0) { + return null; + } + + const sortedList = list.slice().sort((a, b) => a - b); + const middleIndex = Math.floor(sortedList.length / 2); + + if (sortedList.length % 2 === 0) { + return (sortedList[middleIndex - 1] + sortedList[middleIndex]) / 2; + } else { + return sortedList[middleIndex]; + } } -module.exports = calculateMedian; +module.exports = calculateMedian; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.js b/Sprint-1/implement/dedupe.js index 781e8718..3de91804 100644 --- a/Sprint-1/implement/dedupe.js +++ b/Sprint-1/implement/dedupe.js @@ -1 +1,20 @@ -function dedupe() {} + +function dedupe(arr) { + if (!(arr instanceof Array)) { + return []; + } + + const seen = new Set(); + const result = []; + + for (const item of arr) { + if (!seen.has(item)) { + seen.add(item); + result.push(item); + } + } + + return result; + } + + module.exports = dedupe; \ No newline at end of file diff --git a/Sprint-1/implement/dedupe.test.js b/Sprint-1/implement/dedupe.test.js index 23e0f863..47c31bd6 100644 --- a/Sprint-1/implement/dedupe.test.js +++ b/Sprint-1/implement/dedupe.test.js @@ -1,4 +1,5 @@ const dedupe = require("./dedupe.js"); + /* Dedupe Array @@ -13,15 +14,50 @@ E.g. dedupe([1, 2, 1]) target output: [1, 2] // Acceptance Criteria: -// Given an empty array -// When passed to the dedupe function -// Then it should return an empty array -test.todo("given an empty array, it returns an empty array"); +describe('dedupe', () => { + // Given an empty array + // When passed to the dedupe function + // Then it should return an empty array + it("given an empty array, it returns an empty array", () => { + expect(dedupe([])).toEqual([]); + }); + + // Given an array with no duplicates + // When passed to the dedupe function + // Then it should return a copy of the original array + it("given an array with no duplicates, it returns a copy of the original array", () => { + const arr = [1, 2, 3]; + expect(dedupe(arr)).toEqual(arr); + }); + + it("given an array of strings with no duplicates, it returns a copy of the original array", () => { + const arr = ['a', 'b', 'c']; + expect(dedupe(arr)).toEqual(arr); + }); + + // Given an array with strings or numbers + // When passed to the dedupe function + // Then it should remove the duplicate values, preserving the first occurence of each element + it("given an array with duplicate numbers, it removes the duplicate values, preserving the first occurrence", () => { + expect(dedupe([5, 1, 1, 2, 3, 2, 5, 8])).toEqual([5, 1, 2, 3, 8]); + }); + + it("given an array with duplicate strings, it removes the duplicate values, preserving the first occurrence", () => { + expect(dedupe(['a', 'a', 'a', 'b', 'b', 'c'])).toEqual(['a', 'b', 'c']); + }); + + it("given an array with mixed duplicate types, it removes the duplicate values, preserving the first occurrence", () => { + expect(dedupe([1, '1', 1, '2', 2])).toEqual([1, '1', '2', 2]); + }); -// Given an array with no duplicates -// When passed to the dedupe function -// Then it should return a copy of the original array + it("given an array with duplicate values in different order, it removes the duplicate values, preserving the first occurrence", () => { + expect(dedupe([1, 2, 1])).toEqual([1, 2]); + }); -// Given an array with strings or numbers -// When passed to the dedupe function -// Then it should remove the duplicate values, preserving the first occurence of each element + it("given an invalid input, it returns an empty array", () => { + expect(dedupe(null)).toEqual([]); + expect(dedupe(undefined)).toEqual([]); + expect(dedupe({})).toEqual([]); + expect(dedupe("string")).toEqual([]); + }); +}); \ No newline at end of file diff --git a/Sprint-1/implement/max.js b/Sprint-1/implement/max.js index 6dd76378..fdef3d3f 100644 --- a/Sprint-1/implement/max.js +++ b/Sprint-1/implement/max.js @@ -1,4 +1,15 @@ function findMax(elements) { -} - -module.exports = findMax; + if (!elements || elements.length === 0) { + return -Infinity; + } + + let max = -Infinity; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === 'number' && elements[i] > max) { + max = elements[i]; + } + } + return max; + } + + module.exports = findMax; \ No newline at end of file diff --git a/Sprint-1/implement/max.test.js b/Sprint-1/implement/max.test.js index 82f18fd8..ed1cde7d 100644 --- a/Sprint-1/implement/max.test.js +++ b/Sprint-1/implement/max.test.js @@ -15,29 +15,48 @@ const findMax = require("./max.js"); // Given an empty array // When passed to the max function // Then it should return -Infinity -// Delete this test.todo and replace it with a test. -test.todo("given an empty array, returns -Infinity"); +test("given an empty array, returns -Infinity", () => { + expect(findMax([])).toBe(-Infinity); +}); // Given an array with one number // When passed to the max function // Then it should return that number +test("given an array with one number, returns that number", () => { + expect(findMax([5])).toBe(5); +}); // Given an array with both positive and negative numbers // When passed to the max function // Then it should return the largest number overall +test("given an array with both positive and negative numbers, returns the largest number", () => { + expect(findMax([-10, 5, 20, -5])).toBe(20); +}); // Given an array with just negative numbers // When passed to the max function // Then it should return the closest one to zero +test("given an array with just negative numbers, returns the closest one to zero", () => { + expect(findMax([-10, -5, -20, -1])).toBe(-1); +}); // Given an array with decimal numbers // When passed to the max function // Then it should return the largest decimal number +test("given an array with decimal numbers, returns the largest decimal number", () => { + expect(findMax([1.5, 2.7, 1.1, 3.9])).toBe(3.9); +}); // Given an array with non-number values // When passed to the max function // Then it should return the max and ignore non-numeric values +test("given an array with non-number values, returns the max and ignores non-numeric values", () => { + expect(findMax(["hey", 10, "hi", 60, 10])).toBe(60); +}); // Given an array with only non-number values // When passed to the max function // Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, returns -Infinity", () => { + expect(findMax(["a","b","c"])).toBe(-Infinity); +}); \ No newline at end of file diff --git a/Sprint-1/implement/sum.js b/Sprint-1/implement/sum.js index 9062aafe..7fb53b6f 100644 --- a/Sprint-1/implement/sum.js +++ b/Sprint-1/implement/sum.js @@ -1,4 +1,14 @@ function sum(elements) { -} - -module.exports = sum; + if (!Array.isArray(elements)) { + return 0; // Handle cases where input is not an array + } + let total = 0; + for (let i = 0; i < elements.length; i++) { + if (typeof elements[i] === 'number') { + total += elements[i]; + } + } + return total; + } + + module.exports = sum; \ No newline at end of file diff --git a/Sprint-1/implement/sum.test.js b/Sprint-1/implement/sum.test.js index dd0a090c..597c76c8 100644 --- a/Sprint-1/implement/sum.test.js +++ b/Sprint-1/implement/sum.test.js @@ -1,11 +1,3 @@ -/* Sum the numbers in an array - -In this kata, you will need to implement a function that sums the numerical elements of an array - -E.g. sum([10, 20, 30]), target output: 60 -E.g. sum(['hey', 10, 'hi', 60, 10]), target output: 80 (ignore any non-numerical elements) -*/ - const sum = require("./sum.js"); // Acceptance Criteria: @@ -13,24 +5,53 @@ const sum = require("./sum.js"); // Given an empty array // When passed to the sum function // Then it should return 0 -test.todo("given an empty array, returns 0") +test("given an empty array, returns 0", () => { + expect(sum([])).toBe(0); +}); // Given an array with just one number // When passed to the sum function // Then it should return that number +test("given an array with one number, returns that number", () => { + expect(sum([5])).toBe(5); +}); // Given an array containing negative numbers // When passed to the sum function // Then it should still return the correct total sum +test("given an array with negative numbers, returns correct total", () => { + expect(sum([-1, -2, 3])).toBe(0); +}); // Given an array with decimal/float numbers // When passed to the sum function // Then it should return the correct total sum +test("given an array with decimal numbers, returns correct total", () => { + expect(sum([1.5, 2.5, 3])).toBe(7); +}); // Given an array containing non-number values // When passed to the sum function // Then it should ignore the non-numerical values and return the sum of the numerical elements +test("given an array with mixed values, ignores non-numbers", () => { + expect(sum(['hey', 10, 'hi', 60, 10])).toBe(80); +}); // Given an array with only non-number values // When passed to the sum function // Then it should return the least surprising value given how it behaves for all other inputs +test("given an array with only non-number values, returns 0", () => { + expect(sum(['a','b','c'])).toBe(0); +}); + +test("given a null input, returns 0", () => { + expect(sum(null)).toBe(0); +}); + +test("given an undefined input, returns 0", () => { + expect(sum(undefined)).toBe(0); +}); + +test("given an object input, returns 0", () => { + expect(sum({})).toBe(0); +}); \ No newline at end of file diff --git a/Sprint-1/refactor/includes.js b/Sprint-1/refactor/includes.js index 29dad81f..e0efbea9 100644 --- a/Sprint-1/refactor/includes.js +++ b/Sprint-1/refactor/includes.js @@ -1,4 +1,4 @@ -// Refactor the implementation of includes to use a for...of loop +/*// Refactor the implementation of includes to use a for...of loop function includes(list, target) { for (let index = 0; index < list.length; index++) { @@ -11,3 +11,14 @@ function includes(list, target) { } module.exports = includes; +*/ +function includes(list, target) { + for (const element of list) { + if (element === target) { + return true; + } + } + return false; +} + +module.exports = includes; diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af8..1055eeab 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... - +/*address is object because we used curly bracket */ // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -12,4 +12,9 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +//console.log(`My house number is ${address[0]}`); +// Using dot notation: +console.log(`My house number is ${address.houseNumber}`); + +// Or +console.log(`My house number is ${address["houseNumber"]}`); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c212597..7986dd9a 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,8 +1,10 @@ // Predict and explain first... - +/*Objects are collections of key-value pairs*/ // 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 - +// But it isn't working. Explain why first and then fix the +problem +/*. The for...of loop +expects a sequence of values it can step through. +Objects don't provide that sequence directly.*/ const author = { firstName: "Zadie", lastName: "Smith", @@ -11,6 +13,10 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +//for (const value of author) { + //console.log(value);} + for (const value of Object.values(author)) { + console.log(value); } +/* Object.keys(): returns an array of the object's property names. +Object.entries(): returns an array of key-value pairs (arrays).*/ diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22c..68c6c677 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,15 +1,25 @@ // Predict and explain first... +/* recipe is an object we can not put directly in a template literal string output :bruschetta serves 2 + ingredients: +[object Object]*/ // 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? +/* Using a foreach loop is a good opportunity to destructure objects*/ + const recipe = { title: "bruschetta", serves: 2, ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +//console.log(`${recipe.title} serves ${recipe.serves} +//ingredients: +//${recipe}`); +console.log(`${recipe.title} serves ${recipe.serves}`); +console.log("ingredients:"); +recipe.ingredients.forEach(ingredient => { + console.log(ingredient); +}); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308..7c327c68 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,13 @@ -function contains() {} +function contains(obj, property) { + if (typeof obj !== 'object' || obj === null) { + return false; // Handle non-object inputs + } -module.exports = contains; + if (Array.isArray(obj)) { + return false; // Arrays should never return true + } + + return obj.hasOwnProperty(property); +} + +module.exports = contains; \ No newline at end of file diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f..070c3a11 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -1,7 +1,8 @@ const contains = require("./contains.js"); /* -Implement a function called contains that checks an object contains a +Implement a function called contains that checks +an object contains a particular property E.g. contains({a: 1, b: 2}, 'a') // returns true @@ -20,16 +21,28 @@ 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 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 with existing property returns true", () => { + 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 with non-existent property returns false", () => { + 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 array returns false", () => { + expect(contains([1, 2, 3], "0")).toBe(false); +}); + diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07..61728e4c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,25 @@ -function createLookup() { - // implementation here +// implement/lookup.js +function createLookup(items, keySelector, valueSelector) { + if (!Array.isArray(items)) { + return {}; + } + + if (typeof keySelector !== 'function' || typeof valueSelector !== 'function') { + throw new Error('keySelector and valueSelector must be functions'); + } + + const lookup = {}; + + for (const item of items) { + const key = keySelector(item); + const value = valueSelector(item); + + if (key !== undefined) { + lookup[key] = value; + } + } + + return lookup; } -module.exports = createLookup; +module.exports = createLookup; \ No newline at end of file diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5..fd2e5818 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,35 +1,37 @@ -const createLookup = require("./lookup.js"); - -test.todo("creates a country currency code lookup for multiple codes"); - -/* - -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' - } -*/ +const createLookup = require('./lookup'); + +describe('createLookup', () => { + const people = [ + { id: 1, name: 'Alice' }, + { id: 2, name: 'Bob' }, + { id: 3, name: 'Charlie' }, + ]; + + test('creates a lookup object with IDs as keys and names as values', () => { + const idLookup = createLookup( + people, + (person) => person.id, + (person) => person.name + ); + expect(idLookup).toEqual({ '1': 'Alice', '2': 'Bob', '3': 'Charlie' }); + }); + + test('creates a lookup object with names as keys and IDs as values', () => { + const nameLookup = createLookup( + people, + (person) => person.name, + (person) => person.id + ); + expect(nameLookup).toEqual({ Alice: 1, Bob: 2, Charlie: 3 }); + }); + + test('returns an empty object when given an empty array', () => { + const emptyLookup = createLookup([], (item) => item, (item) => item); + expect(emptyLookup).toEqual({}); + }); + + test('returns an empty object when given a non-array', () => { + const nonArrayLookup = createLookup('not an array', (item) => item, (item) => item); + expect(nonArrayLookup).toEqual({}); + }); +}); \ No newline at end of file diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f..9d0e2a7d 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +1,51 @@ function parseQueryString(queryString) { const queryParams = {}; + + // Return empty object if query string is empty if (queryString.length === 0) { return queryParams; } + + // Split query string into key-value pairs const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + // Find the first "=" to split into key and value + const equalIndex = pair.indexOf("="); + + let key, value; + if (equalIndex === -1) { + // If there's no "=", treat the entire pair as the key with a null value + key = pair; + value = null; + } else { + // Split into key and value + key = pair.substring(0, equalIndex); + value = pair.substring(equalIndex + 1); + } + + // Skip if key is empty + if (key) { + // Decode key, replacing "+" with spaces + const decodedKey = decodeURIComponent(key.replace(/\+/g, " ")); + + // Decode value, but do not replace "+" with spaces + const decodedValue = value ? decodeURIComponent(value) : null; + + // Handle duplicate keys by storing values in an array + if (queryParams.hasOwnProperty(decodedKey)) { + if (Array.isArray(queryParams[decodedKey])) { + queryParams[decodedKey].push(decodedValue); + } else { + queryParams[decodedKey] = [queryParams[decodedKey], decodedValue]; + } + } else { + queryParams[decodedKey] = decodedValue; + } + } } return queryParams; } -module.exports = parseQueryString; +module.exports = parseQueryString; \ No newline at end of file diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b78..f4410887 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -1,12 +1,37 @@ -// 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 querystring with no value", () => { + expect(parseQueryString("key&key2")).toEqual({ + key: null, + key2: null, + }); +}); + +test("parses querystring with empty key", () => { + expect(parseQueryString("=value&key2=value2")).toEqual({ + key2: "value2", }); }); + +test("parses querystring with URL-encoded characters", () => { + expect(parseQueryString("key=value%20with%20spaces&key2=value%3Dwith%3Dequals")).toEqual({ + key: "value with spaces", + key2: "value=with=equals", + }); +}); + +test("parses querystring with duplicate keys", () => { + expect(parseQueryString("key=value1&key=value2")).toEqual({ + key: ["value1", "value2"], + }); +}); + +test("parses empty querystring", () => { + expect(parseQueryString("")).toEqual({}); +}); \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f4732181..2f24be9b 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,25 @@ -function tally() {} - -module.exports = tally; +function tally(items) { + // Handle invalid input + if (!Array.isArray(items)) { + throw new Error("Invalid input: Expected an array"); + } + + // Create an empty object to store the counts + const result = {}; + + // Iterate over each item in the array + for (const item of items) { + // If the item is already in the result object, increment its count + if (result[item]) { + result[item]++; + } else { + // Otherwise, add the item to the result object with a count of 1 + result[item] = 1; + } + } + + // Return the result object + return result; + } + + 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 2ceffa8d..455804db 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -1,34 +1,26 @@ 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 } - */ +// Test 1: Tally on an empty array returns an empty object +test("tally on an empty array returns an empty object", () => { + expect(tally([])).toEqual({}); +}); -// Acceptance criteria: +// Test 2: Tally on an array with duplicate items +test("tally on an array with duplicate items", () => { + expect(tally(['a', 'a', 'b', 'c'])).toEqual({ a: 2, b: 1, c: 1 }); +}); -// 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 3: Tally on an array with a single item +test("tally on an array with a single item", () => { + 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 4: Tally on an array with all identical items +test("tally on an array with all identical items", () => { + expect(tally(['a', 'a', 'a'])).toEqual({ a: 3 }); +}); -// Given an array with duplicate items -// When passed to tally -// Then it should return counts for each unique item - -// Given an invalid input like a string -// When passed to tally -// Then it should throw an error +// Test 5: Tally on invalid input (string) +test("tally on invalid input throws an error", () => { + expect(() => tally("invalid")).toThrow("Invalid input: Expected an array"); +}); \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1..5e626f3b 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -17,13 +17,47 @@ function invert(obj) { } // a) What is the current return value when invert is called with { a : 1 } - +/*When invert is called with { a: 1 }, the loop runs once, and invertedObj.key += value assigns 1 to the key "key". The result is: + */ // b) What is the current return value when invert is called with { a: 1, b: 2 } - +/*{ a: 1, b: 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 key-value pairs for the given object. +Object.entries({ a: 1, b: 2 }) returns [["a", 1], ["b", 2]] // d) Explain why the current return value is different from the target output + /* It uses a fixed key "key" (invertedObj.key = value) instead of dynamically + using the value as the key. + + It overwrites the same key "key" in each iteration, losing the previous + key-value pair. + */ +// e) Fix the implementation o#f 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; // Use the value as the new key + } -// e) Fix the implementation of invert (and write tests to prove it's fixed!) + return invertedObj; +} +console.log(invert({ a: 1 })); +console.log(invert({ a: 1, b: 2 })); +console.log(invert({ x: 10, y: 20 })); +console.log(invert({})); // {} +console.log(invert({ foo: "bar", baz: "qux" })); +/*$ cd interpret + +sripa@LAPTOP-CLQ688JA MINGW64 ~/Desktop/CYF/Module-Data-Groups/sprint-2/interpret (main) +$ node invert.js +{ '1': 'a' } +{ '1': 'a', '2': 'b' } +{ '10': 'x', '20': 'y' } +{} +{ bar: 'foo', qux: 'baz' } + +sripa@LAPTOP-CLQ688JA MINGW64 ~/Desktop/CYF/Module-Data-Groups/sprint-2/interpret (main) +$ */ \ No newline at end of file