Skip to content

London | 26-ITP-Jan | Mohsen Zamani | sprint 2 | coursework#953

Open
mohsenzamanist wants to merge 1 commit intoCodeYourFuture:mainfrom
mohsenzamanist:feature/sprint-2
Open

London | 26-ITP-Jan | Mohsen Zamani | sprint 2 | coursework#953
mohsenzamanist wants to merge 1 commit intoCodeYourFuture:mainfrom
mohsenzamanist:feature/sprint-2

Conversation

@mohsenzamanist
Copy link

Learners, PR Template

Self checklist

  • I have titled my PR with Region | Cohort | FirstName LastName | Sprint | Assignment Title
  • My changes meet the requirements of the task
  • I have tested my changes
  • My changes follow the style guide

Changelist

Complete Sprint 2 exercises.

Questions

@mohsenzamanist mohsenzamanist added the Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. label Feb 10, 2026
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}\ningredients:`);
recipe.ingredients.forEach((ingredient) => console.log(ingredient));
Copy link
Contributor

Choose a reason for hiding this comment

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

Could also explore using Array.prototype.join() to produce the desired string .

Comment on lines +3 to +4
const keys = Object.keys(obj);
return keys.includes(property);
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.

Suggestion: Look up these two alternatives and find out their differences
JS "in" operator vs Object.hasOwn

// When passed to contains
// Then it should return false or throw an error
it("contains returns false or throws an error if given parameter is not a valid object", () => {
expect(contains([], "key1")).toEqual(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

Array is a kind of object where indexes serve as its keys.

With your current implementation,
contains([], "key1") returns false because "key1" is not a key of the empty array.
However, contains(["A", "B"], "1") would return true.

Comment on lines +45 to +50
expect(() => contains(undefined, "key1")).toThrow(
"The parameter given is not a plain JS object."
);
expect(() => contains(null, "key1")).toThrow(
"The parameter given is not a plain JS object."
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Number is not a plain JS object but it is given a different treatment.

Wouldn't it be "friendlier to the caller" if the function can be designed to behave consistently for any value that is not an object or is an array?

// Then it should return false or throw an error
it("contains returns false or throws an error if given parameter is not a valid object", () => {
expect(contains([], "key1")).toEqual(false);
expect(contains("key1:value1", "key1")).toEqual(false);
Copy link
Contributor

@cjyuan cjyuan Feb 20, 2026

Choose a reason for hiding this comment

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

With your current implementation, this test (with the 2nd argument changed to "1") would fail even though the first argument is a string.

  expect(contains("key1:value1", "1")).toEqual(false);

Comment on lines +21 to +23
const existingKeys = Object.keys(queryParams);
if (key === "" && value === "") continue;
if (existingKeys.includes(key)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We could just check queryParams[key] directly. This way, we don't have to keep creating an array of keys.

Copy link
Contributor

Choose a reason for hiding this comment

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

Please note that in real querystring, both key and value are percent-encoded or URL encoded in the URL. For example, the string "5%" will be encoded as "5%25". So to get the actual value of "5%25" (whether it is a key or value in the querystring), you should call a function to decode it.
May I suggest looking up any of these terms, and "How to decode URL encoded string in JS"?

Comment on lines +1 to +7
function tally(list) {
if (!Array.isArray(list)) throw new Error("Not an array.");
return list.reduce((acc, curr) => {
acc[curr] = (acc[curr] || 0) + 1;
return acc;
}, {});
}
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.

Comment on lines 27 to +28
// c) What is the target return value when invert is called with {a : 1, b: 2}
// { key: 2 }
Copy link
Contributor

Choose a reason for hiding this comment

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

"Target return value" refers to the expected return value (when the function is correctly implemented).

Comment on lines +30 to +38
function countWords(string) {
const noPunctuationStr = string.replace(/[.,!?]/g, "");
const wordArray = noPunctuationStr.split(" ");
let wordCount = new Map();
for (let word of wordArray) {
wordCount.set(word, (wordCount.get(word) || 0) + 1);
}
const sortedWordCount = [...wordCount.entries()].sort((a, b) => b[1] - a[1]);
return sortedWordCount;
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 check if your function returns what you expect in the following function calls?

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

@cjyuan cjyuan added Reviewed Volunteer to add when completing a review with trainee action still to take. and removed Needs Review Trainee to add when requesting review. PRs without this label will not be reviewed. labels Feb 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Reviewed Volunteer to add when completing a review with trainee action still to take.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments