Skip to content
Open
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
96e54cd
Fixed the implementaton of the function CalculateMedian and passed a…
ahmadehsas Jul 28, 2025
d577680
Implemented the deduplicate function with given arrays and passed th…
ahmadehsas Jul 28, 2025
3ecbe9f
Implemented the function findMax and passed the tests for given arrays.
ahmadehsas Jul 29, 2025
0467d03
Implemented the function sum that sums the numerical elements of an a…
ahmadehsas Jul 29, 2025
a9a5914
Refactored the implementation of Includes function and passed the giv…
ahmadehsas Jul 29, 2025
d5ca4ab
Fixed the code address object and used dot notation to access the hou…
ahmadehsas Aug 5, 2025
ce7733e
Explained and fixed the problem in the mention code.
ahmadehsas Aug 6, 2025
73e8544
Fixed the mention code and printed out each ingredient in a new line …
ahmadehsas Aug 6, 2025
f512318
Implemented the function Contain, and passed tests for this function.
ahmadehsas Aug 6, 2025
22c4094
Implemented the createLookup function and passed the test.
ahmadehsas Aug 7, 2025
1231d9c
Fixed the implementation for this test and wrote tests.
ahmadehsas Aug 8, 2025
aa42568
Implemented the function called tally and passed tests.
ahmadehsas Aug 8, 2025
ef820ad
Fixed the implementation of invert function and wrote tests.
ahmadehsas Aug 9, 2025
898abfc
passed the test for contains of an array of length returns true.
ahmadehsas Aug 13, 2025
2bc209b
Fixed the code in one line with prettier-ignore.
ahmadehsas Aug 14, 2025
d0788b8
fixed the test for given an array as input return false.
ahmadehsas Aug 15, 2025
9940e55
Restore Sprint-1 folder to match main branch.
ahmadehsas Nov 12, 2025
ae98aa1
Fixed Sprint-1.
ahmadehsas Nov 21, 2025
268774e
Moved prep folder from this branch.
ahmadehsas Nov 21, 2025
9c2f2f8
checked if the code is working without if... statement.
ahmadehsas Nov 21, 2025
1f7a23d
changed the regax + to * to work for a zero characters.
ahmadehsas Nov 22, 2025
fa7c649
created an object with no prototype.
ahmadehsas Nov 22, 2025
e165bfe
modified the answers of question 'a' and 'b'.
ahmadehsas Nov 23, 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
6 changes: 5 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// The bellow code should be in array format.

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +13,7 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
//console.log(`My house number is ${address[0]}`);

// Here we use dot notation to access the houseNumber property
console.log(`My house number is ${address.houseNumber}`);
16 changes: 14 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// In this code, we are trying to iterate over the values of an object using " for ... of loop".
// However, the `for ... of` loop is designed to iterate over iterable objects like arrays, not objects.

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

for (const value of author) {
console.log(value);
// for this code we use `for ... in` loop method whenever we want to loop though an object.
// The `for ... in` loop iterates through properties in the prototype chain.
// This means that we need to check if the property belongs to the object using hasownproperty whenever we loop through an object with the `for ... in` loop.

for (const key in author) {
if (author.hasOwnProperty(key)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why introduce the if-statement on line 21? Can you justify its need?

Copy link
Author

Choose a reason for hiding this comment

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

When I tested my code without the 'if ... statement', it worked, but the if ... statement is neccessary if there might be properties inherited from the prototype chain.
So the 'if' check doesn't change the output.

Copy link
Contributor

Choose a reason for hiding this comment

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

  • You can output the value of key within the loop to see what values you get
  • You can also use AI to help you clarify some concept

console.log(`${key}: ${author[key]}`);
}
}

//for (const value of author) {
// console.log(value);
//}
14 changes: 11 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// we should add new line `\n` between ingredients to log them on a new line.

// 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 @@ -10,6 +11,13 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
// Here we add `join("\n") to the ingredients array to log each ingredient in a new line.
console.log(
`${recipe.title} serves ${
recipe.serves
} ingredients: ${recipe.ingredients.join("\n")}`
);

//console.log(`${recipe.title} serves ${recipe.serves}
// ingredients:
//${recipe}`);
15 changes: 14 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function contains() {}
function contains(obj, prop) {
return obj.hasOwnProperty(prop);
}

// Or we can use `for ...in loop to check for the property
// function contains(obj, prop) {
// for (const key in obj){
// if (key === prop) return true;
// }
// return false;
// }

console.log(contains({ a: 1, b: 2 }, "a")); // true
console.log(contains({ a: 1, b: 2 }, "c")); // false

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("contains on empty object returns false", () => {
expect(contains({}, "a")).toBe(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 existing property returns true", () => {
expect(contains({ a: 1, b: 2 }, "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 on object with non-existent property returns false", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains on an array returns false", () => {
expect(contains([1, 2, 3], 4)).toBe(false);
});

test("contains on an array of length returns true", () => {
expect(contains([1, 2, 3, 4], "length")).toBe(true);
}); // the test returns true because 'length' is a property of the array object

test("contains given on array as input return false", () => {
expect(contains([1, 2, 3], "a")).toBe(false);
});
Comment on lines 44 to 56
Copy link
Contributor

Choose a reason for hiding this comment

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

Do these function calls return what you expect?

  contains([1, 2, 3], "0");
  contains(null, "a");
  contains(undefined, "a");
  contains(1234, "a");

Copy link
Author

Choose a reason for hiding this comment

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

My function checks for its own property on an object. For the first input `contains([1, 2, 3], "0"); return true because arrays are objects and "0" is a property key.
Inputs like 'null', 'undefined', and '123' will cause errors because they are not objects.

Copy link
Contributor

Choose a reason for hiding this comment

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

In that case you may want to update the test descriptions on lines 44 and 52.

15 changes: 13 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
function createLookup() {
// implementation here
function createLookup(codePairs) {
const lookup = codePairs.reduce((acc, [key, value]) => {
acc[key] = value;
return acc;
}, {});
return lookup;
}

console.log(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
);

module.exports = createLookup;
14 changes: 13 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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 expectedOutput = {
US: "USD",
CA: "CAD",
};

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

/*

Expand Down
2 changes: 1 addition & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const [key, value] = pair.split(/=(.+)/); // We use regex to split only at first.
queryParams[key] = value;
}
Comment on lines 7 to 13
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this function call return what you expect?

  parseQuerySring("A=");  

In real query string, both key and value are percent-encoded or URL encoded.
For example,

tags%5B%5D=hello%20world -> key is tags[], value is hello world

Can your function handle URL-encoded query string?

Suggestion: Look up "How to decode a URL-encoded string in JavaScript".


Expand Down
17 changes: 14 additions & 3 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,21 @@
// Below is one test case for an edge case the implementation doesn't handle well.
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.

const parseQueryString = require("./querystring.js")
const parseQueryString = require("./querystring.js");

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
expect(parseQueryString("equation=x=y+1")).toStrictEqual({
equation: "x=y+1",
});
});

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

test("parses multiple parameters", () => {
expect(parseQueryString("name=John&age=30")).toStrictEqual({
name: "John",
age: "30",
});
});
18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function tally() {}
function tally(array) {
if (!Array.isArray(array)) {
throw new Error("Input must be an array");
// check if the input ia an array. if not, throw an error.
}
const tally = {};
Copy link
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.


for (const item of array) {
tally[item] = (tally[item] || 0) + 1;
// If the item already exists in tally, increase its count by 1.
// If it doesn't exist, start from 1.
}

return tally;
}

console.log(tally(["a", "a", "a"]));

module.exports = tally;
13 changes: 12 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,27 @@ 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 a single item returns count of 1", () => {
expect(tally(["a"])).toEqual({ a: 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", () => {
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 duplicates returns correct counts", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally on a string throws an error", () => {
expect(() => tally("a")).toThrow("Input must be an array");
});
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;
}
console.log(invert({ a: 1 })); // {1: "a"}
console.log(invert({ a: 1, b: 2 })); // {1: "a", 2: "b"}
module.exports = invert;

// a) What is the current return value when invert is called with { a : 1 }
// The current return value is {1: "a"}

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// The current return value is {1: "a", 2: "b"}
Copy link
Contributor

Choose a reason for hiding this comment

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

The "current return value" refers to the value returned by the original (unmodified) function.

With the original function, the objects you described on line 23 and 26 are not the objects returned by the function.

Can you find out the actual object returned by the original function?


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

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries takes an object and return an array of key-value pairs.
// it needed because it converts the object into an array of [key, value] pairs which makes it easy to loop over both key and value at the same time.

// d) Explain why the current return value is different from the target output
// because it returns the [key, value] of an object in reverse order [value,key]

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
14 changes: 14 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const invert = require("./invert.js");

test("inverts an object with unique keys", () => {
expect(invert({ a: 1, b: 2 })).toStrictEqual({ 1: "a", 2: "b" });
});

test("inverts an object with string keys", () => {
// prettier-ignore
expect(invert({ cat: "meow", dog: "bark" })).toStrictEqual({ meow: "cat", bark: "dog"});
});

test("inverts an empty object", () => {
expect(invert({})).toEqual({});
});
Loading