-
-
Notifications
You must be signed in to change notification settings - Fork 94
ITP_GLASGOW_APR | HANNA_MYKYTIUK | MODULE__DATA_\GROUPS | SPRINT_2 #492
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,11 @@ | ||
function contains() {} | ||
function contains(obj, property) { | ||
try { | ||
return Object.hasOwn(obj, property); | ||
} | ||
catch(e ){ | ||
console.log(e); | ||
return false; | ||
} | ||
} | ||
|
||
module.exports = contains; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,23 +13,39 @@ as the object doesn't contains a key of 'c' | |
|
||
// Acceptance criteria: | ||
|
||
// Given a contains function | ||
// When passed an object and a property name | ||
// Then it should return true if the object contains the property, false otherwise | ||
describe("contains", () => { | ||
// Given a contains function | ||
// When passed an object and a property name | ||
// Then it should return true if the object contains the property, false otherwise | ||
it("check if object has property", () => { | ||
expect(contains({name: 'Hanna'}, 'name')).toEqual(true); | ||
expect(contains({name: 'Hanna'}, 'surname')).toEqual(false); | ||
}); | ||
// Given an empty object | ||
// When passed to contains | ||
// Then it should return false | ||
it("if object is empty it should return false", () => { | ||
expect(contains({}, 'name')).toEqual(false); | ||
}); | ||
// Given an object with properties | ||
// When passed to contains with an existing property name | ||
// Then it should return true | ||
it("if object has multiple properties and passed existing property name it should return true", () => { | ||
expect(contains({property1: 'exist1', property2: 'exist2'}, 'property1')).toEqual(true); | ||
}); | ||
// Given an object with properties | ||
// When passed to contains with a non-existent property name | ||
// Then it should return false | ||
it("if object has multiple properties and passed non-existing property name it should return false", () => { | ||
expect(contains({property1: 'exist1', property2: 'exist2'}, 'property3')).toEqual(false); | ||
}); | ||
// Given invalid parameters like an array | ||
// When passed to contains | ||
// Then it should return false or throw an error | ||
it("in case of invalid parametr function contains should throw an error", () => { | ||
expect(contains( [1,2,3], 'property')).toEqual(false); | ||
expect(contains( "test", 'property')).toEqual(false); | ||
}); | ||
Comment on lines
+45
to
+48
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. 1st Issue: Array is a kind of objects in JavaScript where its indexes are treated as key. So If you want to reject any type of value that is not an object, you will need to explicitly write code to check. (You can try asking ChatGPT how to properly check if a value is an object in JS) 2nd Issue:
|
||
}); | ||
|
||
// Given an empty object | ||
// When passed to contains | ||
// Then it should return false | ||
test.todo("contains on empty object returns false"); | ||
|
||
// Given an object with properties | ||
// When passed to contains with an existing property name | ||
// Then it should return true | ||
|
||
// Given an object with properties | ||
// When passed to contains with a non-existent property name | ||
// Then it should return false | ||
|
||
// Given invalid parameters like an array | ||
// When passed to contains | ||
// Then it should return false or throw an error |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,10 @@ | ||
function createLookup() { | ||
// implementation here | ||
function createLookup(complexArray) { | ||
let object ={}; | ||
for (let i=0; i<complexArray.length; i++){ | ||
object[complexArray[i][0]] = complexArray[i][1]; | ||
} | ||
return object; | ||
|
||
} | ||
|
||
module.exports = createLookup; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,11 +6,19 @@ function parseQueryString(queryString) { | |
const keyValuePairs = queryString.split("&"); | ||
|
||
for (const pair of keyValuePairs) { | ||
const [key, value] = pair.split("="); | ||
queryParams[key] = value; | ||
//console.log(pair.split("=")); | ||
const array = pair.split("="); | ||
let value = ''; | ||
for(let i = 1; i<array.length; i++){ | ||
value += array[i]; | ||
if (i != array.length - 1){ | ||
value += '='; | ||
} | ||
} | ||
Comment on lines
+11
to
+17
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. You can also write this as |
||
queryParams[array[0]] = value; | ||
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. Please note that in real querystring, both |
||
} | ||
|
||
return queryParams; | ||
} | ||
console.log(parseQueryString("equation=x=y+1")); | ||
|
||
module.exports = parseQueryString; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,31 @@ | ||
function tally() {} | ||
function tally(array) { | ||
try { | ||
if (typeof(array) == 'string'){ | ||
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. There is a way to check if a value is an array. (Ask ChatGPT) |
||
console.log('String is invalid input'); | ||
return false; | ||
} | ||
let copy = []; | ||
for (let i=0; i<array.length; i++){ | ||
if (copy.indexOf(array[i]) === -1) { | ||
copy.push(array[i]); | ||
} | ||
} | ||
let object = {}; | ||
for (let i=0; i < copy.length; i++){ | ||
let count = 0; | ||
for (let j=0; j < array.length; j++){ | ||
if (array[j] == copy[i]){ | ||
count = count + 1; | ||
} | ||
} | ||
object[copy[i]] = count; | ||
} | ||
Comment on lines
+7
to
+22
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. Would you try implement the following approach (which is more efficient by using object as a lookup table):
|
||
return object; | ||
} catch(e){ | ||
console.log(e); | ||
return false; | ||
} | ||
|
||
} | ||
|
||
module.exports = tally; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,16 +19,30 @@ const tally = require("./tally.js"); | |
// Given a function called tally | ||
// When passed an array of items | ||
// Then it should return an object containing the count for each unique item | ||
it("it should return an object containing the count for each unique item", () => { | ||
expect(tally(['a'])).toEqual({ a: 1 }); | ||
expect(tally(['a', 'a', 'a'])).toEqual({ a: 3}); | ||
expect(tally(['a', 'a', 'b', 'c'])).toEqual({ a : 2, b: 1, c: 1 }); | ||
}); | ||
|
||
// 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"); | ||
it("empty array should return an empty object", () => { | ||
expect(tally(['a'])).toEqual({ a: 1 }); | ||
}); | ||
|
||
// Given an array with duplicate items | ||
// When passed to tally | ||
// Then it should return counts for each unique item | ||
it("array with duplicate items should return an object containing the count for each unique item", () => { | ||
expect(tally(['a', 'a', 'b', 'b', 'c', 'c'])).toEqual({ a : 2, b: 2, c: 2 }); | ||
}); | ||
|
||
|
||
// Given an invalid input like a string | ||
// When passed to tally | ||
// Then it should throw an error | ||
it("Given an invalid input it should throw an error", () => { | ||
expect(tally('string')).toEqual(false); | ||
}); | ||
Comment on lines
+46
to
+48
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. Your function is not throwing any error when the parameter is not an array (the description of the test is not quite correct). |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const invert = require("./invert.js"); | ||
|
||
it("it should return an object with reverce key and values", () => { | ||
expect(invert({ a : 1 })).toEqual({ '1': 'a' }); | ||
expect(invert({ a: 1, b: 2 })).toEqual({ '1': 'a', '2': 'b' }); | ||
}); |
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.
Please remove unrelated files when you request a code review.