Skip to content
Open
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// As this is an object (key-value pairs), the elements cannot be accessed through indexing like array.
// Values can be accessed through their relevant key

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +14,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,4 +1,6 @@
// Predict and explain first...
// As object is key-value pairs of data, it isn't exactly iterable like arrays.
// We can loop over the keys or values of an object using Object.entries()

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

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
16 changes: 15 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// It is not accessing the ingredients array correctly.

// 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 +13,17 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);


// approach 2

// let msg = '';

// for (const ingredient of recipe.ingredients) {
// msg += ingredient + '\n';
// }

// console.log(`${recipe.title} serves ${recipe.serves}
// ingredients:
// ${msg}`);
12 changes: 11 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function contains() {}
function contains(obj, x) {
for (const key in obj) {
if (key == x) {
return true
}
}
return false
}

module.exports = contains;

// Object.hasOwn(obj, key)
// This ensures the property belongs directly to the object, not its prototype.
31 changes: 30 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,45 @@ 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 on empty object returns false", () => {
expect(contains({})).toStrictEqual(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 an existing property name returns true", () => {
expect(contains({a: 1, b: 2, c: 3}, 'c')).toStrictEqual(true);
expect(contains({name: "John Doe", job: "developer"}, 'job')).toStrictEqual(true)
expect(contains({color: "blue", size: "xxl", madeOf: "China"}, 'madeOf')).toStrictEqual(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 properties with non-existent property name return false", () => {
expect(contains({a: 1, b: 2, c: 3}, 'g')).toStrictEqual(false);
expect(contains({a: 1, b: 2, c: 3}, 'abc')).toStrictEqual(false);
expect(contains({name: "John Doe", age: 31}, 'job')).toStrictEqual(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", "b", "c"])).toStrictEqual(false);
Copy link
Copy Markdown
Contributor

@cjyuan cjyuan Mar 20, 2026

Choose a reason for hiding this comment

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

Does your function return the value you expect from the following function calls?

contains(["a", "b", "c"], "1")
contains("abc", "1")
contains(null, "1")
contains(undefined, "1")

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.

Now, it handles correctly.

expect(contains("I am a string")).toStrictEqual(false);
expect(contains(777)).toStrictEqual(false);
})

test("contains() handles arrays, strings, null, and undefined safely", () => {
expect(contains([["a", "b", "c"], "1"])).toStrictEqual(false);
expect(contains(["abc", "1"])).toStrictEqual(false);
expect(contains(null, "1")).toStrictEqual(false);
expect(contains(undefined, "1")).toStrictEqual(false);
})
6 changes: 3 additions & 3 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(arr) {
return Object.fromEntries(arr);
}

module.exports = createLookup;
module.exports = createLookup;
16 changes: 15 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
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", () => {
const input = [['US', 'USD'], ['CA', 'CAD']];
const givenInput = createLookup(input);
const targetOutput = {US: 'USD', CA: 'CAD'};

expect(givenInput).toEqual(targetOutput);
});

test("creates a country currency code lookup for multiple codes", () => {
const input = [['AUS', 'AUD'], ['GB', 'GBP'], ['US', 'USD']];
const givenInput = createLookup(input);
const targetOutput = {AUS: 'AUD', GB: 'GBP', US: 'USD'};

expect(givenInput).toEqual(targetOutput);
})

/*

Expand Down
30 changes: 26 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,34 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}

if (!queryString) return queryParams;

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
// skip empty pairs (e.g. &&)
if (pair === "") continue;

const index = pair.indexOf("=");

let key, value;

if (key === -1) {
key = pair;
value = "";
} else {
key = pair.substring(0, index);
value = pair.substring(index + 1);
}

// decoding URL
try {
key = decodeURIComponent(key);
value = decodeURIComponent(value);
} catch (error) {

}

queryParams[key] = value;
}
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 each of the following function calls, does your function return the value you expect?

parseQueryString("a=b&=&c=d")
parseQueryString("a=")
parseQueryString("=b")
parseQueryString("a=b&&c=d")
parseQueryString("a%25b=c%26d")

Note:

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

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.

`function parseQueryString(queryString) {
const queryParams = {};

if (!queryString) return queryParams;

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

for (const pair of keyValuePairs) {
// skip empty pairs (e.g. &&)
if (pair === "") continue;

const index = pair.indexOf("=");

let key, value;

if (key === -1) {
  key = pair;
  value = "";
} else {
  key = pair.substring(0, index);
  value = pair.substring(index + 1);
}

// decoding URL
try {
  key = decodeURIComponent(key);
  value = decodeURIComponent(value);
} catch (error) {
  
}

queryParams[key] = value;

}

return queryParams;
}`


Expand Down
41 changes: 41 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,45 @@ test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
});
expect(parseQueryString("token=abc=123=xyz")).toEqual({
"token": "abc=123=xyz",
});
expect(parseQueryString("shop=myshop.myshopify.com")).toEqual({
"shop": "myshop.myshopify.com",
});
});

test("parses querystring values containing = and &", () => {
expect(parseQueryString("category=shoes&color=black&size=10")).toEqual({
category: "shoes", color: "black", size: "10"
});
expect(parseQueryString("page=2&limit=10")).toEqual({
page: "2", limit: "10"
});
});

test("parses querystring values containing +", () => {
expect(parseQueryString("q=javascript+array+methods")).toEqual({
q: "javascript+array+methods"
})
});

test("new tests", () => {
expect(parseQueryString("a=b&=&c=d")).toEqual({
a: "b", "": "", c: "d"
});
expect(parseQueryString("a=")).toEqual({
a: ""
});
expect(parseQueryString("=b")).toEqual({
"": "b"
});
expect(parseQueryString("a=b&&c=d")).toEqual({
a: "b", c: "d"
});
expect(parseQueryString("a%25b=c%26d")).toEqual({
"a%b": "c&d"
})
})


24 changes: 23 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("The input should be an array!");
}

const countObj = Object.create(null);

for (const item of arr) {

if (countObj[item]) {
countObj[item] = countObj[item] + 1;
} else {
countObj[item] = 1;
}
}

return countObj;
}

module.exports = tally;

// console.log(tally(['a', 'a', 'a', 'a', 'a', 'b', 'c']));


// tally(['a', 'a', 'a', 'a', 'a', 'b', 'c']), target output: { a : 5, b: 1, c: 1 }
36 changes: 35 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,46 @@ 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 on an array with duplicate items returns the counts for each uniqe item", () => {
const input = ['a', 'a', 'b', 'c'];
const givenInput = tally(input);
const targetOutput = { a : 2, b: 1, c: 1 };

expect(givenInput).toEqual(targetOutput);
})

test("tally on an array with duplicate items returns the counts for each uniqe item", () => {
const input = ['a', 'a', 'bx', 'bx', 'b', 'b', 'b', 'b', 'c'];
const givenInput = tally(input);
const targetOutput = { a : 2, bx: 2, b: 4, c: 1 };

expect(givenInput).toEqual(targetOutput);
})

test("tally on ", () => {
const input = ["toString", "toString"];
const givenInput = tally(input);
const targetOutput = {toString: 2};

expect(givenInput).toEqual(targetOutput);
})

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error

test("tall on invalid input throws an error", () => {
expect(() => tally("I am a string")).toThrow();
expect(() => tally(777)).toThrow();
expect(() => tally(true)).toThrow();
expect(() => tally(undefined)).toThrow();
expect(() => tally(null)).toThrow();
})
11 changes: 10 additions & 1 deletion 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 }
// { key: 1 }

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

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

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries() returns an array of a given object's own enumerable string-keyed property key-value pairs
// It is needed here in order to loop over each of the key-value pairs

// d) Explain why the current return value is different from the target output
// It is because `.key `is used on object.
// Instead [] should be used

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// fixed the issue by using `invertedObj[value] = key`
// It is swaping the key and value in the returned object
23 changes: 23 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,26 @@

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

function countWords(str) {
const arr = str.replace(/[^a-zA-Z0-9\s]/g, '').toLowerCase().split(" ")

let countObj = {};

for (const item of arr) {

if (countObj[item]) {
countObj[item] = countObj[item] + 1
} else {
countObj[item] = 1;
}
}

// `Object.entries()` converts object to array
const sortedArray = Object.entries(countObj).sort((a, b) => b[1] - a[1]);

// `Object.fromEntries()` coverts the sorted array to object
return Object.fromEntries(sortedArray);
}

console.log(countWords("you? and! me, and you. me me me me hello Me"));
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 your function return what you expect in the following function calls?

countWords("Hello,World! Hello World!");
countWords("constructor constructor");
countWords("          Hello World      ");

Note: The spec is not clear about exactly what to expect from these function calls. This is just for self-check.

7 changes: 6 additions & 1 deletion Sprint-2/stretch/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// refactor calculateMode by splitting up the code
// into smaller functions using the stages above

function calculateMode(list) {
function calculateFrequency(list) {
// track frequency of each value
let freqs = new Map();

Expand All @@ -19,6 +19,11 @@ function calculateMode(list) {

freqs.set(num, (freqs.get(num) || 0) + 1);
}
return freqs;
}

function calculateMode(list) {
const freqs = calculateFrequency(list);

// Find the value with the highest frequency
let maxFreq = 0;
Expand Down
Loading
Loading