Skip to content

ZA|Innocent Niwatwine| Data-groups Sprint-2 #629

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 18 commits into
base: main
Choose a base branch
from
6 changes: 5 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Predict and explain first...
//The code is trying to access address[0], but address is an object, can not be an array.
//Objects use keys instead of numeric indices.
//So address[0] is undefined, because there is no property with the key "0".


// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +16,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
6 changes: 5 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Predict and explain first...

//for...of is used with iterables like arrays and strings
//objects are not iterables by default as it is seen in the code like author which is an object


// 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 +15,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of object.values(author) {
console.log(value);
}
3 changes: 2 additions & 1 deletion Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
////${recipe} brings the entire object (recipe) directly into the string.

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -12,4 +13,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
5 changes: 4 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
function contains() {}
function contains(array, value) {
return array.includes(value);
}


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 @@ -22,14 +22,32 @@ as the object doesn't contains a key of 'c'
// Then it should return false
test.todo("contains on empty object returns false");

// Empty object return false
test("contains on empty object returns false", () => {
const result = contains({}, "a");
expect(result).toBe(false); });
// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

// Object with properties and existing key should return true
test("contains returns true for existing property", () => {
const obj = { a: 1, b: 2 };
const result = contains(obj, "a");
expect(result).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("contains returns false for non-existing property", () => {
const obj = { a: 1, b: 2 };
const result = contains(obj, "c");
expect(result).toBe(false);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains returns false or throws when input is not an object", () => {
const obj = ["a", "b"];
expect(() => contains(["a", "b"], "a")).toThrow(); });
10 changes: 9 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
function createLookup() {
function createLookup(pairs) {
const lookup= {};
for (let [countryCode, currencyCode] of pairs) {
lookup[countryCode] = currencyCode;
}

return lookup;
// implementation here
}
// implementation here
}

Expand Down
14 changes: 13 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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 expected = {
US: 'USD',
CA: 'CAD'
};
expect(createLookup(input)).toEqual(expected);
});


/*

Expand Down
16 changes: 10 additions & 6 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
if (!queryString) return queryParams;

const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
}
const [key, ...rest] = pair.split("="); // Grab everything after first '='
const value = rest.join("="); // Safely join back if value had '=' signs

const decodedKey = decodeURIComponent((key || "").replace(/\+/g, " "));
const decodedValue = decodeURIComponent((value || "").replace(/\+/g, " "));

queryParams[decodedKey] = decodedValue;
}

return queryParams;
}
Expand Down
28 changes: 25 additions & 3 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,31 @@
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.

const parseQueryString = require("./querystring.js")

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
test("parses querystring values containing +", () => {
expect(parseQueryString("equation=x%3Dy%2B1")).toEqual({
"equation": "x=y+1",
});
});
test("returns empty object for empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses single key with empty value", () => {
expect(parseQueryString("foo=")).toEqual({ foo: "" });
});

test("parses single key with no equals sign", () => {
expect(parseQueryString("foo")).toEqual({ foo: "" });
});

test("parses multiple key-value pairs", () => {
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
});

test("decodes URL-encoded characters", () => {
expect(parseQueryString("name=John%20Doe&city=New%20York")).toEqual({
name: "John Doe",
city: "New York",

});
});
21 changes: 20 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
function tally() {}

function tally(items) {
// Validate input: must be an array
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

// Create an object to hold counts
const counts = {};

// Loop through each item and count occurrences
for (const item of items) {
if (counts[item]) {
counts[item] += 1;
} else {
counts[item] = 1;
}
}

return counts;
}
module.exports = tally;
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,24 @@ 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", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

test("tally counts each unique item correctly", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 1,
});
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws error on non-array input", () => {
expect(() => tally("not-an-array")).toThrow("Input must be an array");
});
35 changes: 34 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,53 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

// a) What is the current return value when invert is called with { a : 1 }

// // { key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// { key: 2 }

// 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?
// It returns an array of key-value pairs from the object obj, where each pair is itself an array

// d) Explain why the current return value is different from the target output
// The return is inside the first loop, where the function will exit after the first iteration, and never processes all key-value pairs.

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

test("inverts an empty object to an empty object", () => {
expect(invert({})).toEqual({});
});

test("inverts an object with one key-value pair", () => {
expect(invert({ x: 10 })).toEqual({ "10": "x" });
});

test("inverts a simple object", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b" });
});

test("inverts an object with duplicate values, keeping the last key", () => {
expect(invert({ a: 1, b: 1 })).toEqual({ "1": "b" });
});

test("inverts an object with string values", () => {
expect(invert({ hello: "world", name: "Innocent" })).toEqual({
world: "hello",
Innocent: "name",
});
});

test("throws an error when given a non-object input", () => {
expect(() => invert("not an object")).toThrow("Input must be an object");
});
15 changes: 0 additions & 15 deletions Sprint-3/quote-generator/index.html

This file was deleted.

Loading