diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..498e2adf7 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,8 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +//console.log(`My house number is ${address[0]}`); +// Objects in javascript are key,value types that is why the code is not working, +// We are trying to access house number using the key "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..ddb98a456 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,8 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); -} + + for (const key in author) { + console.log(key); + } + diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..70ab395b2 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -7,9 +7,19 @@ const recipe = { title: "bruschetta", serves: 2, - ingredients: ["olive oil", "tomatoes", "salt", "pepper"], + ingredients: ["olive oil", "tomatoes", "salt", "pepper"] }; console.log(`${recipe.title} serves ${recipe.serves} - ingredients: -${recipe}`); +// ingredients: +// ${recipe.ingredients.join("\n")}`); + // + + //OR +//recipe.ingredients.forEach(ingredient => console.log(ingredient)); + + + //.join ==> converts array in to a string. + + //("\n") ==> this is called new line character which is used to make sure the ingredients are +// listed in a new line instead of separated by commas. \ No newline at end of file diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..9c8518d1b 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,5 @@ -function contains() {} +function contains(obj,key) { + return (key in obj); +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..06a84a684 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,35 @@ 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 obj = {}; +expect(contains(obj,"m")).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 an existing property should return true", () => { +const obj = {name:"Milli",surname:"Mesfin",city:"Birmingham"}; +expect(contains(obj,'surname')).toBe(true); +expect(contains(obj,'city')).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 name should return false", () => { + +const obj = {name:"Milli",surname:"Mesfin", city:"Birmingham"} +expect(contains(obj,'age')).toBe(false); +expect(contains(obj,'address')).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contains an invalid parameter like array should return false" ,() => { +const obj = ["code","your","future"]; +expect(contains(obj,'age')).toBe(false); +expect(contains(obj,'address')).toBe(false); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..4c964a832 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,8 @@ -function createLookup() { - // implementation here -} +function createLookup(countryCurrencyPairs) { + + const obj = Object.fromEntries(countryCurrencyPairs); + + return obj ; +}; module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..2ea12f928 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,15 @@ 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 countryCurrencyPairs = [['US','USD'], ['CA', 'CAD']] + expect(createLookup(countryCurrencyPairs)).toEqual({ + + 'US':'USD', + 'CA':'CAD' + + }); + +}); /* diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..368b924db 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,16 @@ -function tally() {} +function tally(arr) { + if (!Array.isArray(arr)) { + //this line checks if arr is not an array, if it's not an array it will throw error. + throw new Error("not an array"); + } + + const count = {}; + for(const item of arr){ + count[item]=(count[item]||0)+1; // this line is where we update the count of each item in the count object. + //count[item]=> is checking if already exists as a key in the count object. + // ||0 => this changes the undefined count to zero if the item have not seen in the count object and then we add 1 . + } + return count; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..39c2ee2c5 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,29 @@ 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", () => { + const count = []; + expect(tally([])).toEqual({}) +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("An array with duplicate items should return counts for each unique item", () => { + const count = ["h", "e", "l", "l", "o",]; + const expectedOutput = {h: 1, e: 1, l: 2, o: 1 }; + + expect(tally(count)).toEqual(expectedOutput); + }); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("An array with invalid input should throw an error", () => { + const count = ["hello", "hey", "hey", "hello", "hello", "hi"]; + const expectedOutput = {hello: 3, hey: 2, hi: 1 }; + + expect(tally(count)).toEqual(expectedOutput); + }); + +