diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..1193eacca 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,5 @@ // Predict and explain first... - +//will log undefined , there is no "0" key, objects do not have indexes so we can't use [0] // 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,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`);//or ["houseNumber"] diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..592a83087 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +// it comes out of the loop after the first console.log // 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,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(`${value}:${author[value]}`); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..42dd9f549 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,5 +1,5 @@ // Predict and explain first... - +//it will log everything in one line, we need to add \n // 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? @@ -10,6 +10,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:`); + +for (const ingredient of recipe.ingredients) { + console.log(ingredient); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..ce68fb81d 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,16 @@ -function contains() {} +function contains(obj, property) { + if (typeof obj !== "object" || obj === null || Array.isArray(obj)) { + throw new Error("Parameter is not an object literal"); + } + if (Object.keys(obj).length === 0) { + return false; + } + + if (property in obj) { + return true; + } + + return false; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 326bdb1f2..6565d518e 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,16 +16,32 @@ as the object doesn't contains a key of 'c' // Given a contains function // When passed an object and a property name // Then it should return true if the object contains the property, false otherwise +test("if the object contains the property, return 'true'", () => { + expect(contains({ a: 1 }, "a")).toEqual(true); +}); +test("if the object contains the property, return 'true'", () => { + expect(contains({ a: 1, "a,s,3": "op" }, ["a", "s", 3])).toEqual(true); +}); + +test("if the object doesn't contain the property return 'false'", () => { + expect(contains({ a: 1, d: 7 }, "m")).toEqual(false); +}); +test("if the object doesn't contain the property return 'false'", () => { + expect(contains({ a: 1, s: 7 }, [1, 2, "a"])).toEqual(false); +}); // 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", () => { + expect(contains({}, "a")).toEqual(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +//<<> // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false @@ -33,3 +49,6 @@ test.todo("contains on empty object returns false"); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("when instead of an object literal the parameter is an array throw error", () => { + expect(() => contains([], "a")).toThrow("Parameter is not an object literal"); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..fd3a85055 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,18 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrency) { + let countryCurrencyPairs = {}; + for (const pair of countryCurrency) { + let country = pair[0]; + let currency = pair[1]; + countryCurrencyPairs[country] = currency; + } + return countryCurrencyPairs; } +console.log( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ["RO", "RON"], + ]) +); module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..d401513f3 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,6 +1,14 @@ 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"], + ["RO", "RON"], + ]) + ).toEqual({ US: "USD", CA: "CAD", RO: "RON" }); +}); /* diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..0a93c3551 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -3,14 +3,30 @@ 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(pair===""){ + continue + } + const equalIndex=pair.indexOf("=") + if (equalIndex === -1) { + queryParams[pair]=""; + continue + } + const key=pair.substring(0,equalIndex) + const value=pair.substring(equalIndex+1) + queryParams[key]=value + } return queryParams; } +//console.log(parseQueryString("equationxy+1")); module.exports = parseQueryString; + + + + diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..20eac2231 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,10 +3,52 @@ // 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", + expect(parseQueryString("equation=x=y+1")).toEqual({ equation: "x=y+1" }); +}); + +test("parse querystring with multiple values", () => { + expect(parseQueryString("equation=x=y+1&sound=none")).toEqual({ + equation: "x=y+1", + sound: "none", }); }); + +test("parse querystring with multiple ==", () => { + expect(parseQueryString("equation==x=y+1&sound=none")).toEqual({ + equation: "=x=y+1", + sound: "none", + }); +}); + +test("parse querystring with multiple &&", () => { + expect(parseQueryString("equation=x=y+1&&sound=none")).toEqual({ + equation: "x=y+1", + sound: "none", + }); +}); + +test("parse querystring with no =", () => { + expect(parseQueryString("equation=x=y+1&soundnone")).toEqual({ + equation: "x=y+1", + soundnone:"", + }); +}); + +//don't know how to do this +// test("parse querystring with multiple ==", () => { +// expect(parseQueryString("equation==x=y+1&sound=none")).toEqual({ +// equation=: "x=y+1", +// sound: "none", +// }); +// }); + +//don't know how to do this +// test("parses querystring values containing &", () => { +// expect(parseQueryString("&equation=x=y+1&sound=none")).toEqual({ +// "&equation": "x=y+1", +// sound: "none", +// }); +// }); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..6ed7f6ec5 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,17 @@ -function tally() {} +function tally(arr) { +const objectCount={} +if(!Array.isArray(arr)){ + throw new Error("Invalid input"); +} +if(arr.length===0){ + return {} +} +for(const element of arr){ + objectCount[element]=objectCount[element] ? objectCount[element] +1:1; +} +return objectCount + +} + module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..a68e13ec4 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,27 @@ const tally = require("./tally.js"); // 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("on an array of items returns an object containing the count for each unique item ", () => { + expect(tally(["a", "a", "a", "h"])).toEqual({ a: 3, h: 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("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("on an array of items returns an object containing the count for each unique item ", () => { + expect(tally([1,1,4,4,4,4])).toEqual({ 1: 2, 4: 4 }); +}); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test("given invalid input , throw error",()=>{ + expect(() => tally("hello")).toThrow("Invalid input"); +}) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..59705c738 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,26 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } // a) What is the current return value when invert is called with { a : 1 } +//["a",1] // 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? +//returns an array so we can access each element of it so we can swap their order // d) Explain why the current return value is different from the target output +//it creates a new key:value pair with the key being "key",also it doesnt invert the key with the value // 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..c0e0db0b6 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,4 @@ +const invert = require("./invert.js"); +test("swap the keys and values in the object", () => { + expect(invert({ a: 1, asd: "d3e" })).toEqual({ 1: "a", d3e: "asd" }); +});