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
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);
}
7 changes: 4 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,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);
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;
34 changes: 34 additions & 0 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,40 @@ test.todo("contains on empty object returns false");
// When passed to contains with a non-existent property name
// Then it should return false



// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
describe('contains function', () => {

test('returns false for an empty object', () => {
const object = {};
expect(contains({}, 'name')).toBe(false);
});

test('returns true when property exists', () => {
const object = { name: 'jin', age: 13 };
expect(contains(object, 'age')).toBe(true);
});

test('returns false when property does not exist', () => {
const object = { name: 'jin', age: 13 };
expect(contains(object, 'nice')).toBe(false);
});

test('returns false when input is an array', () => {
const object = [1, 2, 5];
expect(contains(object, '0')).toBe(false);
});

test('returns false when input is null', () => {
expect(contains(null, 'age')).toBe(false);
});

test('returns false when input is not an object', () => {
expect(contains(123, 'age')).toBe(false);
expect(contains('hello', 'length')).toBe(false);
});

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

for (const[country, currency] of countryCurrencyPairs) {
countryCodeCurrency[country] = currency
}

return countryCodeCurrency
}
Comment thread
cjyuan marked this conversation as resolved.

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


module.exports = createLookup;
30 changes: 30 additions & 0 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,33 @@ It should return:
'CA': 'CAD'
}
*/

// create single country currency in countryCurrencyPairs


//given an empty country currency pair/array

describe("createLookup", () => {

test("creates a country currency code lookup for multiple codes", () => {
const input = [['US', 'USD'], ['CA', 'CAD']];
const expected = { US: 'USD', CA: 'CAD' };

expect(createLookup(input)).toEqual(expected);
});

test("creates a single country code currency", () => {
const input = [['GB', 'GBP']];
const expected = { GB: 'GBP' };

expect(createLookup(input)).toEqual(expected);
});

test("returns an empty object when given an empty pair", () => {
const input = [[]];
const expected = {};

expect(createLookup(input)).toEqual(expected);
});

});
18 changes: 16 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
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("=");
if (!pair.includes("=")) continue;

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

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

if (key === "") continue;

queryParams[key] = value;
}

return queryParams;
}

console.log(parseQueryString("color=blue&quality=good"))
console.log(parseQueryString("equation=x=y+1"))
console.log(parseQueryString(""))
Comment thread
cjyuan marked this conversation as resolved.

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' });
});
27 changes: 26 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
function tally() {}
function tally(array) {
if (!Array.isArray(array)) {
throw new Error("Invalid input: expected an array");
}

const count = Object.create(null);

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

return count;
}

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

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


module.exports = tally;
41 changes: 40 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,51 @@ 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("counts a single item correctly", () => {
expect(tally(['a'])).toEqual({ a: 1 });
});

// Repeated items
test("counts repeated items correctly", () => {
expect(tally(['a', 'a', 'a'])).toEqual({ a: 3 });
});

// Mixed items
test("counts multiple different items", () => {
expect(tally(['a', 'a', 'b', 'c'])).toEqual({
a: 2,
b: 1,
c: 1
});
});


test("tally on single, repeated or duplicate items return counts for each unique item ", () => {
expect(tally([ 2,"bee",2,"apple","apple",2, "banana"])).toEqual({
2: 3,
bee: 1,
apple: 2,
banana: 1
});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("throws error for string input", () => {
expect(() => tally("morning"))
.toThrow("Invalid input: expected an array");
});

test("throws error for number input", () => {
expect(() => tally(123))
.toThrow();
});
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