diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..c1d871fcc 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,5 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); + +houseNumber: (42, console.log(`My house number is ${address.houseNumber}`)); diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..f3a06f5b3 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -2,6 +2,7 @@ // 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 +// the "author" is an object so it needs to be in square brackets to be called const author = { firstName: "Zadie", @@ -11,6 +12,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of [author]) { console.log(value); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..55770316e 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -3,6 +3,7 @@ // 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? +// The recipe is an object so it needs to be in square brackets to be call each element by its key and index. const recipe = { title: "bruschetta", @@ -10,6 +11,11 @@ const recipe = { ingredients: ["olive oil", "tomatoes", "salt", "pepper"], }; -console.log(`${recipe.title} serves ${recipe.serves} +console.log(` ${recipe.title} + serves ${recipe.serves} ingredients: -${recipe}`); + ${recipe.ingredients[0]} + ${recipe.ingredients[1]} + ${recipe.ingredients[2]} + ${recipe.ingredients[3]} + `); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..bf2311dcf 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,13 @@ -function contains() {} +function contains(inPut, content) { + try { + console.log(inPut.hasOwnProperty(content)); + return inPut.hasOwnProperty(content); + } catch (error) { + console.error(error); + } +} +console.log(contains({ a: 1, b: 2 }, "a")); +console.log(contains({}, 9)); +// console.log(contains([a, 1, b, 2], "a")); module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..9b0fcf2b3 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,49 @@ 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({}, "c")).toEqual(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("contains correct object returns true", () => { + expect(contains({ a: 4, b: 3, c: 9 }, "c")).toEqual(true); + expect(contains({ a: 4, b: 3, c: 9 }, "a")).toEqual(true); + expect( + contains( + { ant: "in your pants", bee: "in your bonnet", cats: "pajamas" }, + "bee" + ) + ).toEqual(true); + expect(contains({ aa: "cars", ba: "baracus", cdeez: "nope" }, "ba")).toEqual( + true + ); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("contains incorrect object returns false", () => { + expect(contains({ a: 4, b: 3, c: 9 }, "d")).toEqual(false); + expect(contains({ a: 4, b: 3, c: 9 }, "8")).toEqual(false); + expect( + contains( + { ant: "in your pants", bee: "in your bonnet", cats: "pajamas" }, + "wasp" + ) + ).toEqual(false); + expect( + contains({ aa: "cars", ba: "baracus", cdeez: "nope" }, "Hanable Smith") + ).toEqual(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contains incorrect object type returns false", () => { + expect(contains([], "d")).toBe(false); + expect(contains("h", "d")).toBe(false); + expect(contains(9, "d")).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..80e3da79c 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,13 @@ -function createLookup() { - // implementation here +function createLookup(countryAndCurrency) { + const money = new Map(countryAndCurrency); + const cash = Object.fromEntries(money); + console.log(cash); + console.log(money); + return cash; } +createLookup([ + ["US", "USD"], + ["CA", "CAD"], +]); module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..ec860860e 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,12 @@ const createLookup = require("./lookup.js"); - -test.todo("creates a country currency code lookup for multiple codes"); +test("contains correct object returns true", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ US: "USD", CA: "CAD" }); +}); /* diff --git a/Sprint-2/implement/package.json b/Sprint-2/implement/package.json new file mode 100644 index 000000000..30e709f56 --- /dev/null +++ b/Sprint-2/implement/package.json @@ -0,0 +1,10 @@ +{ + "name": "implement", + "description": "coureswork", + "devDependencies": { + "jest": "^30.2.0" + }, + "scripts": { + "test": "jest" + } +} diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..8e31efb70 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -12,5 +12,6 @@ function parseQueryString(queryString) { return queryParams; } - +console.log(parseQueryString("y=8&r=y")); +console.log(parseQueryString("equation=x=y+1")); module.exports = parseQueryString; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..3674a50a2 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,20 @@ -function tally() {} +function tally(arr) { + let count = {}; + for (let letters of arr) { + if (count[letters]) { + count[letters]++; + } else { + count[letters] = 1; + } + } + console.log(arr); + return count; +} + +console.log(tally(["a", "a", "b", "c"])); module.exports = tally; +// let chars = arr.reduce( +// (accsess, currentVal, i) => ({ ...arr, [i]: currentVal }), +// {} +// ); diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..0e07579f3 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,7 +23,9 @@ 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(["a", "b", "a"]).toEqual({ a: 2, b: 1 })); +}); // Given an array with duplicate items // When passed to tally diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..3d5d892ec 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -6,24 +6,43 @@ // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} -function invert(obj) { - const invertedObj = {}; +// function invert(obj) { +// const invertedObj = {}; - for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; - } +// for (const [key, value] of Object.entries(obj)) { +// invertedObj.key = value; +// } - return invertedObj; -} +// return invertedObj; +// } +// console.log(invert({ a: 1, b: 2 })); // a) What is the current return value when invert is called with { a : 1 } +// it only returns { key: 1 }. // b) What is the current return value when invert is called with { a: 1, b: 2 } +// it only returns 2 because the loop continues and rewrights the key // c) What is the target return value when invert is called with {a : 1, b: 2} +// it should return { "1": "a", "2": "b" } + +// d) What does Object.entries return? Why is it needed in this program +// Object.entries returns the key, value pairs for the object it is given. + +// e) Explain why the current return value is different from the target output +// The invertedObj.key = value; is using the literal string "key" as its key instead of the variable key -// c) What does Object.entries return? Why is it needed in this program? +// f) Fix the implementation of invert (and write tests to prove it's fixed!) -// d) Explain why the current return value is different from the target output +function invert(obj) { + const invertedObj = {}; + + for (const [key, value] of Object.entries(obj)) { + invertedObj[value] = key; + } + + return invertedObj; +} +console.log(invert({ a: 1, b: 2 })); -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +module.exports = invert; diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 000000000..e0e0d6ab8 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,5 @@ +const invert = require("../interpret/invert.js"); + +test("test for a number followed by a letter", () => { + expect(invert({ 1: "a", 2: "b", 3: "c" })).toEqual({ a: 1, b: 2, c: 3 }); +}); 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",