diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..d2809c142 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,8 +1,13 @@ // Predict and explain first... - +I predict that the code will not log out the housenumber +because there is no method declared inside the object to return the object's properties. // This code should log out the houseNumber from the address object // but it isn't working... // Fix anything that isn't working +when I run the code it give this message as output (My house number is undefined). +when I changed the console.log declaration to : +console.log('my house number is' + ' ' + address.houseNumber); +it give the right output for the house number. const address = { houseNumber: 42, @@ -12,4 +17,9 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +// console.log(`My house number is ${address[0]}`); +console.log('my house number is' + ' ' + address.houseNumber); + +when I checked an AI tool it explained that address is an object, not an array. +So address[0] tries to access a property with the key "0" — which doesn’t exist → undefined. +to access the houseNumber property we need to use the property name not an index. diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..59d644c17 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,5 @@ // Predict and explain first... - +I predict that the program will not give the property values of the object because the for loop is not correctly calling the values of the auther 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 @@ -11,6 +11,17 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); -} +// for (const value of author) { +// console.log(value); +// } + +let text = " "; +for (let x in author) { + text+= author[x] + "\n "; +}; +console.log(text); + +// or: +const values = Object.values(author); +console.log(values); + diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..aee3d6c4d 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,6 @@ // Predict and explain first... +I expect that the program will not log out the right output because the ingridents are not being accssed correctly. +as the recipe object is being logged out rather than the ingridents. // This program should log out the title, how many it serves and the ingredients. // Each ingredient should be logged on a new line @@ -12,4 +14,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients .join("\n") }`); diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a..c7a188e23 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..e4c60cf22 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -20,16 +20,57 @@ 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.todo("contains on empty object returns false"); +test("contains an empty object returns false", () => { + const obj = {}; + expect(contains(obj, "a")).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 properties return ture", () => { + const obj = { a: 1, b: 2, c: 3 }; + expect(contains(obj, "a")).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 propreties return false for non-existing property", () => { + const obj = { a: 1, b: 2, c: 3 }; + expect(contains(obj, "d")).toBe(false); +}); // Given invalid parameters like an array // When passed to contains // Then it should return false or throw an error +test("contains with invalid parameters return false", () => { + const obj = ["a", "b", "c", "d"]; + expect(contains(obj, "a")).toBe(false); +}); + +// Give an error for null values: + +test("contains give an error when obj is null", ()=> { + expect((()=> contains(null , "a"))).toThrow(); +}); +// throw error for undefiend values: +test("contains give an error when obj is undefined", ()=> { + expect((()=> contains(undefined , "a"))).toThrow(); +}); + +// throw error for non object typs: +test(" give an error when obj is a number", ()=> { +expect((()=> contains(123, "a"))).toThrow(); +}); + +// throw error when obj is a string: +test(" give an error when obj is a string", ()=> { + expect((()=> contains("hello", "a"))).toThrow(); +}); + +// give error when obj is boolean: +test(" give an error when obj is a boolean", ()=> { + expect((()=> contains(true, "a"))).toThrow(); +}); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..5c120898b 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,5 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + return Object.fromEntries(countryCurrencyPairs); } module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a..f6b1c33a4 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -33,3 +33,11 @@ It should return: 'CA': 'CAD' } */ +test('Create a lookup object of key value pairs from an array of code pairs', ()=> +{ + const countryCurrencyPairs = [["US", "USD"], ["CA", "CAD"]]; + expect(createLookup(countryCurrencyPairs)).toEqual({ + US: "USD", + CA: "CAD"}); + +}); \ No newline at end of file diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..1ee34343a 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +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 (!queryString) return queryParams; + + const pairs = queryString.split("&"); + + for (const pair of pairs) { + if (!pair) continue; + + const index = pair.indexOf("="); + + let key, value; + + if (index === -1) { + key = decodeURIComponent(pair); + value = true; + } else { + key = decodeURIComponent(pair.slice(0, index)); + value = decodeURIComponent(pair.slice(index + 1)); + } + queryParams[key] = value; } return queryParams; } -module.exports = parseQueryString; +module.exports = parseQueryString; \ No newline at end of file diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 3e218b789..19815d900 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -10,3 +10,43 @@ test("parses querystring values containing =", () => { "equation": "x=y+1", }); }); +// Empty query string: +test("parses querystring with empty string", () => { + expect(parseQueryString("")).toEqual({}); +}); +// Key without value: +test("parses querystring handles key without =", () => { + expect(parseQueryString("flag")).toEqual({ + flag: true, + }); +}); + +// Empty value: +test("parses querystring with empty value", () => { + expect(parseQueryString("key=")).toEqual({ + key: "", + }); +}); + +// Multiple params: +test("parses querystring with multiple params", () => { + expect(parseQueryString("name=John&age=30&city=New+York")).toEqual({ + name: "John", + age: "30", + city: "New York" + }); +}); + +// Value containing = : +test("parses querystring values containing =", () => { + expect(parseQueryString("equation=x=y+1")).toEqual({ + "equation": "x=y+1", + }); +}); + +// Trailing ampersand: +test("parses querystring with trailing ampersand", () => { + expect(parseQueryString("name=John&")).toEqual({ + name: "John" + }); +}); diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..3ee2b7767 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,19 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)) { + throw new Error("Input must be an array"); + } + + const result = {}; + + for (let item of items) { + if (Object.hasOwn(result, item)) { + result[item] += 1; + } else { + result[item] = 1; + }npm TextDecoderStream + } + + return result; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 2ceffa8dd..d0cf86fce 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -19,11 +19,17 @@ 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("tally on an array of items returns an object with counts of each item", () => { + const items = ["a","a","b","c"] + expect(tally(items)).toEqual ({a:2, b:1, c: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", () => { + const items = []; + expect(tally(items)).toEqual({}); +}); // Given an array with duplicate items // When passed to tally @@ -32,3 +38,7 @@ test.todo("tally on an empty array returns an empty object"); // Given an invalid input like a string // When passed to tally // Then it should throw an error +test('tally on invalid input throw an error', ()=> { + const items = 'invalid input'; + expect(() => tally(items)).toThrow(); +}) \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..6af13915e 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,34 @@ 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 } - +the current return value is {key :1}. // b) What is the current return value when invert is called with { a: 1, b: 2 } - +the current return value is {key: 2}. // c) What is the target return value when invert is called with {a : 1, b: 2} - +the target return value should be {"1": "a", "2" : "b"}. // c) What does Object.entries return? Why is it needed in this program? - +object.entries() returns an array of key/value pairs of an object. it is needed in this program to loop through the object and access both the key and value at the same Time . // d) Explain why the current return value is different from the target output - +because using the key element in the loop is not correct. as using it in this line : invertedObj.key = value; +creats a propty called key rather than using it as a variable . it also doesn't swap the key/vlaue . // e) Fix the implementation of invert (and write tests to prove it's fixed!) + + +test ("invert should swap keys and values in an object with one key and value", ()=> { +expect(invert({a :1})).toEqual({"1" :"a"}) +}); + +test ("invert should swap keys and values in an object with multiple keys and values", ()=> { +expect(invert({a :1, b: 2})).toEqual({"1" :"a", "2" : "b"}) +}); + +test('invert should return an empty object when given an empty object', ()=> { + expect(invert({})).toEqual({}); +}); \ No newline at end of file