-
-
Notifications
You must be signed in to change notification settings - Fork 96
Westmidlands/Millena-Mesfin/Module Data Groups Sprint#2/Week 10 #485
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 all commits
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,5 @@ | ||
function contains() {} | ||
function contains(obj,key) { | ||
return (key in obj); | ||
} | ||
Comment on lines
+1
to
+3
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. Consider the following two approaches for determining if an object contains a property:
Which of these approaches suits your needs better? 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 cj, I got to differentiate the (propertyName in obj) suits to my function. |
||
|
||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,16 +20,35 @@ 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 an empty object returns false", () => { | ||
const obj = {}; | ||
expect(contains(obj,"m")).toBe(false); | ||
}); | ||
|
||
// Given an object with properties | ||
// When passed to contains with an existing property name | ||
// Then it should return true | ||
test("contains an object with an existing property should return true", () => { | ||
const obj = {name:"Milli",surname:"Mesfin",city:"Birmingham"}; | ||
expect(contains(obj,'surname')).toBe(true); | ||
expect(contains(obj,'city')).toBe(true); | ||
}); | ||
|
||
// Given an object with properties | ||
// When passed to contains with a non-existent property name | ||
// Then it should return false | ||
test("contains an object with non-existent property name should return false", () => { | ||
|
||
const obj = {name:"Milli",surname:"Mesfin", city:"Birmingham"} | ||
expect(contains(obj,'age')).toBe(false); | ||
expect(contains(obj,'address')).toBe(false); | ||
}); | ||
|
||
// Given invalid parameters like an array | ||
// When passed to contains | ||
// Then it should return false or throw an error | ||
test("contains an invalid parameter like array should return false" ,() => { | ||
const obj = ["code","your","future"]; | ||
expect(contains(obj,'age')).toBe(false); | ||
expect(contains(obj,'address')).toBe(false); | ||
}); | ||
Comment on lines
+50
to
+54
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. An array is a kind of object in JS. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
function createLookup() { | ||
// implementation here | ||
} | ||
function createLookup(countryCurrencyPairs) { | ||
|
||
const obj = Object.fromEntries(countryCurrencyPairs); | ||
|
||
return obj ; | ||
}; | ||
|
||
module.exports = createLookup; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,16 @@ | ||
function tally() {} | ||
function tally(arr) { | ||
if (!Array.isArray(arr)) { | ||
//this line checks if arr is not an array, if it's not an array it will throw error. | ||
throw new Error("not an array"); | ||
} | ||
|
||
const count = {}; | ||
for(const item of arr){ | ||
count[item]=(count[item]||0)+1; // this line is where we update the count of each item in the count object. | ||
//count[item]=> is checking if already exists as a key in the count object. | ||
// ||0 => this changes the undefined count to zero if the item have not seen in the count object and then we add 1 . | ||
} | ||
return count; | ||
} | ||
|
||
module.exports = tally; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,12 +23,29 @@ const tally = require("./tally.js"); | |
// Given an empty array | ||
// When passed to tally | ||
// Then it should return an empty object | ||
test.todo("tally on an empty array returns an empty object"); | ||
test("tally on an empty array returns an empty object", () => { | ||
const count = []; | ||
expect(tally([])).toEqual({}) | ||
}); | ||
|
||
// Given an array with duplicate items | ||
// When passed to tally | ||
// Then it should return counts for each unique item | ||
test("An array with duplicate items should return counts for each unique item", () => { | ||
const count = ["h", "e", "l", "l", "o",]; | ||
const expectedOutput = {h: 1, e: 1, l: 2, o: 1 }; | ||
|
||
expect(tally(count)).toEqual(expectedOutput); | ||
}); | ||
|
||
// Given an invalid input like a string | ||
// When passed to tally | ||
// Then it should throw an error | ||
test("An array with invalid input should throw an error", () => { | ||
const count = ["hello", "hey", "hey", "hello", "hello", "hi"]; | ||
const expectedOutput = {hello: 3, hey: 2, hi: 1 }; | ||
|
||
expect(tally(count)).toEqual(expectedOutput); | ||
}); | ||
Comment on lines
+44
to
+49
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. The description and the code does not match. |
||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you modify this to output the property values instead of property names?