Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
// Predict and explain first...It should report the house number as 42

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -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}`);


5 changes: 3 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
// Predict and explain first...It should report 40 as the value

// 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
Expand All @@ -11,6 +11,7 @@ const author = {
alive: true,
};

for (const value of author) {
//for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
13 changes: 9 additions & 4 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
// Predict and explain first...It should report the title, serves & ingredient but ingredient might have issues.

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -10,6 +10,11 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
// 1. Log the title and servings
console.log(`${recipe.title} serves ${recipe.serves}`);
console.log("Ingredients:");

// 2. Loop through ingredients to log each on a new line
recipe.ingredients.forEach(ingredient => {
console.log(`- ${ingredient}`);
});
16 changes: 16 additions & 0 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
function contains() {}

module.exports = contains;
*/

function contains(obj, propName) {
if (
typeof obj !== 'object' ||
obj === null ||
(typeof propName !== 'string' && typeof propName !== 'number' && typeof propName !== 'symbol')
) {
return false;
}
return propName in obj;
}

module.exports = contains;

8 changes: 5 additions & 3 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function createLookup() {
// implementation here
}

const createLookup = (countryCurrencyPairs) => {
return Object.fromEntries(countryCurrencyPairs);
};


module.exports = createLookup;
39 changes: 39 additions & 0 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/*
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
Expand All @@ -12,5 +13,43 @@ function parseQueryString(queryString) {

return queryParams;
}
*/






function parseQueryString(queryString) {
if (!queryString || queryString.trim() === "") {
return {};
}

const cleanedString = queryString.startsWith("?") // check if it has ? and remocve
? queryString.substring(1)
: queryString;

const pairs = cleanedString.split("&"); //Split
const result = {};

for (const pair of pairs) {
// split on the FIRST '='
const [key, ...valueParts] = pair.split("=");
const value = valueParts.join("="); // Rejoin if value contained '='

// Decode key and value to handle special characters
const decodedKey = decodeURIComponent(key);
const decodedValue = value ? decodeURIComponent(value) : "";

if (decodedKey) {
result[decodedKey] = decodedValue;
}
}

return result;

}



module.exports = parseQueryString;
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}


function tally(arr) {
// Input validation

if (!Array.isArray(arr)) {
throw new Error("Input must be an array");
}

// The reduction method effectively builds the frequency map
return arr.reduce((acc, item) => {
acc[item] = (acc[item] || 0) + 1;
return acc;
}, {});
}


module.exports = tally;
31 changes: 28 additions & 3 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}


function invert(obj) {
const invertedObj = {};

Expand All @@ -16,14 +17,38 @@ function invert(obj) {
return invertedObj;
}

// a) What is the current return value when invert is called with { a : 1 }
console.log(invert({ a: 1, b: 2 }));


// a) What is the current return value when invert is called with { a : 1 }. ANSWER = { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// b) What is the current return value when invert is called with { a: 1, b: 2 }. ANSWER = { key: 2 }

// c) What is the target return value when invert is called with {a : 1, b: 2}
// c) What is the target return value when invert is called with {a : 1, b: 2}. ANSWER = { "1": "a", "2": "b" }

// c) What does Object.entries return? Why is it needed in this program?
//ANSWER:
// c(i) Object.entries(obj) returns an array of [key, value] pairs e.g ({ a: 1, b: 2 }) → [["a", 1], ["b", 2]]
// c(ii) Object.entries allows you to loop through both keys and values .

// d) Explain why the current return value is different from the target output
//ANSWER: It is different because the line is assigning to the literal property "key" instead of using the variable key or value

// e) Fix the implementation of invert (and write tests to prove it's fixed!)

function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj[value] = key;
}

return invertedObj;
}



console.log(invert({ a: 1 }))
console.log(invert({ a: 1, b: 2 }))


Loading