-
-
Notifications
You must be signed in to change notification settings - Fork 283
West Midlands | 26-Jan-ITP | Fida Ali Zada | Sprint 2 | Data Group #985
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 8 commits
b4cca09
5187f64
67cf526
28a76d8
b2973a6
983f892
00e4862
6e5c048
629a5b3
8efa5ba
06759cb
4dadf43
f5c06bd
57a602e
25c9a48
5ae851f
337f88f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,13 @@ | ||
| function contains() {} | ||
| function contains(obj, x) { | ||
| for (const key in obj) { | ||
| if (Object.hasOwn(obj, key) && key == x) { | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| module.exports = contains; | ||
|
|
||
| // Object.hasOwn(obj, key) | ||
| // This ensures the property belongs directly to the object, not its prototype. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,16 +20,38 @@ 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({})).toStrictEqual(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 an existing property name returns true", () => { | ||
| expect(contains({a: 1, b: 2, c: 3}, 'c')).toStrictEqual(true); | ||
| expect(contains({name: "John Doe", job: "developer"}, 'job')).toStrictEqual(true) | ||
| expect(contains({color: "blue", size: "xxl", madeOf: "China"}, 'madeOf')).toStrictEqual(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 properties with non-existent property name return false", () => { | ||
| expect(contains({a: 1, b: 2, c: 3}, 'g')).toStrictEqual(false); | ||
| expect(contains({a: 1, b: 2, c: 3}, 'abc')).toStrictEqual(false); | ||
| expect(contains({name: "John Doe", age: 31}, 'job')).toStrictEqual(false); | ||
| }) | ||
|
|
||
| // Given invalid parameters like an array | ||
| // When passed to contains | ||
| // Then it should return false or throw an error | ||
|
|
||
| test("contains on invalid parameters returns false", () => { | ||
| expect(contains(["a", "b", "c"])).toStrictEqual(false); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does your function return the value you expect from the following function calls?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, it handles correctly.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test cannot not yet confirm that the function correctly returns false when the first argument is an array. Arrays are objects, with their indices acting as keys. A proper test should use a valid Currently your function will return
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now, I understand what you are telling, sir. I changed the function first: } module.exports = contains;` And the test: Please check if it is okay; otherwise, I will change. |
||
| expect(contains("I am a string")).toStrictEqual(false); | ||
| expect(contains(777)).toStrictEqual(false); | ||
| }) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| function createLookup() { | ||
| // implementation here | ||
| function createLookup(arr) { | ||
| return Object.fromEntries(arr); | ||
| } | ||
|
|
||
| module.exports = createLookup; | ||
| module.exports = createLookup; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,26 @@ | ||
| function tally() {} | ||
| function tally(arr) { | ||
| if (Array.isArray(arr)) { | ||
|
|
||
| let countObj = {}; | ||
|
|
||
| for (const item of arr) { | ||
|
|
||
| if (countObj[item]) { | ||
| countObj[item] = countObj[item] + 1; | ||
| } else { | ||
| countObj[item] = 1; | ||
| } | ||
| } | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
|
|
||
| return countObj; | ||
| } | ||
|
|
||
| else throw new Error(""); | ||
|
cjyuan marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| module.exports = tally; | ||
|
|
||
| // console.log(tally(['a', 'a', 'a', 'a', 'a', 'b', 'c'])); | ||
|
|
||
|
|
||
| // tally(['a', 'a', 'a', 'a', 'a', 'b', 'c']), target output: { a : 5, b: 1, c: 1 } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,3 +26,26 @@ | |
|
|
||
| 3. Order the results to find out which word is the most common in the input | ||
| */ | ||
|
|
||
| function countWords(str) { | ||
| const arr = str.replace(/[^a-zA-Z0-9\s]/g, '').toLowerCase().split(" ") | ||
|
|
||
| let countObj = {}; | ||
|
|
||
| for (const item of arr) { | ||
|
|
||
| if (countObj[item]) { | ||
| countObj[item] = countObj[item] + 1 | ||
| } else { | ||
| countObj[item] = 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); | ||
| } | ||
|
|
||
| console.log(countWords("you? and! me, and you. me me me me hello Me")); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does your function return what you expect in the following function calls? Note: The spec is not clear about exactly what to expect from these function calls. This is just for self-check.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for pointing out the bugs. Now, this should work for all cases. `function countWords(str) { const countObj = Object.create(null); for (const word of words) { // // |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,10 +8,10 @@ function totalTill(till) { | |
| let total = 0; | ||
|
|
||
| for (const [coin, quantity] of Object.entries(till)) { | ||
| total += coin * quantity; | ||
| total += Number(coin.slice(0, -1)) * Number(quantity); | ||
| } | ||
|
|
||
| return `£${total / 100}`; | ||
| return `£${(total / 100).toFixed(2)}`; | ||
| } | ||
|
|
||
| const till = { | ||
|
|
@@ -22,10 +22,15 @@ const till = { | |
| }; | ||
| const totalAmount = totalTill(till); | ||
|
|
||
| module.exports = totalTill; | ||
|
|
||
| // a) What is the target output when totalTill is called with the till object | ||
| // £NaN | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the question is asking for the "expected output", not the actual output.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. // a) What is the target output when totalTill is called with the till object |
||
|
|
||
| // b) Why do we need to use Object.entries inside the for...of loop in this function? | ||
| // to convert the given object into array | ||
|
|
||
| // c) What does coin * quantity evaluate to inside the for...of loop? | ||
| // it converts different coins with different values into single pense | ||
|
|
||
| // d) Write a test for this function to check it works and then fix the implementation of totalTill | ||
Uh oh!
There was an error while loading. Please reload this page.