From 5007c4b2e7454bed90a8cb18e7332329a4f67932 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Thu, 30 Oct 2025 01:35:50 +0000 Subject: [PATCH 01/16] added all tests to test, started mean.js but not finished --- prep/mean.js | 18 ++++++++++++++++++ prep/mean.test.js | 22 ++++++++++++++++++++++ prep/package.json | 16 ++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 prep/mean.js create mode 100644 prep/mean.test.js create mode 100644 prep/package.json diff --git a/prep/mean.js b/prep/mean.js new file mode 100644 index 000000000..31f463f9c --- /dev/null +++ b/prep/mean.js @@ -0,0 +1,18 @@ +//a data structure is used to group data together. it is a collection of data which has functions which allow you to access and manipulate the data. +//an array is a zero indexed collection of data that holds data in an ordered list. +function calculateMean(list) { + let total = 0; + + for (const item of list) { + total += item; + } +} + +function calculateMedian(list) { + const middleIndex = Math.floor(list.length / 2); + const median = list.splice(middleIndex, 1)[0]; + + return median; +} + +calculateMean([10, 20, 30, 40, 50]); diff --git a/prep/mean.test.js b/prep/mean.test.js new file mode 100644 index 000000000..386732522 --- /dev/null +++ b/prep/mean.test.js @@ -0,0 +1,22 @@ +test("calculates the mean of a list of numbers", () => { + const list = [3, 50, 7]; + const currentOutput = calculateMean(list); + const targetOutput = 20; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("calculates the median of a list of odd length", () => { + const list = [10, 20, 30, 50, 60]; + const currentOutput = calculateMedian(list); + const targetOutput = 30; + + expect(currentOutput).toEqual(targetOutput); +}); + +test("doesn't modify the input", () => { + const list = [1, 2, 3]; + calculateMedian(list); + + expect(list).toEqual([1, 2, 3]); // Note that the toEqual matcher checks the values inside arrays when comparing them - it doesn't use `===` on the arrays, we know that would always evaluate to false. +}); diff --git a/prep/package.json b/prep/package.json new file mode 100644 index 000000000..2301e1638 --- /dev/null +++ b/prep/package.json @@ -0,0 +1,16 @@ +{ + "name": "prep", + "version": "1.0.0", + "description": "", + "main": "mean.js", + "scripts": { + "test": "jest" + }, + "keywords": [], + "author": "", + "license": "ISC", + "type": "commonjs", + "devDependencies": { + "jest": "^30.2.0" + } +} From f21c96e6e26ba3ce80b0bf42afe82a6ec0937e1b Mon Sep 17 00:00:00 2001 From: asaniDev Date: Sat, 15 Nov 2025 13:53:39 +0000 Subject: [PATCH 02/16] predicted the result of each file before fixing them --- Sprint-2/debug/address.js | 4 +++- Sprint-2/debug/author.js | 4 +++- Sprint-2/debug/recipe.js | 10 +++++++--- 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..41ce8e624 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,8 +1,10 @@ // Predict and explain first... +//the original code is using the wrong notation to access our object this will return undefined as we do not have a 0 property. // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working +//fixed it by puting "houseNumber" in the square brackets. const address = { houseNumber: 42, @@ -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..a78491d62 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,7 +1,9 @@ // Predict and explain first... +//the method for of is an array method and it would be looking for an indexed list of items rather than a key value pair so an error will be thrown since we are using an object. // 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 +//i can fix this by changing for ... of to for ... in. const author = { firstName: "Zadie", @@ -11,6 +13,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value in author) { console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..47cbc6ba1 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,8 +1,10 @@ // Predict and explain first... +//it will display the title and number of people it serves on one line then the whole recipe on the next line // 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? +//by using the string literal to ensure multiple lines, then having the title on the first line, the number served on the next line and ingredients on the third line. using the dot notation to access the properties needed. const recipe = { title: "bruschetta", @@ -10,6 +12,8 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +console.log(`${recipe.title} serves ${recipe.serves} + ingredients: ${recipe.ingredients[0]} + ${recipe.ingredients[1]} + ${recipe.ingredients[2]} + ${recipe.ingredients[3]}`); From b058970b50ab73fadd1a5b531babf5dc84ec83ff Mon Sep 17 00:00:00 2001 From: asaniDev Date: Sat, 15 Nov 2025 13:54:38 +0000 Subject: [PATCH 03/16] installed dependencies --- Sprint-2/package-lock.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Sprint-2/package-lock.json b/Sprint-2/package-lock.json index 9b4c725d6..ceda7296e 100644 --- a/Sprint-2/package-lock.json +++ b/Sprint-2/package-lock.json @@ -56,6 +56,7 @@ "integrity": "sha512-Oixnb+DzmRT30qu9d3tJSQkxuygWm32DFykT4bRoORPa9hZ/L4KhVB/XiRm6KG+roIEM7DBQlmg27kw2HZkdZg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@ampproject/remapping": "^2.2.0", "@babel/code-frame": "^7.25.7", @@ -1368,6 +1369,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "caniuse-lite": "^1.0.30001663", "electron-to-chromium": "^1.5.28", From b63c6de42d9bc9b7336434491be58650ab6d815f Mon Sep 17 00:00:00 2001 From: asaniDev Date: Sun, 23 Nov 2025 01:01:17 +0000 Subject: [PATCH 04/16] made up tests and then created functions to pass the required tests in implement folder --- Sprint-2/implement/contains.js | 33 +++++++++++++++++++++- Sprint-2/implement/contains.test.js | 11 +++++++- Sprint-2/implement/lookup.js | 17 +++++++++++- Sprint-2/implement/lookup.test.js | 21 +++++++++++++- Sprint-2/implement/querystring.js | 36 +++++++++++++++++++++--- Sprint-2/implement/querystring.test.js | 24 ++++++++++++++-- Sprint-2/implement/tally.js | 38 +++++++++++++++++++++++++- Sprint-2/implement/tally.test.js | 17 +++++++++++- 8 files changed, 185 insertions(+), 12 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..12b9231c3 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,34 @@ -function contains() {} +function contains(checkObj, checkProp) { + if ( + typeof checkObj === "object" && + checkObj !== null && + !Array.isArray(checkObj) + ) { + const checkKeys = Object.keys(checkObj); + let isProperty = false; + + if (checkKeys.length === 0) { + return false; + } + + if (Object.hasOwn(checkObj, checkProp)) { + return true; + } else { + for (let key of checkKeys) { + if (key === checkProp) { + isProperty = true; + } + } + + if (isProperty === false) { + return false; + } + } + } else { + throw new Error("error invalid parameter please provide an object"); + } +} + +contains({ d: "me" }, 4); module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..80a49f8cd 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,25 @@ 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("returns true when an object contains a property that matches the one passed to contains", () => + expect(contains({ area: "Manchester" }, "area")).toBe(true)); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("returns false when an object with properties is passed to contains with a non-existent property name", () => + expect(contains({ area: "Manchester" }, "town")).toBe(false)); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("should throw an error when when an invalid parameter like an array is passed to contains", () => + expect(() => contains([1, 4], 4)).toThrow( + "error invalid parameter please provide an object" + )); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..dacb9fc31 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,20 @@ -function createLookup() { +function createLookup(inputArray) { // implementation here + const returnObject = {}; + + if (Array.isArray(inputArray)) { + if (inputArray.length === 0) { + return "passed array was empty, no values to display"; + } + + for (let item of inputArray) { + returnObject[item[0]] = item[1]; + } + + return returnObject; + } else { + throw new Error("error incorrect parameter passed please provide an array"); + } } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..7c5fd6a40 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,25 @@ 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", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ["MW", "MWK"], + ["ZW", "ZWD"], + ]) + ).toEqual({ US: "USD", CA: "CAD", MW: "MWK", ZW: "ZWD" }); +}); + +test("if passed an empty array , should print a message telling user array is empty", () => { + expect(createLookup([])).toBe("passed array was empty, no values to display"); +}); + +test("if passed parameter which is not an array throw an error", () => { + expect(() => createLookup("US, USD")).toThrow( + "error incorrect parameter passed please provide an array" + ); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..8bd59028e 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,14 +3,42 @@ function parseQueryString(queryString) { if (queryString.length === 0) { return queryParams; } - const keyValuePairs = queryString.split("&"); - for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (queryString.includes("&")) { + const keyValuePairs = queryString.split("&"); + + for (const pair of keyValuePairs) { + let countMatch = 0; + + if (pair.includes("=")) { + const equalSignIndex = pair.indexOf("="); + + queryParams[pair.slice(0, equalSignIndex)] = pair.slice( + equalSignIndex + 1 + ); + } else { + throw new Error( + "error invalid format string, no = to separate key value pairs" + ); + } + } + } + + if (queryString.includes("=")) { + const equalSignIndex = queryString.indexOf("="); + + queryParams[queryString.slice(0, equalSignIndex)] = queryString.slice( + equalSignIndex + 1 + ); + } else { + throw new Error( + "error invalid format string, no = to separate key value pairs" + ); } return queryParams; } +console.log(`with ${parseQueryString("equation=x=y+1")}`); + module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..7bdf55cd3 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,30 @@ // 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("parseQueryString receives an empty string", () => { + expect(parseQueryString("")).toEqual({}); +}); test("parses querystring values containing =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ - "equation": "x=y+1", + equation: "x=y+1", }); }); + +test("if our function is passed only one key - value pair", () => { + expect(parseQueryString("color=brown")).toEqual({ color: "brown" }); +}); + +test("parses querystring without an =, it should throw an error", () => { + expect(() => parseQueryString("colorisequaltobrown")).toThrow( + "error invalid format string, no = to separate key value pairs" + ); +}); + +test("if our function is passed only one string but there is no =", () => { + expect(parseQueryString("color,brown")).toEqual( + "error invalid format string, no = to separate key value pair" + ); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..cbdd2aecc 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,39 @@ -function tally() {} +function tally(inputArray) { + if (Array.isArray(inputArray)) { + if (inputArray.length === 0) { + return {}; + } + + let itemCount = 0; + const tallyObject = {}; + let n = 0; + + while (inputArray.length > 0) { + const tempArray = []; + let i = 0; + let currentArrayItem = inputArray[0]; + + while (i < inputArray.length) { + if (currentArrayItem === inputArray[i]) { + itemCount++; + tempArray.push(inputArray.splice(i, 1)); + i--; + } + + i++; + } + + if (tempArray.length > 0) { + tallyObject[tempArray[0]] = tempArray.length; + } + } + console.log(`the new array and the new object ${tallyObject}`); + return tallyObject; + } else { + throw new Error("error invalid input passed, please provide an array"); + } +} + +console.log(`new object is ${tally(["a", "a", "b", "c", "c", "d", "a"])}`); module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..afc6315aa 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,27 @@ const tally = require("./tally.js"); // 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("given an array with duplicate items it should return counts for each unique item ", () => { + expect(tally(["a", "a", "b", "c", "c", "d", "a"])).toEqual({ + a: 3, + b: 1, + c: 2, + d: 1, + }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("should throw an error when tally is passed an invalid input like a string", () => { + expect(() => tally("b,b,c,d,e,e,f")).toThrow( + "error invalid input passed, please provide an array" + ); +}); From e17bf3024c0beb907faf24b371e3997fec46f2aa Mon Sep 17 00:00:00 2001 From: asaniDev Date: Sat, 29 Nov 2025 16:08:23 +0000 Subject: [PATCH 05/16] fixed invert function,answered questions and added tests to check it works as expected --- Sprint-2/interpret/invert.js | 37 +++++++++++++++++++++++++++++-- Sprint-2/interpret/invert.test.js | 31 ++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 Sprint-2/interpret/invert.test.js diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..0c3bab967 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -9,21 +9,54 @@ function invert(obj) { const invertedObj = {}; - for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + if (Object.prototype.toString.call(obj) === "[object Object]") { + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + } else { + throw new Error("error invalid input entered, expecting an object"); } return invertedObj; } +module.exports = invert; + +console.log( + `the return value when invert is called with ${invert({ + cars: { toyota: 2, bmw: 1, benz: 4 }, + })}` +); + +/* +for (const property in obj) { + if (typeof obj[property] !== "string") { + throw new Error( + "error invalid input entered, expecting an object to have only strings as values" + ); + } else { + + } + } + +*/ + // a) What is the current return value when invert is called with { a : 1 } +//it returns a string describing the object. // b) What is the current return value when invert is called with { a: 1, b: 2 } +//it aso returns a string describing the object. // 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 does Object.entries return? Why is it needed in this program? +//it returns an array made up of arrays of the original objects key - value pairs. +//it allows us to unpack the contents of the object into an array which can then be used to create the new object // d) Explain why the current return value is different from the target output +//because we used dot notation to assign a value to our key, this creates a property called key +//and assigns it a value. we want our key to get its name from a variable so we need to use bracket notation. // e) Fix the implementation of invert (and write tests to prove it's fixed!) +//we can fix it by using 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..7ce178b17 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,31 @@ +const invert = require("./invert.js"); + +describe("tests to see if invert function swaps around the keys and values in a given object", () => { + test("if invert is passed a value which is not an object, it should throw an error", () => { + expect(() => + invert([ + ["x", 10], + ["y", 20], + ]) + ).toThrow("error invalid input entered, expecting an object"); + }); + + test("if we are passed an empty object we should return an empty object", () => { + expect(invert({})).toEqual({}); + }); + + test("given an object with key value pairs, it should swap the keys and values in the object", () => { + expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" }); + expect( + invert({ city: "Birmingham", population: "345908", boroughs: "20" }) + ).toEqual({ Birmingham: "city", 345908: "population", 20: "boroughs" }); + }); +}); + +/* +test("if invert is passed an object which has an array or object as one of its values, it should throw an error", () => { + expect(() => invert({ cars: { toyota: 2, bmw: 1, benz: 4 } })).toThrow( + "error invalid input entered, expecting an object to have only strings as values" + ); + }); +*/ From a1c9c1ec579b03d93805c55e7cb647e7a67c2dcf Mon Sep 17 00:00:00 2001 From: asaniDev Date: Tue, 16 Dec 2025 13:42:41 +0000 Subject: [PATCH 06/16] removed test console logs, cleaned up the code and fixed faulty test in querystring test suite --- Sprint-2/debug/address.js | 2 +- Sprint-2/implement/contains.js | 2 -- Sprint-2/implement/querystring.js | 2 -- Sprint-2/implement/querystring.test.js | 2 +- Sprint-2/implement/tally.js | 3 --- Sprint-2/interpret/invert.js | 29 +++++++++----------------- Sprint-2/interpret/invert.test.js | 11 ++++------ 7 files changed, 16 insertions(+), 35 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 41ce8e624..caf72bc2b 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -4,7 +4,7 @@ // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working -//fixed it by puting "houseNumber" in the square brackets. +//fixed it by putting "houseNumber" in the square brackets. const address = { houseNumber: 42, diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 12b9231c3..9b3271a05 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -29,6 +29,4 @@ function contains(checkObj, checkProp) { } } -contains({ d: "me" }, 4); - module.exports = contains; diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 8bd59028e..eed78c49e 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -39,6 +39,4 @@ function parseQueryString(queryString) { return queryParams; } -console.log(`with ${parseQueryString("equation=x=y+1")}`); - module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 7bdf55cd3..fe93c5d07 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -26,7 +26,7 @@ test("parses querystring without an =, it should throw an error", () => { }); test("if our function is passed only one string but there is no =", () => { - expect(parseQueryString("color,brown")).toEqual( + expect(() => parseQueryString("color,brown")).toThrow( "error invalid format string, no = to separate key value pair" ); }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index cbdd2aecc..2d9b21257 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -27,13 +27,10 @@ function tally(inputArray) { tallyObject[tempArray[0]] = tempArray.length; } } - console.log(`the new array and the new object ${tallyObject}`); return tallyObject; } else { throw new Error("error invalid input passed, please provide an array"); } } -console.log(`new object is ${tally(["a", "a", "b", "c", "c", "d", "a"])}`); - module.exports = tally; diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 0c3bab967..72c2f776d 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -11,6 +11,16 @@ function invert(obj) { if (Object.prototype.toString.call(obj) === "[object Object]") { for (const [key, value] of Object.entries(obj)) { + if ( + value === null || + Array.isArray(value) || + (typeof value === "object" && !Array.isArray(value)) + ) { + throw new Error( + "error invalid input entered, expecting an object to have only strings as values" + ); + } + invertedObj[value] = key; } } else { @@ -22,25 +32,6 @@ function invert(obj) { module.exports = invert; -console.log( - `the return value when invert is called with ${invert({ - cars: { toyota: 2, bmw: 1, benz: 4 }, - })}` -); - -/* -for (const property in obj) { - if (typeof obj[property] !== "string") { - throw new Error( - "error invalid input entered, expecting an object to have only strings as values" - ); - } else { - - } - } - -*/ - // a) What is the current return value when invert is called with { a : 1 } //it returns a string describing the object. diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js index 7ce178b17..cb5c1e144 100644 --- a/Sprint-2/interpret/invert.test.js +++ b/Sprint-2/interpret/invert.test.js @@ -21,11 +21,8 @@ describe("tests to see if invert function swaps around the keys and values in a ).toEqual({ Birmingham: "city", 345908: "population", 20: "boroughs" }); }); }); - -/* test("if invert is passed an object which has an array or object as one of its values, it should throw an error", () => { - expect(() => invert({ cars: { toyota: 2, bmw: 1, benz: 4 } })).toThrow( - "error invalid input entered, expecting an object to have only strings as values" - ); - }); -*/ + expect(() => invert({ cars: { toyota: 2, bmw: 1, benz: 4 } })).toThrow( + "error invalid input entered, expecting an object to have only strings as values" + ); +}); From 87ccd7f0874a9e1f3b87d9b3fd7faa96b421b3c6 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 00:17:23 +0000 Subject: [PATCH 07/16] changed the console to log the values held by the keys in the object --- Sprint-2/debug/author.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index a78491d62..d8be5b0b4 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -14,5 +14,5 @@ const author = { }; for (const value in author) { - console.log(value); + console.log(author[value]); } From 43ae752b4fc35cd31e3901203a9dee9c46b6d51c Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 01:25:43 +0000 Subject: [PATCH 08/16] changed it so it can work for any number of ingredients --- Sprint-2/debug/recipe.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 47cbc6ba1..441885ff0 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -12,8 +12,15 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; +//changed console to display for any number of ingredients console.log(`${recipe.title} serves ${recipe.serves} ingredients: ${recipe.ingredients[0]} ${recipe.ingredients[1]} ${recipe.ingredients[2]} ${recipe.ingredients[3]}`); + +console.log(`${recipe.title} serves ${recipe.serves} + ingredients:`); +for (let ingredient of recipe.ingredients) { + console.log(` ${ingredient}`); +} From dd2c422881672ba0a8f70208e783238bd181c863 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 02:09:39 +0000 Subject: [PATCH 09/16] removed redundant code from contantains to remain with one if else statement --- Sprint-2/implement/contains.js | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index 9b3271a05..2cea09edd 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -4,25 +4,10 @@ function contains(checkObj, checkProp) { checkObj !== null && !Array.isArray(checkObj) ) { - const checkKeys = Object.keys(checkObj); - let isProperty = false; - - if (checkKeys.length === 0) { - return false; - } - if (Object.hasOwn(checkObj, checkProp)) { return true; } else { - for (let key of checkKeys) { - if (key === checkProp) { - isProperty = true; - } - } - - if (isProperty === false) { - return false; - } + return false; } } else { throw new Error("error invalid parameter please provide an object"); From f90d792deedbb1419c51ad63eb02d3b112ec94ef Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 02:11:56 +0000 Subject: [PATCH 10/16] made sure exception was testing for arrays as well --- Sprint-2/implement/contains.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 80a49f8cd..0449b8c1e 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -39,6 +39,6 @@ test("returns false when an object with properties is passed to contains with a // When passed to contains // Then it should return false or throw an error test("should throw an error when when an invalid parameter like an array is passed to contains", () => - expect(() => contains([1, 4], 4)).toThrow( + expect(() => contains([1, 4, 5])).toThrow( "error invalid parameter please provide an object" )); From c63b70febfe50710d6ec3b12c1667ccc1e3259fc Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 02:20:54 +0000 Subject: [PATCH 11/16] refactored lookup to remove else block, returned empty object for empty array passed --- Sprint-2/implement/lookup.js | 20 ++++++++++---------- Sprint-2/implement/lookup.test.js | 4 ++-- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index dacb9fc31..63fa99cc9 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -2,19 +2,19 @@ function createLookup(inputArray) { // implementation here const returnObject = {}; - if (Array.isArray(inputArray)) { - if (inputArray.length === 0) { - return "passed array was empty, no values to display"; - } - - for (let item of inputArray) { - returnObject[item[0]] = item[1]; - } + if (!Array.isArray(inputArray)) { + throw new Error("error incorrect parameter passed please provide an array"); + } + if (inputArray.length === 0) { return returnObject; - } else { - throw new Error("error incorrect parameter passed please provide an array"); } + + for (let item of inputArray) { + returnObject[item[0]] = item[1]; + } + + return returnObject; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 7c5fd6a40..94e15bf90 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -11,8 +11,8 @@ test("creates a country currency code lookup for multiple codes", () => { ).toEqual({ US: "USD", CA: "CAD", MW: "MWK", ZW: "ZWD" }); }); -test("if passed an empty array , should print a message telling user array is empty", () => { - expect(createLookup([])).toBe("passed array was empty, no values to display"); +test("if passed an empty array , should return an empty object", () => { + expect(createLookup([])).toEqual({}); }); test("if passed parameter which is not an array throw an error", () => { From b7dddb5c6533c22fd1b81235d87f19c5a628cfb0 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 03:15:08 +0000 Subject: [PATCH 12/16] fixed bug in querystring.js which ran both blocks of code --- Sprint-2/implement/querystring.js | 22 ++++++++++------------ Sprint-2/implement/querystring.test.js | 7 +++++++ 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index eed78c49e..c5d2eb113 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -6,25 +6,21 @@ function parseQueryString(queryString) { if (queryString.includes("&")) { const keyValuePairs = queryString.split("&"); + console.log(keyValuePairs + "our 2 pairs"); for (const pair of keyValuePairs) { - let countMatch = 0; - - if (pair.includes("=")) { - const equalSignIndex = pair.indexOf("="); - - queryParams[pair.slice(0, equalSignIndex)] = pair.slice( - equalSignIndex + 1 - ); - } else { + if (!pair.includes("=")) { throw new Error( "error invalid format string, no = to separate key value pairs" ); + } else { + const keyValuePair = pair.split("="); + console.log(keyValuePair + "the single pair"); + + queryParams[keyValuePair[0]] = keyValuePair[1]; } } - } - - if (queryString.includes("=")) { + } else if (queryString.includes("=")) { const equalSignIndex = queryString.indexOf("="); queryParams[queryString.slice(0, equalSignIndex)] = queryString.slice( @@ -40,3 +36,5 @@ function parseQueryString(queryString) { } module.exports = parseQueryString; + +console.log(parseQueryString("color=brown&width=100")); diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index fe93c5d07..3106254cc 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -9,6 +9,13 @@ test("parseQueryString receives an empty string", () => { expect(parseQueryString("")).toEqual({}); }); +test("if our function is passed 2 key - value pairs", () => { + expect(parseQueryString("color=brown&width=100")).toEqual({ + color: "brown", + width: "100", + }); +}); + test("parses querystring values containing =", () => { expect(parseQueryString("equation=x=y+1")).toEqual({ equation: "x=y+1", From 180ac401ce8c68d6a13913723b5b93885e7e4e5d Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 03:59:27 +0000 Subject: [PATCH 13/16] changed conditions in invert function to allow specific primitive data types only --- Sprint-2/interpret/invert.js | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 72c2f776d..755c4f5f7 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -9,13 +9,9 @@ function invert(obj) { const invertedObj = {}; - if (Object.prototype.toString.call(obj) === "[object Object]") { + if (obj !== null && !Array.isArray(obj) && typeof obj === "object") { for (const [key, value] of Object.entries(obj)) { - if ( - value === null || - Array.isArray(value) || - (typeof value === "object" && !Array.isArray(value)) - ) { + if (typeof value != "string" && typeof value !== "number") { throw new Error( "error invalid input entered, expecting an object to have only strings as values" ); @@ -36,7 +32,7 @@ module.exports = invert; //it returns a string describing the object. // b) What is the current return value when invert is called with { a: 1, b: 2 } -//it aso returns a string describing the object. +//it aso returns the type of the object. // 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"}. From 1d92cf8e526dffc5cfb7d01acad86c553f584653 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 14:39:33 +0000 Subject: [PATCH 14/16] changed prop of test in last test to give a valid key of an array --- Sprint-2/implement/contains.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 0449b8c1e..0207c5348 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -39,6 +39,6 @@ test("returns false when an object with properties is passed to contains with a // When passed to contains // Then it should return false or throw an error test("should throw an error when when an invalid parameter like an array is passed to contains", () => - expect(() => contains([1, 4, 5])).toThrow( + expect(() => contains([1, 4, 5], 0)).toThrow( "error invalid parameter please provide an object" )); From b9111f10ffe70b5733fbfc887690da6940596273 Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 20:40:05 +0000 Subject: [PATCH 15/16] fixed indentation in querystring file and refactored tally.js to use itemcount --- Sprint-2/implement/querystring.js | 4 +--- Sprint-2/implement/tally.js | 9 +++++---- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index c5d2eb113..bbcd7b862 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,12 +1,12 @@ function parseQueryString(queryString) { const queryParams = {}; + if (queryString.length === 0) { return queryParams; } if (queryString.includes("&")) { const keyValuePairs = queryString.split("&"); - console.log(keyValuePairs + "our 2 pairs"); for (const pair of keyValuePairs) { if (!pair.includes("=")) { @@ -36,5 +36,3 @@ function parseQueryString(queryString) { } module.exports = parseQueryString; - -console.log(parseQueryString("color=brown&width=100")); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index 2d9b21257..105a0d15a 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -9,6 +9,7 @@ function tally(inputArray) { let n = 0; while (inputArray.length > 0) { + itemCount = 0; const tempArray = []; let i = 0; let currentArrayItem = inputArray[0]; @@ -20,11 +21,11 @@ function tally(inputArray) { i--; } - i++; - } + if (itemCount > 0) { + tallyObject[currentArrayItem] = itemCount; + } - if (tempArray.length > 0) { - tallyObject[tempArray[0]] = tempArray.length; + i++; } } return tallyObject; From dcb2cbb6330f76e13dd4b1be934c59df5570672d Mon Sep 17 00:00:00 2001 From: asaniDev Date: Wed, 17 Dec 2025 20:48:49 +0000 Subject: [PATCH 16/16] corrected an error in interpretation of the return of my function --- Sprint-2/interpret/invert.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index 755c4f5f7..c8e2d7e35 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -32,7 +32,7 @@ module.exports = invert; //it returns a string describing the object. // b) What is the current return value when invert is called with { a: 1, b: 2 } -//it aso returns the type of the object. +//it returns an object with one key value pair, the key is key and value is 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"}.