Skip to content

ZA | May-2025 | Malusi Skunyana | Sprint-2 #571

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 16 commits 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
8 changes: 7 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// Predict and explain first...
// The code is trying to access the houseNumber property of the address object
// but it's using an incorrect syntax. Instead of using address[0],
// it should use address.houseNumber or address['houseNumber'].

// This code should log out the houseNumber from the address object
// but it isn't working...
// It logs `undefined` because `address[0]` is trying to access the first element
// of an array, but `address` is an object, not an array.

// Fix anything that isn't working

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

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address['houseNumber']}.`);
9 changes: 8 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
// Predict and explain first...
// The code is trying to iterate over the `author` object using a `for...of` loop.
// However, `for...of` is designed to iterate over iterable objects like arrays or
// strings and does not work with plain objects. Therefore, it will throw a `TypeError`
// indicating that the object is not iterable.

// 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
// It isn't working because the `for...of` loop is not suitable for iterating over
// an object. Instead, we can use `Object.values()` to get an array of the
// object's values and then iterate over that array.

const author = {
firstName: "Zadie",
Expand All @@ -11,6 +18,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
12 changes: 10 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
// Predict and explain first...
// The code will log out the title and how many it serves, but it will not log
// the ingredients correctly. It will not log ingredients correctly because
// ${recipe} does not access the ingredients array properly. Secondly, it will
// not format the ingredients on new lines as expected.

// 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?
// To fix it, we need to access the ingredients array properly and format it so
// that each ingredient appears on a new line. We can use the join method to
// convert the array into a string with each ingredient on a new line and use
// template literals to format the output correctly.

const recipe = {
title: "bruschetta",
Expand All @@ -11,5 +19,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join('\n ')}`);
20 changes: 19 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
function contains() {}
function contains() {
// Check if the first argument is an object
if (typeof arguments[0] !== 'object' || arguments[0] === null) {
return false; // Return false for non-object types
}

// If the first argument is an array, return false
if (Array.isArray(arguments[0])) {
return false;
}

// Check if the second argument is a string
if (typeof arguments[1] !== 'string') {
return false; // Return false for non-string property names
}

// Check if the object contains the property
return Object.prototype.hasOwnProperty.call(arguments[0], arguments[1]);
}

module.exports = contains;
20 changes: 19 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,38 @@ as the object doesn't contains a key of 'c'
// 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
test("contains on object with existing property returns true", () => {
expect(contains({ a: 1, b: 2 }, 'a')).toBe(true);
});

test("contains on object with non-existent property returns false", () => {
expect(contains({ a: 1, b: 2 }, 'c')).toBe(false);
});

// 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({}, 'a')).toBe(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 existing property returns true", () => {
expect(contains({ a: 1, b: 2 }, 'a')).toBe(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 non-existent property returns false", () => {
expect(contains({ a: 1, b: 2 }, 'c')).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains on array returns false", () => {
expect(contains([1, 2, 3], '0')).toBe(false);
});
8 changes: 6 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const lookup = {};
for (const [country, currency] of pairs) {
lookup[country] = currency;
}
return lookup;
}

module.exports = createLookup;
9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
test("creates a country currency code lookup for multiple codes", () => {
const input = [['US', 'USD'], ['CA', 'CAD']];
const expectedOutput = {
US: 'USD',
CA: 'CAD'
};
expect(createLookup(input)).toEqual(expectedOutput);
});

/*

Expand Down
8 changes: 6 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
const [key, ...rest] = pair.split("=");
const value = rest.join("=");
// Decode key and value to handle special characters
const decodedKey = decodeURIComponent(key);
const decodedValue = decodeURIComponent(value);
queryParams[decodedKey] = decodedValue;
}

return queryParams;
Expand Down
42 changes: 42 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,45 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

test("parses querystring values containing multiple =", () => {
expect(parseQueryString("equation=x=y+1&another=2=3")).toEqual({
"equation": "x=y+1",
"another": "2=3",
});
});

test("parses querystring with no values", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses querystring with multiple values", () => {
expect(parseQueryString("a=1&b=2&c=3")).toEqual({
"a": "1",
"b": "2",
"c": "3",
});
});

test("parses querystring with empty values", () => {
expect(parseQueryString("a=&b=&c=")).toEqual({
"a": "",
"b": "",
"c": "",
});
});

test("parses querystring with special characters", () => {
expect(parseQueryString("a=1&b=2%20with%20spaces&c=3%40email.com")).toEqual({
"a": "1",
"b": "2 with spaces",
"c": "3@email.com",
});
});

test("parses querystring with encoded characters", () => {
expect(parseQueryString("a=1&b=%C3%A9%20%C3%A0%20%C3%A9")).toEqual({
"a": "1",
"b": "é à é",
});
});
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Invalid input");
}

return items.reduce((acc, item) => {
if (typeof item !== 'string') {
throw new Error("Invalid input");
}
acc[item] = (acc[item] || 0) + 1;
return acc;
}, {});
}

module.exports = tally;
22 changes: 21 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,36 @@ 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
test("tally on an array of items returns an object with counts", () => {
const input = ['a', 'b', 'a', 'c', 'b', 'a'];
const expectedOutput = { a: 3, b: 2, c: 1 };
const result = tally(input);
expect(result).toEqual(expectedOutput);
});

// 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 input = [];
const expectedOutput = {};
const result = tally(input);
expect(result).toEqual(expectedOutput);
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally on an array with duplicate items returns counts for each unique item", () => {
const input = ['a', 'b', 'a', 'c', 'b', 'a'];
const expectedOutput = { a: 3, b: 2, c: 1 };
const result = tally(input);
expect(result).toEqual(expectedOutput);
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally on an invalid input throws an error", () => {
expect(() => tally("invalid")).toThrowError("Invalid input");
});
29 changes: 29 additions & 0 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}

/*

function invert(obj) {
const invertedObj = {};

Expand All @@ -17,13 +19,40 @@ function invert(obj) {
}

// a) What is the current return value when invert is called with { a : 1 }
console.log(invert({ a: 1 })); // returns { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
console.log(invert({ a: 1, b: 2 })); // returns { key: 2 }

*/

// c) What is the target return value when invert is called with {a : 1, b: 2}
// target output: {"1": "a", "2": "b"}

// c) What does Object.entries return? Why is it needed in this program?
// Object.entries returns an array of a given object's own enumerable string-keyed
// property [key, value] pairs. It is needed in this program to easily iterate
// over the key-value pairs of the input object.


// d) Explain why the current return value is different from the target output
// The current return value is different from the target output because the code
// incorrectly assigns the value to the key in the inverted object.
// It uses `invertedObj.key = value` instead of `invertedObj[value] = key`

// e) Fix the implementation of invert (and write tests to prove it's fixed!)

function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj[value] = key;
}

return invertedObj;
}

// Tests
console.log(invert({ a: 1 })); // returns { "1": "a" }
console.log(invert({ a: 1, b: 2 })); // returns { "1": "a", "2": "b" }
console.log(invert({ x: 10, y: 20 })); // returns { "10": "x", "20": "y" }
34 changes: 34 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,37 @@

3. Order the results to find out which word is the most common in the input
*/

function countWords(str) {
if (typeof str !== 'string') {
throw new Error("Invalid input: expected a string");
}

// Remove punctuation and normalize to lowercase
const cleanedText = str
.replace(/[.,!?]/g, '') // Remove punctuation
.toLowerCase(); // Convert to lowercase

const words = cleanedText.split(/\s+/); // Split by whitespace
const counts = {};

for (const word of words) {
if (word === '') continue; // Skip empty strings
counts[word] = (counts[word] || 0) + 1;
}

// Sort by most common words
const sortedCounts = Object.fromEntries(
Object.entries(counts).sort(([, a], [, b]) => b - a)
);

return sortedCounts;
}

// Test cases:

console.log(countWords("You and me, and YOU!"));
// Output: { you: 2, and: 2, me: 1 }

console.log(countWords("Hello, world! Hello again."));
// Output: { hello: 2, world: 1, again: 1 }
Loading