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 14 commits intoCodeYourFuture:mainfrom
Conversation
Sprint-2/debug/author.js
Outdated
| }; | ||
|
|
||
| for (const value of author) { | ||
| for (const [key, value] of Object.entries(author)) { |
There was a problem hiding this comment.
Since this loop does not need key, you could consider using the method that returns only an array of property values.
There was a problem hiding this comment.
for (const value of Object.values(author)) { console.log(value); }
Sprint-2/implement/contains.js
Outdated
| for (const key in obj) { | ||
| if (Object.hasOwn(obj, key) && key == x) { |
There was a problem hiding this comment.
Why check if key is in the same object the loop is iterating though?
There was a problem hiding this comment.
for (const key in obj) { if (key == x) { return true } }
| // 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")
| const value = pair.substring(index + 1); | ||
|
|
||
| queryParams[key] = value; | ||
| } |
There was a problem hiding this comment.
For each of the following function calls, does your function return the value you expect?
parseQueryString("a=b&=&c=d")
parseQueryString("a=")
parseQueryString("=b")
parseQueryString("a=b&&c=d")
parseQueryString("a%25b=c%26d")
Note:
-
In real query string, both
keyandvalueare percent-encoded or URL encoded in the URL.
For example, the string "5%" is encoded as "5%25". So to get the actual value of "5%25"
(whether it is a key or value in the query string), we need to call a function to decode it. -
You can also explore the
URLSearchParamsAPI.
There was a problem hiding this comment.
`function parseQueryString(queryString) {
const queryParams = {};
if (!queryString) return queryParams;
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
// skip empty pairs (e.g. &&)
if (pair === "") continue;
const index = pair.indexOf("=");
let key, value;
if (key === -1) {
key = pair;
value = "";
} else {
key = pair.substring(0, index);
value = pair.substring(index + 1);
}
// decoding URL
try {
key = decodeURIComponent(key);
value = decodeURIComponent(value);
} catch (error) {
}
queryParams[key] = value;
}
return queryParams;
}`
Sprint-2/implement/tally.js
Outdated
| return countObj; | ||
| } | ||
|
|
||
| else throw new Error(""); |
There was a problem hiding this comment.
Can you give a more descriptive error message?
There was a problem hiding this comment.
else throw new Error("The input should be an array!");
Sprint-2/implement/tally.js
Outdated
| let countObj = {}; | ||
|
|
||
| for (const item of arr) { | ||
|
|
||
| if (countObj[item]) { | ||
| countObj[item] = countObj[item] + 1; | ||
| } else { | ||
| countObj[item] = 1; | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
`function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("The input should be an array!");
}
const countObj = Object.create(null);
for (const item of arr) {
if (countObj[item]) {
countObj[item] = countObj[item] + 1;
} else {
countObj[item] = 1;
}
}
return countObj;
}`
Sprint-2/interpret/invert.js
Outdated
| // c) What is the target return value when invert is called with {a : 1, b: 2} | ||
| // { key: 2 } |
There was a problem hiding this comment.
The "target value" refers to the expected value when the function is working correctly.
| 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.
| // 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.
|
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? |
Self checklist
Changelist
Completed the exercises in the Sprint-2 folder