Skip to content
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

London | Emmanuel Gessessew| Module-Data-Groups | WEEK 2 #261

Open
wants to merge 3 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
6 changes: 5 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// Predict and explain first...
// The current code tries to access the houseNumber property of the address object using address[0]. This doesn't work because object properties in JavaScript are typically accessed using dot notation (address.houseNumber) or bracket notation with a string key (address["houseNumber"]).

// address[0] would only work if address were an array or if there was a property with the key 0 on the object, which isn't the case here.

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +15,5 @@ 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,4 +1,8 @@
// Predict and explain first...
// The current code tries to access the houseNumber property of the address object using address[0].
// This doesn't work because object properties in JavaScript are typically accessed using dot notation (address.houseNumber) or bracket notation with a string key (address["houseNumber"]).
//address[0] would only work if address were an array or if there was a property with the key 0 on the object, which isn't the case here.


// 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);
}
8 changes: 6 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Predict and explain first...
// The code attempts to log the ingredients array within the recipe object, but it uses ${recipe} in the string template.
// This will log the string representation of the entire recipe object, which isn't the desired behavior.
//Instead, you want to explicitly log each ingredient on a new line. The code also doesn't include logic to format the ingredients array properly.


// 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 @@ -11,5 +15,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);
13 changes: 10 additions & 3 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function contains() {}

module.exports = contains;
function contains(obj, property) {

if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}
return Object.prototype.hasOwnProperty.call(obj, property);
}

module.exports = contains;

40 changes: 38 additions & 2 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const contains = require("./contains.js");
//const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
Expand All @@ -20,7 +20,7 @@ as the object doesn't contains a key of 'c'
// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
//test.todo("contains on empty object returns false");

// Given an object with properties
// When passed to contains with an existing property name
Expand All @@ -33,3 +33,39 @@ test.todo("contains on empty object returns false");
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error


//answer

const contains = require("./contains.js");

describe("contains", () => {
test("contains on empty object returns false", () => {
expect(contains({}, "a")).toBe(false);
});

test("returns true for an object containing the property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).toBe(true);
expect(contains(obj, "b")).toBe(true);
});

test("returns false for an object without the property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "c")).toBe(false);
});

test("returns false for invalid inputs like an array", () => {

Choose a reason for hiding this comment

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

Great to see adding many types of unit tests covering corner scenarios

expect(contains([1, 2, 3], "a")).toBe(false);
expect(contains(null, "a")).toBe(false);
expect(contains(undefined, "a")).toBe(false);
expect(contains("string", "a")).toBe(false);
});

test("handles nested objects correctly", () => {
const obj = { a: { b: 2 }, c: 3 };
expect(contains(obj, "a")).toBe(true); // "a" exists at the top level
expect(contains(obj.a, "b")).toBe(true); // "b" exists in the nested object
expect(contains(obj, "b")).toBe(false); // "b" doesn't exist at the top level
});
});
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(countryCurrencyPairs) {
const lookup = {};
for (const [countryCode, currencyCode] of countryCurrencyPairs) {
lookup[countryCode] = currencyCode;
}
return lookup;
}

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

test.todo("creates a country currency code lookup for multiple codes");
// test.todo("creates a country currency code lookup for multiple codes");

/*

Expand Down Expand Up @@ -33,3 +33,55 @@ It should return:
'CA': 'CAD'
}
*/


//answer
const createLookup = require("./lookup.js");

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

test("returns an empty object for an empty array", () => {
expect(createLookup([])).toEqual({});
});

test("handles single pair correctly", () => {
const input = [["GB", "GBP"]];
const expectedOutput = { GB: "GBP" };
expect(createLookup(input)).toEqual(expectedOutput);
});

test("overwrites duplicate country codes with the last currency", () => {
const input = [
["US", "USD"],
["US", "USN"],
];
const expectedOutput = { US: "USN" };
expect(createLookup(input)).toEqual(expectedOutput);
});

test("ignores invalid entries", () => {
const input = [
["FR", "EUR"],
["INVALID"], // invalid entry
["DE", "EUR"],
];
const expectedOutput = {
FR: "EUR",
DE: "EUR",
};
expect(createLookup(input)).toEqual(expectedOutput);
});
});
6 changes: 4 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
const [key, ...valueParts] = pair.split("=");
const value = valueParts.join("="); // Handles "=" in the value
queryParams[decodeURIComponent(key)] = decodeURIComponent(value);
}

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

test("handles an empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

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

test("handles keys without values", () => {
expect(parseQueryString("keyWithoutValue&key2=value2")).toEqual({
keyWithoutValue: "",
key2: "value2",
});
});

test("decodes URI components", () => {
expect(parseQueryString("name=John%20Doe&city=New%20York")).toEqual({
name: "John Doe",
city: "New York",
});
});

test("handles keys and values with special characters", () => {
expect(parseQueryString("key=va%26lue&key%2F=another%2Bvalue")).toEqual({
key: "va&lue",
"key/": "another+value",
});
});

test("handles duplicate keys, keeping the last occurrence", () => {
expect(parseQueryString("a=1&a=2")).toEqual({
a: "2",
});
});

test("handles values with leading and trailing spaces", () => {
expect(parseQueryString("key= value ")).toEqual({
key: " value ",
});
});
17 changes: 14 additions & 3 deletions Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function tally() {}

module.exports = tally;
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const counts = {};
for (const item of items) {
counts[item] = (counts[item] || 0) + 1;
}
return counts;
}

module.exports = tally;

56 changes: 54 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const tally = require("./tally.js");
//const tally = require("./tally.js");

/**
* tally array
Expand All @@ -23,7 +23,7 @@ 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.todo("tally on an empty array returns an empty object");

// Given an array with duplicate items
// When passed to tally
Expand All @@ -32,3 +32,55 @@ test.todo("tally on an empty array returns an empty object");
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error


//answer
const tally = require("./tally.js");

describe("tally", () => {
test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

test("returns counts for each unique item in an array with duplicate items", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 1,
});
});

test("returns counts for a single item array", () => {
expect(tally(["a"])).toEqual({
a: 1,
});
});

test("handles numbers in the array", () => {
expect(tally([1, 2, 2, 3, 3, 3])).toEqual({
1: 1,
2: 2,
3: 3,
});
});

test("throws an error for invalid input like a string", () => {
expect(() => tally("invalid input")).toThrow("Input must be an array");
});

test("handles an array with mixed types", () => {
expect(tally([1, "a", 1, "a", "b"])).toEqual({
1: 2,
a: 2,
b: 1,
});
});

test("handles special characters", () => {
expect(tally(["!", "!", "?", "a"])).toEqual({
"!": 2,
"?": 1,
a: 1,
});
});
});
32 changes: 20 additions & 12 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,32 @@

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

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

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

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" }

// d) What does Object.entries return? Why is it needed in this program?
//Object.entries({ a: 1, b: 2 })

// c) What does Object.entries return? Why is it needed in this program?

// d) Explain why the current return value is different from the target output
// e) Explain why the current return value is different from the target output
Object.entries({ a: 1, b: 2 })

// f) 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; // Correctly assigns the value as a key
}

return invertedObj;
}

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
module.exports = invert;
Loading