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: 3 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Predict and explain first...
// it will print out My house number is undefined because the address is object rather than array so address[0] will look for property 0 which does not exit

// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working
// Fix anything that isn't working, it should be address.houseNumber in order to get value 42

const address = {
houseNumber: 42,
Expand All @@ -12,4 +13,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
4 changes: 3 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...
// it will throw error or just print out undefined because

// 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
// Because author is not array rather object so we can not use for loop, we use object.values to get loop

const author = {
firstName: "Zadie",
Expand All @@ -11,6 +13,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
10 changes: 7 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
// Predict and explain first...
// It might print out undefined or something else

// 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? i will loop through ingredient to get each single ingredient

const recipe = {
title: "bruschetta",
Expand All @@ -11,5 +12,8 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);

for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
Comment on lines +17 to +19
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.

Alternate approach to explore:
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.

14 changes: 13 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function contains() {}
function contains(object, property) {
if (typeof object !== 'object' || object === null || Array.isArray(object)) {
return false;
}
return object.hasOwnProperty(property);
}



const object={name:'jin', age:13}

console.log (contains(object, 'age'))
console.log (contains(object, 'nice'))

module.exports = contains;
14 changes: 14 additions & 0 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,30 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
object={}
console.log(object,'')
test.todo("contains on empty object returns false");

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
object={name:'jin', age:13}
console.log (contains(object, 'age'))
test.todo("contains age property in object return true")


// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
object={name:'jin', age:13}
console.log (contains(object, 'nice'))
test.todo("contains no nice property in object return false")



// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
object={name:'jin', age:13}
console.log (contains(object, '[1,2,5]'))
test.todo("contains no array property in object return false")
Comment on lines +38 to +49
Copy link
Contributor

@cjyuan cjyuan Mar 21, 2026

Choose a reason for hiding this comment

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

test.todo(" ... ") is just a placeholder to serve as a reminder for "what needs to be done". You were supposed to implement the corresponding Jest tests described in the comments on this file.

Can you implement all the tests in all the .test.js files?

11 changes: 9 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const countryCodeCurrency={}
for (const[country, currency] of countryCurrencyPairs){
countryCodeCurrency[country]=currency
}

return countryCodeCurrency
}
Comment on lines +1 to 8
Copy link
Contributor

Choose a reason for hiding this comment

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

The spacing around the operators is not quite consistent.

Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?


console.log(createLookup([['GB','GBP']]))

module.exports = createLookup;
11 changes: 11 additions & 0 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const createLookup = require("./lookup.js");

const result=createLookup([['US','USD'],['CA','CAD']])
console.log (result)
test.todo("creates a country currency code lookup for multiple codes");

/*
Expand Down Expand Up @@ -33,3 +35,12 @@ It should return:
'CA': 'CAD'
}
*/

// create single country currency in countryCurrencyPairs
const result1=createLookup([['GB','GBP']])
console.log(result1)
test.todo('creates a single county code currency ')

//given an empty country currency pair/array
console.log(createLookup([[]]))
test.todo('return an empty object with empty pair given')
Comment on lines +40 to +46
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Should also implement these tests

  • Better practice to end each statement by a semicolon (;) to reduce chance of errors.

12 changes: 10 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
if (typeof queryString!=="string"||queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const equalIndex = pair.indexOf("=");

const key = pair.slice(0, equalIndex);
const value = pair.slice(equalIndex + 1);

queryParams[key] = value;
}

return queryParams;
}

console.log(parseQueryString("color=blue&quality=good"))
console.log(parseQueryString("equation=x=y+1"))
console.log(parseQueryString(""))
Comment on lines +20 to +22
Copy link
Contributor

Choose a reason for hiding this comment

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

For each of the following function calls, does your function return the value you expect?

parseQueryString("a=b&=&c=d")
parseQueryString("a=b&&c=d")

Note:

  • (Implementing this is optional) In real query string, both key and value are percent-encoded or URL encoded in the URL.
    For example, the string "5%" is encoded as "5%25". So to get the actual value of "5%25"
    (whether it is a key or value in the query string), we need to call a function to decode it.

  • You can also explore the URLSearchParams API.


module.exports = parseQueryString;
28 changes: 28 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,31 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

// Duplicate keys, last one wins
test('duplicate keys overwrite previous', () => {
expect(parseQueryString('color=blue&color=red')).toEqual({
color: 'red'
});
});

// Empty string input
test('returns empty object for empty string', () => {
expect(parseQueryString('')).toEqual({});
});

// Null/invalid input
test('returns empty object for null or non-string', () => {
expect(parseQueryString(null)).toEqual({});
expect(parseQueryString(123)).toEqual({});
});

// Missing value
test('handles keys with empty values', () => {
expect(parseQueryString('empty=')).toEqual({ empty: '' });
});

// Missing key
test('ignores pairs without keys', () => {
expect(parseQueryString('=novalue')).toEqual({ '': 'novalue' });
});
26 changes: 25 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,27 @@
function tally() {}
function tally(array) {
if (!Array.isArray(array)) {
throw new Error("Invalid input: expected an array");
}

const count={}
for(item of array){
if (count[item]){
count[item] += 1
}
else{
count[item] = 1
}
}
return count
}
Comment on lines +2 to +16
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Can you improve the indentation?

  • 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.


console.log(tally([ 2,"bee",2,"apple","apple",2, "banana"]))

try {
console.log(tally('morning'));
} catch (err) {
console.error(err.message);
}


module.exports = tally;
5 changes: 4 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ const tally = require("./tally.js");
// Given an empty array
// When passed to tally
// Then it should return an empty object
console.log(tally([ ]))
test.todo("tally on an empty array returns an empty object");

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

console.log(tally([ 2,"bee",2,"apple","apple",2, "banana"]))
test.todo("tally on single, repeated or duplicate items return counts for each unique item ")
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test.todo('tally on string return throw an error:invalid input, expected an array')
13 changes: 11 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,29 @@ 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 }
console.log(invert({a:1, b:2, apple:4}))

// a) What is the current return value when invert is called with { a : 1 }
// it returns {key:1}

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// it returns {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(obj) lets loop [key, value] pairs easily because .entries(object/array) is built in method in javascript

// d) Explain why the current return value is different from the target output
// Because invertedObj.key=value is wrong which means key of property will be always 'key' and value will remains the same as obj array in invertedObj

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// I put invertedObh[value]= key instead then the function works as expected
Loading