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
5 changes: 4 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Predict and explain first...
//if you run this code with console.log('My house number is ${address[0]}'); it well log out 'My house number is undefined' becouse adress is an object and not an array.
// to fix this we need to remove (`My house number is ${address[0]}`); and chnage it to (adress.houseNumber) becouse we need to access the houseNumber property of the address object.
// Then it well log out 42 as expected.

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +15,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(address.houseNumber);
2 changes: 1 addition & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ const author = {
alive: true,
};

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

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(recipe.title);
console.log(`Serves: ${recipe.serves}`);
console.log("Ingredients:");
for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
Comment on lines +16 to +18
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Your code works.

Here is an alternative worth exploring:
Since ingredient values are separated by '\n' in the output, we could also use
Array.prototype.join() to construct the equivalent string and then output the resulting string.

15 changes: 14 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function contains() {}
function contains(obj, prop) {
if (typeof obj !== "object" || obj === null) {
return false;
}
if (typeof prop !== "string") {
return false;
}
Comment on lines +5 to +7
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Other types of value can also be used as key (property name) -- They are just converted to equivalent string implicitly.

So obj[1] or obj[1.0] or obj[3-2] are treated the same as obj["1"].


if (Array.isArray(obj)) {
return false;
}
return obj.hasOwnProperty(prop);
}


module.exports = contains;
13 changes: 12 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,27 @@ 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("contains an empty object returns false", function () {
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 returns true when property exists", function () {
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 returns false when property does not exist", function () {
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 returns false when passed an array", function () {
expect(contains([1, 2, 3], "a")).toBe(false);
});
Comment on lines +44 to +46
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This test does not yet confirm that the function correctly returns false when the first argument is an array.
This is because contains([1, 2, 3], "a") could also return false simply because "a" is not a key of the array.

Arrays are objects, with their indices acting as keys. A proper test should use a valid
key to ensure the function returns false specifically because the input is an array, not because the key is missing.

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

for (const pair of countryCurrencyPairs) {
lookup[pair[0]] = pair[1];
}
return lookup;
}

module.exports = createLookup;
5 changes: 4 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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", function() {
expect(createLookup([['US', 'USD'], ['CA', 'CAD']])).toEqual({'US': 'USD', 'CA': 'CAD'});
});


/*

Expand Down
2 changes: 1 addition & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const [key, value] = pair.split(/=(.*)/s);
queryParams[key] = value;
Comment on lines -9 to 10
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

For the following function call, does your function return the value you expect?

parseQueryString("key1=value1&&key2=value2")

}

Expand Down
18 changes: 18 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,23 @@ const parseQueryString = require("./querystring.js")
test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",

});
});

test("parses empty querystring", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses querystring with multiple key value pairs", () => {
expect(parseQueryString("name=Karim&age=20")).toEqual({
"name": "Karim",
"age": "20"
});
});

test("parses querystring with empty value", () => {
expect(parseQueryString("name=")).toEqual({
"name": ""
});
});
12 changes: 11 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error('Items must be an array');
}
const counts = {};
for (const item of items) {
counts[item] = (counts[item] || 0) + 1;
}
return counts;
Comment on lines +5 to +9
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Does the following function call return the value you expect?

tally(["toString", "toString"]);

Suggestion: Look up an approach to create an empty object with no inherited properties.


}

module.exports = tally;
11 changes: 10 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,21 @@ 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("tally counts unique 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("tally throws an error for invalid input", () => {
expect(() => tally("not an array")).toThrow("Items must be an array");
});

31 changes: 30 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,49 @@ 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 }

// It returns { key: 1 } because invertedObj.key always writes the word "key"

//---------------------------------------------------------------------------------------//

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

// It returns { key: 2 } because it keeps overwriting the same "key" each time

//---------------------------------------------------------------------------------------//

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

// It should return { "1": "a", "2": "b" } because the keys and values are swapped

//---------------------------------------------------------------------------------------//

// c) What does Object.entries return? Why is it needed in this program?

// Object.entries turns the object into an array like [["a", 1], ["b", 2]]
// We need it so we can loop through each key and value one by one

//---------------------------------------------------------------------------------------//

// d) Explain why the current return value is different from the target output

// Because invertedObj.key writes the word "key" literally
// instead of using the actual value as the key
// changing it to invertedObj[value] = key fixes this

//---------------------------------------------------------------------------------------//

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

// Fixed by changing invertedObj.key = value to invertedObj[value] = key
// Tests are written in invert.test.js


module.exports = invert;
16 changes: 16 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const invert = require("./invert.js");

// Test 1 swaps the keys and values
test("invert swaps keys and values", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" });
});

// Test 2 empty object should return empty object
test("invert on an empty object returns an empty object", () => {
expect(invert({})).toEqual({});
});

// Test 3 single key value pair
test("invert works with a single pair", () => {
expect(invert({ x: 10 })).toEqual({ "10": "x" });
});
Loading