diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..53b92bd536 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,5 +1,7 @@ // Predict and explain first... +// my prediction is that this is not indexed array. it is an object that uses key. + // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working @@ -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 8c2125977d..eb6e122d7a 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,8 +1,14 @@ // Predict and explain first... +// my prediction here is that we can not use for..of loop because author is an object. +// The reason is, it doesnt use indices it uses keys.instead we will use for..in loop. + // 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 +// As I said earlier it is not using index, we can not iterate it with +// for ..of loop. The output also going to be the key so we have to change that as well. + const author = { firstName: "Zadie", lastName: "Smith", @@ -11,6 +17,6 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const key in author) { + console.log(author[key]); } diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cdc..2f04d07036 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,15 +1,21 @@ // Predict and explain first... +// My prediction is that ingredients are not gonna be printed out. because the object +// is not attached to the key in the console.log. // 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? +// We can fix it by using for..of loop for the array . + 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}`); + +for (const item of recipe.ingredients) { + console.log(item); +} diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..3a24f3903c 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,14 @@ -function contains() {} +function contains(objIn, property) { + if (Array.isArray(objIn) || objIn === null || typeof objIn !== "object") { + throw new Error("contains requires an object"); + } + + for (const key in objIn) { + if (key === property) { + return true; + } + } + return false; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..46b0da97ac 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -19,22 +19,66 @@ as an array isn't an object // 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("should return true when an object contains the property", () => { + const object1 = { a: 1, b: 2, c: 3 }; + const property = "a"; + const result = contains(object1, property); + expect(result).toEqual(true); +}); // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +test("should return false when empty object is passed", () => { + const object1 = {}; + const property = "a"; + const result = contains(object1, property); + expect(result).toEqual(false); +}); // Given an object with properties // When passed to contains with an existing property name // Then it should return true +test("should return true when object is passed and property name matches", () => { + const object1 = { a: 1, b: 2, c: 3, d: 4 }; + const property = "b"; + const result = contains(object1, property); + expect(result).toEqual(true); +}); +test("should return true when object is passed and property name matches", () => { + const object1 = { name: "sharon", age: 30, hobby: "photography" }; + const property = "hobby"; + const result = contains(object1, property); + expect(result).toEqual(true); +}); // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false +test("should return false when object is passed and property name doesnt matches ", () => { + const object1 = { a: 1, b: 2, c: 3, d: 4 }; + const property = "e"; + const result = contains(object1, property); + expect(result).toEqual(false); +}); // Given a value that isn't an object - an array, a string, a number, // null, or no argument at all // When passed to contains // Then it should throw Error("contains requires an object") // (careful: typeof [] and typeof null are both "object") +test("should throw an error when the value passed is Array", () => { + expect(() => contains([1, 2, 3])).toThrow("contains requires an object"); +}); +test("should throw an error when the value passed is null", () => { + expect(() => contains(null)).toThrow("contains requires an object"); +}); +test("should throw an error when the valu passed is a string", () => { + expect(() => contains("null")).toThrow("contains requires an object"); +}); +test("should throw an error when the value passed is number", () => { + expect(() => contains(7)).toThrow("contains requires an object"); +}); +test("should throw an error when there is no argument", () => { + expect(() => contains()).toThrow("contains requires an object"); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..e82678829e 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,10 @@ -function createLookup() { - // implementation here +function createLookup(array) { + const countryCurrency = {}; + + for (let i = 0; i < array.length; i++) { + countryCurrency[array[i][0]] = array[i][1]; + } + return countryCurrency; } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..ae25412c64 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,5 @@ 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 @@ -33,3 +31,33 @@ It should return: 'CA': 'CAD' } */ +test("should return an empty object key from an empty array ", () => { + expect(createLookup([])).toEqual({}); +}); +test("should return an object key value pairs from an array of one country currency code ", () => { + expect(createLookup([["US", "USD"]])).toEqual({ US: "USD" }); +}); +test("should return an object key value pairs from an array of multiple code pairs", () => { + expect( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) + ).toEqual({ + US: "USD", + CA: "CAD", + }); +}); +test("should return an object key value pairs from an array of three code pairs", () => { + expect( + createLookup([ + ["ERI", "NAK"], + ["CA", "CAD"], + ["EU", "EUR"], + ]) + ).toEqual({ + ERI: "NAK", + CA: "CAD", + EU: "EUR", + }); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..4aa150e837 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,15 +1,29 @@ function parseQueryString(queryString) { const queryParams = {}; + if (queryString.length === 0) { return queryParams; } + const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + if (!pair) { + continue; + } + let [key, ...rest] = pair.split("="); + let value = rest.join("="); + + const lastThreeCharKey = key.slice(-2); + if (!lastThreeCharKey.includes("+") || key.includes("%")) { + key = decodeURIComponent(key.replaceAll("+", " ")); + } + const lastThreeCharVal = value.slice(-2); + if (!lastThreeCharVal.includes("+")) { + value = decodeURIComponent(value.replaceAll("+", " ")); + } queryParams[key] = value; } - return queryParams; } diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df617..e8aecb2015 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, 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("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ @@ -37,12 +37,25 @@ test("should replace '+' by ' '", () => { }); }); -// Stretch exercise: Handling query strings that contain identical keys +test("should replace '+' by ' '", () => { + expect(parseQueryString("first+name=Mary+Jane")).toEqual({ + "first name": "Mary Jane", + }); +}); -// Delete this test if you are not working on this optional case -test("should store values of a key in an array when the key has 2 or more values", () => { - expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({ - key: ["value1", "value2", "value3"], - foo: "bar", +test("should replace multiple plus'+' by ' '", () => { + expect(parseQueryString("name=John+Doe+Foo&age=30&city=New+York")).toEqual({ + name: "John Doe Foo", + age: "30", + city: "New York", }); }); +// Stretch exercise: Handling query strings that contain identical keys + +// Delete this test if you are not working on this optional case +//test("should store values of a key in an array when the key has 2 or more values", () => { +// expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({ +// key: ["value1", "value2", "value3"], +// foo: "bar", +//}); +//}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..ad07d973e3 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,15 @@ -function tally() {} - +function tally(arrayItems) { + const countItem = {}; + if (!Array.isArray(arrayItems)) { + throw new Error("tally rquires an input"); + } + for (const item of arrayItems) { + if (countItem[item]) { + countItem[item] += 1; + } else { + countItem[item] = 1; + } + } + return countItem; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c2965..be4762ab59 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,16 +19,43 @@ 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("should return an object containing the count for each unique item", () => { + const array = ["a"]; + const countArray = tally(array); + expect(countArray).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(" should return an empty object when empty array is given", () => { + expect(tally([])).toEqual({}); +}); // Given an array with duplicate items // When passed to tally // Then it should return counts for each unique item +test("should return an object containing the count for each unique item", () => { + const array = ["a", "a", "a"]; + const countArray = tally(array); + expect(countArray).toEqual({ a: 3 }); +}); +test("should return an object containing the count for each unique item", () => { + const array = ["a", "a", "b", "b", "b", "c"]; + const countArray = tally(array); + expect(countArray).toEqual({ a: 2, b: 3, c: 1 }); +}); // Given an invalid input like a string, a number, or no argument at all // When passed to tally // Then it should throw Error("tally requires an array") +test("should throw an error when given invalid input:string", () => { + expect(() => tally("a")).toThrow("tally rquires an input"); +}); +test("should throw an error when given invalid input:number", () => { + expect(() => tally(5)).toThrow("tally rquires an input"); +}); +test("should throw an error when given invalid input: no argument", () => { + expect(() => tally()).toThrow("tally rquires an input"); +}); diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..7740e596d9 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,36 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } - +module.exports = invert; // a) What is the current return value when invert is called with { a : 1 } +//{key: 1;} + // b) What is the current return value when invert is called with { a: 1, b: 2 } +//{key: 2;} + // c) What is the target return value when invert is called with {a : 1, b: 2} -// c) What does Object.entries return? Why is it needed in this program? +//{'1' :'a','2':'b' } + +// d) What does Object.entries return? Why is it needed in this program? + +//Objet.entries takes objects and returne array with in array or sub arrays. +//in this program it is needed to put the properties in an array as key and value. +// After that line though, the next line takes the key and value and place it in an object again by changing key and value +// position when stored in the object + +// e) Explain why the current return value is different from the target output +//The crrent output is different becouse it is only storing the last element in the loop +// with the key name "key" . So when it is printed out it comes forexample {'key': 1} +//and also the value and key dont change position. -// d) Explain why the current return value is different from the target output +// f) Fix the implementation of invert (and write tests to prove it's fixed!) -// e) Fix the implementation of invert (and write tests to prove it's fixed!) +// fixed the implementation and now it is printing the target value diff --git a/Sprint-2/interpret/invert.test.js b/Sprint-2/interpret/invert.test.js new file mode 100644 index 0000000000..64587c0d88 --- /dev/null +++ b/Sprint-2/interpret/invert.test.js @@ -0,0 +1,14 @@ +const invert = require("./invert.js"); +// for an empty object +test(" should return an empty object if given empty object", () => { + expect(invert({})).toEqual({}); +}); +//test for one proprty in an object +test(" should return an object inverted when given one property object", () => { + expect(invert({ a: 1 })).toEqual({ 1: "a" }); +}); +//test for two property in an object +test(" should return an object that is inverted when given two property object", () => { + expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" }); +}); +//