West Midlands | 26-Jan-ITP | Fida Ali Zada | Sprint 2 | Data Group#985
West Midlands | 26-Jan-ITP | Fida Ali Zada | Sprint 2 | Data Group#985alizada-dev wants to merge 17 commits intoCodeYourFuture:mainfrom
Conversation
| // Then it should return false or throw an error | ||
|
|
||
| test("contains on invalid parameters returns false", () => { | ||
| expect(contains(["a", "b", "c"])).toStrictEqual(false); |
There was a problem hiding this comment.
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")
There was a problem hiding this comment.
This test cannot not yet confirm that the function correctly returns false when the first argument is an array.
This is because contains(["a", "b", "c"]) is the executed as contains("[a", "b", "c"], undefined), which could also return false simply because "undefined" is not a key of the array.
Arrays are objects, with their indices acting as keys. A proper test should use a valid
key to ensure the function returns false specifically because the input is an array, not because the key is missing.
Currently your function will return true in these two function calls.
contains(["a", "b", "c"], "1")contains("abc", "1")
There was a problem hiding this comment.
Now, I understand what you are telling, sir. I changed the function first:
`function contains(obj, x) {
if (
typeof obj !== "object" ||
obj === null ||
Array.isArray(obj)
) {
return false;
}
return Object.prototype.hasOwnProperty.call(obj, x);
}
module.exports = contains;`
And the test:
test("contains() handles arrays, strings, null, and undefined safely", () => { expect(contains([["a", "b", "c"], "1"])).toBe(false); expect(contains(["abc", "1"])).toBe(false); })
Please check if it is okay; otherwise, I will change.
| return Object.fromEntries(sortedArray); | ||
| } | ||
|
|
||
| console.log(countWords("you? and! me, and you. me me me me hello Me")); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thank you for pointing out the bugs. Now, this should work for all cases.
`function countWords(str) {
const words = str
.toLowerCase()
.replace(/[^a-zA-Z0-9\s]/g, '') // replace punctuation with space
.trim()
.split(/\s+/); // split on any white space
const countObj = Object.create(null);
for (const word of words) {
countObj[word] = (countObj[word] || 0) + 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);
}`
Sprint-2/stretch/till.js
Outdated
| // a) What is the target output when totalTill is called with the till object | ||
| // £NaN |
There was a problem hiding this comment.
I think the question is asking for the "expected output", not the actual output.
There was a problem hiding this comment.
// a) What is the target output when totalTill is called with the till object
// £4.40
|
I am sorry, sir, for responding late. I got a job because it was so stressful not working. |
|
The proposed changes look good. Have you applied the changes and pushed the changes to GitHub? I don't see any new commits on this PR branch. |
|
I had forgotten to push the changes. I just pushed them. |
|
Still nothing new on this branch. Did you push the changes on the right branch? |
|
Dear Sir, I am confused. I have committed and pushed all the changes to this PR branch on GitHub. If you check my latest commits, they are shown in the "Files changed" tab as well. |
|
Sorry. I noticed no changes were made to I have now marked the addressed comments as "Resolved conversation". Can you addressed the remaining three unresolved conversation? |
|
It is all right, sir. Thank you for your time and comments. I brought some changes. If the changes look good, I will push them to GitHub. |
|
The proposed changes look good. I will mark this PR as completed. Note: Sharing code as a "code block" in PR comment can make the code easier to read. |

Self checklist
Changelist
Completed the exercises in the Sprint-2 folder