Sheffield| 26-ITP-Jan| Mona-Eltantawy | Sprint 2 | Sprint 2 course-work tasks#1195
Sheffield| 26-ITP-Jan| Mona-Eltantawy | Sprint 2 | Sprint 2 course-work tasks#1195Mona-Eltantawy wants to merge 11 commits intoCodeYourFuture:mainfrom
Conversation
…at passed the npm try in treminal.
cjyuan
left a comment
There was a problem hiding this comment.
Some of the code is not consistently formatted.
Have you installed the prettier VSCode extension and enabled "Format on save/paste" on VSCode,
as recommended in
https://github.com/CodeYourFuture/Module-Structuring-and-Testing-Data/blob/main/readme.md
?
If you have enabled "Format on save" but it is not working, it is likely that you haven't assign a formatter for JS file. This could happen if you have zero or multiple extensions that can format .js file.
If you have installed "Prettier" extension. To assign it as the formatter of JS code, you can try:
- Use "Format document" to format the JS file. Sometimes, VSCode will ask you to choose a formatter, and you can manually select "Prettier".
- Edit
settings.jsonand set Prettier as the default formatter for JS.
See: https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode
| let text = " "; | ||
| for (let x in author) { | ||
| text+= author[x] + " "; | ||
| }; | ||
| console.log(text); | ||
|
|
||
| // or: | ||
| const values = Object.values(author); | ||
| console.log(values); |
There was a problem hiding this comment.
The original code attempts to output the value one by one on a new line.
Can you rewrite your code to also output all the property values one by one on a new line?
| function contains(obj, key) { | ||
| return key in obj; | ||
| } |
There was a problem hiding this comment.
Consider the following two approaches for determining if an object contains a property:
let obj = {}, propertyName = "toString";
console.log( propertyName in obj ); // true
console.log( Object.hasOwn(obj, propertyName) ); // false
Which of these approaches suits your needs better?
For more info, you can look up JS "in" operator vs Object.hasOwn.
There was a problem hiding this comment.
I understand your point her but the function used still give the required output, so I'm not sure if I have to change it to Object.hasOwn( ) method or to keep it.
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
| test ("contains with invalid parameters return false", () => { | ||
| const obj = ['a','b','c','d']; | ||
| expect(contains(obj , "a")).toBe (false); | ||
| }); |
There was a problem hiding this comment.
This test cannot yet confirm that the function correctly returns false when the first argument is an array.
This is because contains(['a','b','c','d'], "a") could also return false simply because "a" 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.
Besides array, you should also test other invalid parameters such as null, undefined, strings, to ensure your function works correctly.
| const result ={ }; | ||
| for (let item of items){ | ||
| result[item]=(result[item] || 0)+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, or
- use
Object.hasOwn()
Self checklist