Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Predict and explain first...

// This code should log out the houseNumber from the address object
// but it isn't working...
// but it isn't working..
// Fix anything that isn't working

const address = {
Expand All @@ -12,4 +12,6 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
console.log(`My house number is ${address['houseNumber']}`);

7 changes: 3 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
// Predict and explain first...

// Predict and explain first..
// This program attempts to log out all the property values in the object.
// But it isn't working. Explain why first and then fix the problem

Expand All @@ -11,6 +10,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) { //for-of is for arrays not objects so use Object.values
console.log(value);
}
}
5 changes: 2 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// Predict and explain first...

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
// How can you fix it?
// How can you fix it?

const recipe = {
title: "bruschetta",
Expand All @@ -12,4 +11,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);;
11 changes: 9 additions & 2 deletions Sprint-2/implement/contains.js
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.
Comment on lines +3 to +10
Copy link
Contributor

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?

contains("ABC", "1");
contains(["A", "B", "C"], "1");
contains(123, "1");
contains(true, "1");
contains({ key: undefined }, "key");

Suggestion:, Look up JS "in" operator vs Object.hasOwn.

11 changes: 9 additions & 2 deletions Sprint-2/implement/lookup.js
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
10 changes: 7 additions & 3 deletions Sprint-2/implement/querystring.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that in real querystring, both key and value are percent-encoded or URL encoded in the URL. For example, the string "5%" will be encoded as "5%25". So to get the actual value of "5%25" (whether it is a key or value in the querystring), you should call a function to decode it.
May I suggest looking up any of these terms, and "How to decode URL encoded string in JS"?

}


return queryParams;
}

module.exports = parseQueryString;
module.exports = parseQueryString;
13 changes: 12 additions & 1 deletion Sprint-2/implement/tally.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.


module.exports = tally;
//no updates. works well
26 changes: 18 additions & 8 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
const numKey = Number(key);
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question is asking, why with the original code, invert({a: 1}) does not return {'1': a} (or {1:a}).

Note: The property name (key) is always a string. Even when we express the key as a number (e.g., { 1 : 'a' }), the key will still be converted to a string '1'.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// in file invert.test.js