-
-
Notifications
You must be signed in to change notification settings - Fork 155
ZA Cape Town | ITP-May-2025 | Dawud Vermeulen | Sprint 2 | Module-Data-Groups #755
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,10 @@ | ||
function contains() {} | ||
function contains(obj, property) { | ||
|
||
module.exports = contains; | ||
if(obj === null || obj === undefined){ | ||
return false | ||
} | ||
return obj[property] !== undefined; | ||
} | ||
|
||
module.exports = contains; | ||
// work done. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,12 @@ | ||
function createLookup() { | ||
// implementation here | ||
function createLookup(pairs) { | ||
const lookup = {}; | ||
for (const pair of pairs){ | ||
if(pair && pair.length === 2){ | ||
lookup[pair[0]] = pair[1]; | ||
} | ||
} | ||
return lookup; | ||
} | ||
|
||
module.exports = createLookup; | ||
// in working order |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,20 @@ | ||
function parseQueryString(queryString) { | ||
const queryParams = {}; | ||
if (queryString.length === 0) { | ||
if (queryString === "") { //removed the .length method | ||
return queryParams; | ||
} | ||
const keyValuePairs = queryString.split("&"); | ||
|
||
for (const pair of keyValuePairs) { | ||
const [key, value] = pair.split("="); | ||
//const [key, value] = pair.split("="); //this line is making kak and its so frustrating cause it assigns value ='x' (test case 1) and leaves the rest of the equation. | ||
const parts = pair.split("="); | ||
const key = parts[0]; | ||
const value = parts.length > 1 ? parts.slice(1).join('=') : ''; //clunky but it works to split them and stitch it together again | ||
queryParams[key] = value; | ||
Comment on lines
+11
to
13
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; | ||
} | ||
|
||
module.exports = parseQueryString; | ||
module.exports = parseQueryString; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
function tally() {} | ||
function tally(items) { | ||
if (!Array.isArray(items)){ | ||
throw new TypeError('that aint no array bay bay'); //okay so this is the biggie smalls and its working | ||
} | ||
const counts = {}; | ||
|
||
for (const item of items){ | ||
counts[item] = (counts[item] || 0) +1; //simple sexy core logic | ||
} | ||
return counts; | ||
} | ||
Comment on lines
+1
to
+11
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 the following function call returns the value you expect?
Suggestion: Look up an approach to create an empty object with no inherited properties. |
||
|
||
module.exports = tally; | ||
//no updates. works well |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,20 +10,30 @@ function invert(obj) { | |
const invertedObj = {}; | ||
|
||
for (const [key, value] of Object.entries(obj)) { | ||
invertedObj.key = value; | ||
const numKey = Number(key); | ||
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. We don't have to convert the key to a number when it looks like a number. |
||
|
||
if (!isNaN(numKey) && String(numKey) === key) { | ||
invertedObj[value] = numKey; | ||
} else { | ||
invertedObj[value] = key; //convert key to number if it is a number | ||
//how do i convert it to a number if it is a number? | ||
//if the key is a number, convert it to a number, otherwise keep it as a string | ||
} | ||
} | ||
|
||
return invertedObj; | ||
} | ||
module.exports = invert; | ||
|
||
// a) What is the current return value when invert is called with { a : 1 } | ||
|
||
// b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
|
||
// a) What is the current return value when invert is called with { a : 1 } | ||
// {'1':'a'} | ||
// b) What is the current return value when invert is called with { a: 1, b: 2 } | ||
// { '1': 'a', '2': 'b'} | ||
Comment on lines
+29
to
+31
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. These are the expected return value from the original code, not the actual return value. |
||
// c) What is the target return value when invert is called with {a : 1, b: 2} | ||
|
||
// {1 : a, 2 : b } | ||
// c) What does Object.entries return? Why is it needed in this program? | ||
|
||
// its a method that takes a obj and returns the [key, value] pairs. | ||
// d) Explain why the current return value is different from the target output | ||
|
||
// cause its converted to strings, the key must be a int and the value must be a string. | ||
Comment on lines
36
to
+37
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 question is asking, why with the original code, Note: The property name (key) is always a string. Even when we express the key as a number (e.g., |
||
// e) Fix the implementation of invert (and write tests to prove it's fixed!) | ||
// in file invert.test.js |
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.
What do you expect the from the following function calls?
Suggestion:, Look up
JS "in" operator vs Object.hasOwn
.