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
14 changes: 12 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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.
19 changes: 15 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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);

4 changes: 3 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,4 +14,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients .join("\n") }`);
4 changes: 3 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
function contains() {}
function contains(obj, key) {
return key in obj;
}
Comment on lines +1 to +3
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.

Consider the following two approaches for determining if an object contains a property:

  let obj = {}, propertyName = "toString";
  console.log( propertyName in obj );                // true
  console.log( Object.hasOwn(obj, propertyName) );   // false

Which of these approaches suits your needs better?
For more info, you can look up JS "in" operator vs Object.hasOwn.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I understand your point her but the function used still give the required output, so I'm not sure if I have to change it to Object.hasOwn( ) method or to keep it.

Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Apr 14, 2026

Choose a reason for hiding this comment

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

Have you found out the difference between key in object and Object.hasOwn(object, key)?

As long as you know their differences, you can use either approach in your implementation because the spec is not clear exactly how the function should behave.


module.exports = contains;
21 changes: 20 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,35 @@ 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);
});
Comment on lines 45 to +51
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 cannot yet confirm that the function correctly returns false when the first argument is an array.
This is because contains(['a','b','c','d'], "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.

Besides array, you should also test other invalid parameters such as null, undefined, strings, to ensure your function works correctly.


// or to throw error
// }))
4 changes: 2 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
return Object.fromEntries(countryCurrencyPairs);
}

module.exports = createLookup;
8 changes: 8 additions & 0 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"});

});
27 changes: 20 additions & 7 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -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;
40 changes: 40 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
});
});
11 changes: 10 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}
const result ={ };
for (let item of items){
result[item]=(result[item] || 0)+1;
}
Comment on lines +5 to +8
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 returns the value you expect?

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

Suggestion:

  • Look up an approach to create an empty object with no inherited properties, or
  • use Object.hasOwn()

return result;
}

module.exports = tally;
14 changes: 12 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
})
26 changes: 20 additions & 6 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({});
});