Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
b8050db
update: fic code to call address is an object sprint-2 address.js
SuWebOnes Jul 19, 2025
a79f207
update: fix code using Object.values() for sprint-2 author.js
SuWebOnes Jul 19, 2025
64b231f
update: fix code to list in ingredients inside object ingredients lis…
SuWebOnes Jul 19, 2025
614a708
add json file
SuWebOnes Jul 20, 2025
3cea226
changing json file
SuWebOnes Jul 20, 2025
06df2e3
test for Normal input of multiple country-currency pairs
SuWebOnes Jul 21, 2025
d48e6ae
editing spell
SuWebOnes Jul 21, 2025
76f4f9e
update: test array input
SuWebOnes Jul 21, 2025
c231d71
Duplicate country codes of last value wins
SuWebOnes Jul 21, 2025
9667024
update code function for lockup
SuWebOnes Jul 21, 2025
256f4bf
update: fix code for an edge case the implementation sprint-2/impleme…
SuWebOnes Jul 21, 2025
7c609f1
update: testing other edge cases such as URL-encoded characters, Key…
SuWebOnes Jul 21, 2025
60de1a2
update: fix test tally on an empty array returns an empty object tall…
SuWebOnes Jul 21, 2025
de11b52
update: test for return counts for each unique item tally.js
SuWebOnes Jul 21, 2025
ea4f6b0
update: test for invalid input tally.test.js
SuWebOnes Jul 21, 2025
ba34de6
update: fix function code for tally based on the test requirements ta…
SuWebOnes Jul 21, 2025
e971976
update: explain cases for original code for sprit-2 implementation in…
SuWebOnes Jul 21, 2025
e7dab95
update: fix code for sprit-2 implementation invert.js
SuWebOnes Jul 21, 2025
7295458
update: include .export line
SuWebOnes Jul 21, 2025
a9a6722
update: Test invert for case empty object returns empty object in sp…
SuWebOnes Jul 21, 2025
33f971e
update: Test invert for case inverts a single key-value pair in spri…
SuWebOnes Jul 21, 2025
fcb6576
update: Test invert for case inverts multiple key-value pairs in spr…
SuWebOnes Jul 21, 2025
3d8ba46
update: Test: duplicate values in sprint-2 interpret invert.test.js
SuWebOnes Jul 21, 2025
e63ca77
update: Test invalid values in sprint-2 interpret invert.test.js
SuWebOnes Jul 21, 2025
342794e
update: function including for valid and and invalid inputs in sprint…
SuWebOnes Jul 21, 2025
7a2d94d
sytx in en-us in sprint-2 branch
SuWebOnes Aug 8, 2025
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
2 changes: 1 addition & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);//address is an object, not an array
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)) {//accessing both key and value using se Object.values()
console.log(value);
}
2 changes: 1 addition & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`); // ingredients in new array
7 changes: 6 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}
function contains(obj, key) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}
return obj.hasOwnProperty(key);
}

module.exports = contains;
38 changes: 20 additions & 18 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
particular property

E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'

E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

// Acceptance criteria:

// Given a contains function
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

// 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({}, "key")).toBe(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("returns true for existing property", () => {
expect(contains({ name: "Alice", age: 30 }, "name")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("returns false for non-existent property", () => {
expect(contains({ name: "Alice", age: 30 }, "email")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("returns false for array instead of object", () => {
expect(contains(["a", "b", "c"], "a")).toBe(false);
});

test("returns false for null input", () => {
expect(contains(null, "key")).toBe(false);
});

test("returns false for undefined input", () => {
expect(contains(undefined, "key")).toBe(false);
});
8 changes: 6 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const result = {};
for (const [country, currency] of pairs) {
result[country] = currency;
}
return result;
}

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

test.todo("creates a country currency code lookup for multiple codes");

// Core Test: Normal input of multiple country-currency pairs
test("creates a country currency code lookup for multiple codes", () => {
const input = [['US', 'USD'], ['CA', 'CAD'], ['JP', 'JPY']];
const expected = {
US: 'USD',
CA: 'CAD',
JP: 'JPY',
};
expect(createLookup(input)).toEqual(expected);
});
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down Expand Up @@ -33,3 +41,13 @@ It should return:
'CA': 'CAD'
}
*/
// Edge Case 1: Empty array input
test("returns empty object when given an empty array", () => {
expect(createLookup([])).toEqual({});
});
// dge Case 2: Duplicate country codes (last value wins)
test("overwrites value if country code is duplicated", () => {
const input = [['US', 'USD'], ['US', 'US Dollars']];
const expected = { US: 'US Dollars' };
expect(createLookup(input)).toEqual(expected);
});
17 changes: 12 additions & 5 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
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("=");
queryParams[key] = value;
const indexOfEqual = pair.indexOf("=");

if (indexOfEqual === -1) {
// No '=' means key with no value
queryParams[decodeURIComponent(pair)] = "";
Copy link

Choose a reason for hiding this comment

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

Can you explain what decodeURIComponent method does?

Copy link
Author

Choose a reason for hiding this comment

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

It converts URL-encoded characters back into their original form

} else {
const key = decodeURIComponent(pair.slice(0, indexOfEqual));
const value = decodeURIComponent(pair.slice(indexOfEqual + 1));
queryParams[key] = value;
}
}

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

//Test: Handles URL-encoded characters (e.g. space encoded as %20)
test("decodes percent-encoded characters", () => {
expect(parseQueryString("note=hello%20world")).toEqual({
note: "hello world",
});
});

// Test: Key without a value (e.g. justKey)
// Should treat missing value as an empty string

test("parses key without value", () => {
expect(parseQueryString("justKey")).toEqual({
justKey: "",
});
});

// Test: Empty query string

test("returns empty object for empty query string", () => {
expect(parseQueryString("")).toEqual({});
});


// Test: Handles multiple pairs including one with no value

test("handles mixed keys with and without values", () => {
expect(parseQueryString("name=Alice&flag")).toEqual({
name: "Alice",
flag: "",
});
});
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array");
}

const counts = {};

for (const item of arr) {
counts[item] = (counts[item] || 0) + 1;
}

return counts;
}

module.exports = tally;
11 changes: 9 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,19 @@ 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 single item array", () => {
expect(tally(["a"])).toEqual({ a: 1 });
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws error on invalid input (non-array)", () => {
expect(() => tally("not-an-array")).toThrow("Input must be an array");
});
42 changes: 33 additions & 9 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,48 @@

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}

function invert(obj) {
const invertedObj = {};
// function invert(obj) {
// const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
}
// for (const [key, value] of Object.entries(obj)) {
// invertedObj.key = value;
// }

return invertedObj;
}
// return invertedObj;
// }

// a) What is the current return value when invert is called with { a : 1 }
//{ key: 1 } // because literally sets the property "key" instead of using the variable value as the new key.

// b) What is the current return value when invert is called with { a: 1, b: 2 }
//{ key: 2 } //because The second iteration of the loop overwrites the first one because it keeps writing to "key".

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

//{ "1": "a", "2": "b" } // because Each key and value from the input object should be swapped.
// Object.entries(obj) returns an array of [key, value] pairs from the object.
// c) What does Object.entries return? Why is it needed in this program?
// Object.entries({ a: 1, b: 2 })
// → [['a', 1], ['b', 2]] because

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

// The original code used invertedObj.key = value, which:
// Created a property literally named "key"
// Overwrote it in every loop
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
function invert(obj) {
// Validate that input is a non-null object and not an array
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
throw new Error("Input must be a non-null object");
}

const invertedObj = {};

// Loop over key-value pairs using Object.entries
for (const [key, value] of Object.entries(obj)) {
// Swap the key and value
invertedObj[String(value)] = key;
}

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

// Test: empty object returns empty object
test("returns empty object when given an empty object", () => {
expect(invert({})).toEqual({});
});
// Test: single key-value pair
test("inverts a single key-value pair", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

// Test: multiple key-value pairs
test("inverts multiple key-value pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});

// Test: duplicate values (last one wins)
test("overwrites key if values are not unique", () => {
expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" }); // 'b' overwrites 'a'
});

test("throws error if input is not an object", () => {
expect(() => invert("string")).toThrow("Input must be a non-null object");
});
Loading
Loading