Skip to content
Closed
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
2 changes: 1 addition & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
15 changes: 13 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,17 @@ const author = {
alive: true,
};

for (const value of author) {
// Prediction:
// The code will not log the property values of the author object as intended.
// This causes the error because author is not iterable.

// Explanation:

// The original code likely used a for...in loop which iterates over the keys of the object, not the values.
// Therefore, logging the key would not give the desired property values.
// By using Object.values(author), we can directly iterate over the values of the object.

// Fixed code:
for (const value of Object.values(author)) {
console.log(value);
}
}
12 changes: 11 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};


// Prediction:
// The code will not log the ingredients correctly because it is trying to log the entire recipe object instead of just the ingredients array.
// This will result in an output that includes the object structure rather than the individual ingredients.

// Explanation:
// The template literal is incorrectly using `${recipe}` which outputs the whole object instead of iterating over the ingredients array.
// To fix this, we need to access the ingredients property and format it correctly, possibly by joining the array elements with new line characters.

// Fixed code:
console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join('\n')}`);
10 changes: 9 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function contains() {}
function contains(obj, key) {
// Check if the first argument is a valid object
if (typeof obj !== "object" || obj === null) {
return false;
}

// Check if the key exists in the object (only own properties)
return Object.prototype.hasOwnProperty.call(obj, key);
}

module.exports = contains;
38 changes: 37 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,55 @@ as the object doesn't contains a key of 'c'
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise


// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("contains on empty object returns false", () => {
expect(contains({}, "a")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("contains on object with existing property returns true", () => {
expect(contains({ a: 1, b: 2 }, "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 on object with non-existent property returns false", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains on invalid parameters returns false", () => {
expect(contains([], "a")).toBe(false);
});

// === NEW TESTS ADDED BELOW ===

// Test for inherited properties (should return false)
test("contains should return false for inherited properties", () => {
expect(contains({a: 1, b: 2}, 'toString')).toBe(false);
});

// Test with arrays (should work properly)
test("contains should work with arrays", () => {
expect(contains(["a", "b", "c"], "0")).toBe(true);
expect(contains(["a", "b", "c"], "1")).toBe(true);
expect(contains(["a", "b", "c"], "2")).toBe(true);
expect(contains(["a", "b", "c"], "3")).toBe(false); // check index that does not exist
});


// Test with array as object
test("contains should handle arrays correctly", () => {
const arr = ['x', 'y', 'z'];
expect(contains(arr, '0')).toBe(true);
expect(contains(arr, '2')).toBe(true);
expect(contains(arr, '3')).toBe(false);
});
12 changes: 10 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = {};

for (const pair of countryCurrencyPairs) {
const countryCode = pair[0];
const currencyCode = pair[1];
Comment on lines +4 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also use array destructuring to shorten the code on lines 4-6 (if you have time).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you're right

lookup[countryCode] = currencyCode;
}

return lookup;
}

module.exports = createLookup;
17 changes: 16 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
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", () => {
// Given: An array of arrays representing country code and currency code pairs
const countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
];

// When: createLookup function is called with the country-currency array
const result = createLookup(countryCurrencyPairs);

// Then: It should return an object where keys are country codes and values are currency codes
expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});

/*

Expand Down
22 changes: 19 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {

if (!queryString || queryString.length === 0) {
return queryParams;
}

// Remove leading '?' if present
if (queryString.startsWith("?")) {
queryString = queryString.substring(1);
}

const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
// Only split on the first '=' to handle values containing '='
const equalsIndex = pair.indexOf("=");
if (equalsIndex === -1) {
// Handle keys without values
const key = decodeURIComponent(pair);
queryParams[key] = "";
} else {
const key = decodeURIComponent(pair.substring(0, equalsIndex));
const value = decodeURIComponent(pair.substring(equalsIndex + 1));
queryParams[key] = value;
}
Comment on lines +3 to +26
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This work great!
In practice, you can consider using a JS built-in function to parse query string.

}

return queryParams;
Expand Down
21 changes: 20 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
function tally() {}
function tally(items) {

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

const result = Object.create(null);


for (const item of items) {
if (result[item]) {
result[item] += 1;
} else {
result[item] = 1;
}
}

return result;

}

module.exports = tally;
10 changes: 9 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ const tally = require("./tally.js");
// 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("returns correct counts for array with duplicate items", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("throws an error for invalid input (not an array)", () => {
expect(() => tally("a")).toThrow("Input must be an array");
});
42 changes: 34 additions & 8 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,50 @@

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

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

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

return invertedObj;
}

// a) What is the current return value when invert is called with { a : 1 }
// Object.entries({ a: 1 }) → [["a", 1]]
// The loop runs once with key = "a" and value = 1
// Inside the loop:
// invertedObj.key = value;
// creates a property literally named "key", not "a"
// So the result is { key: 1 }.
// Answer: { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// Object.entries({ a: 1, b: 2 }) → [["a", 1], ["b", 2]]
// First loop: invertedObj.key = 1 → { key: 1 }
// Second loop: invertedObj.key = 2 → overwrites previous value
// Final result: { key: 2 }
// Answer: { key: 2 }

// c) What is the target return value when invert is called with {a : 1, b: 2}
// { "1": "a", "2": "b" }
// The goal is to swap keys and values, so keys become values and values become keys

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries(obj) returns an array of [key, value] pairs from the object
// Object.entries({ a: 1, b: 2 });
// returns [["a", 1], ["b", 2]]
// It’s needed so that the loop:
// for (const [key, value] of Object.entries(obj))
// can easily access both the key and value in one step.

// d) Explain why the current return value is different from the target output
// The current code uses invertedObj.key = value, which creates a property literally named "key".
// To use the variable key dynamically, you must use bracket notation:
// invertedObj[value] = key;

// 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;
}

module.exports = invert;

36 changes: 36 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,39 @@

3. Order the results to find out which word is the most common in the input
*/



function countWords(str) {
// Use Object.create(null) to avoid inherited properties
const wordCounts = Object.create(null);

if (!str || str.trim() === "") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Between (typeof str != "string" || str.trim() === "") and your current condition, which one do you think is better, and why?

return wordCounts;
}

// Split into words and handle punctuation/case
const words = str.split(/\s+/);

for (const word of words) {
// Remove punctuation and convert to lowercase
const cleanWord = word
.toLowerCase()
.replace(/[.,!?]/g, "")
.trim();

// Skip empty strings
if (cleanWord === "") continue;

// Count the word
if (wordCounts[cleanWord]) {
wordCounts[cleanWord] += 1;
} else {
wordCounts[cleanWord] = 1;
}
}

return wordCounts;
Comment on lines +40 to +61
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you do some pre-processing on str before line 41, you could simplify the code within the loop by about 50% (or more).

}

module.exports = countWords;