diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af83..449a8fc9a 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,7 +1,7 @@ // Predict and explain first... // This code should log out the houseNumber from the address object -// but it isn't working... +// but it isn't working.. // Fix anything that isn't working const address = { @@ -12,4 +12,6 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); +console.log(`My house number is ${address['houseNumber']}`); + diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977..7bf9d075e 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,5 +1,4 @@ -// Predict and explain first... - +// Predict and explain first.. // 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 +10,6 @@ const author = { alive: true, }; -for (const value of author) { +for (const value of Object.values(author)) { //for-of is for arrays not objects so use Object.values console.log(value); -} +} \ No newline at end of file diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cd..9f1ad62ea 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,8 +1,7 @@ // Predict and explain first... - // 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? +// How can you fix it? const recipe = { title: "bruschetta", @@ -12,4 +11,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..4c18f5d30 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,10 @@ -function contains() {} +function contains(obj, property) { -module.exports = contains; + if(obj === null || obj === undefined){ + return false + } + return obj[property] !== undefined; +} + +module.exports = contains; +// work done. \ No newline at end of file diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f..b5311a26d 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,12 @@ -function createLookup() { - // implementation here +function createLookup(pairs) { + const lookup = {}; + for (const pair of pairs){ + if(pair && pair.length === 2){ + lookup[pair[0]] = pair[1]; + } + } + return lookup; } module.exports = createLookup; +// in working order diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f3..6c4f68fb2 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +1,20 @@ function parseQueryString(queryString) { const queryParams = {}; - if (queryString.length === 0) { + if (queryString === "") { //removed the .length method return queryParams; } const keyValuePairs = queryString.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); + //const [key, value] = pair.split("="); //this line is making kak and its so frustrating cause it assigns value ='x' (test case 1) and leaves the rest of the equation. + const parts = pair.split("="); + const key = parts[0]; + const value = parts.length > 1 ? parts.slice(1).join('=') : ''; //clunky but it works to split them and stitch it together again queryParams[key] = value; } + return queryParams; } -module.exports = parseQueryString; +module.exports = parseQueryString; \ No newline at end of file diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812..a52749997 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,14 @@ -function tally() {} +function tally(items) { + if (!Array.isArray(items)){ + throw new TypeError('that aint no array bay bay'); //okay so this is the biggie smalls and its working + } + const counts = {}; + + for (const item of items){ + counts[item] = (counts[item] || 0) +1; //simple sexy core logic + } + return counts; +} module.exports = tally; +//no updates. works well \ No newline at end of file diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1f..d509603dc 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -10,20 +10,30 @@ function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + const numKey = Number(key); + + if (!isNaN(numKey) && String(numKey) === key) { + invertedObj[value] = numKey; + } else { + invertedObj[value] = key; //convert key to number if it is a number + //how do i convert it to a number if it is a number? + //if the key is a number, convert it to a number, otherwise keep it as a string + } } return invertedObj; } +module.exports = invert; -// a) What is the current return value when invert is called with { a : 1 } - -// b) What is the current return value when invert is called with { a: 1, b: 2 } - +// a) What is the current return value when invert is called with { a : 1 } +// {'1':'a'} +// b) What is the current return value when invert is called with { a: 1, b: 2 } +// { '1': 'a', '2': 'b'} // 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? - +// its a method that takes a obj and returns the [key, value] pairs. // d) Explain why the current return value is different from the target output - +// cause its converted to strings, the key must be a int and the value must be a string. // e) Fix the implementation of invert (and write tests to prove it's fixed!) +// in file invert.test.js \ No newline at end of file