Skip to content

LONDON | MAY-2025 | HAKAN MURAT KAVUT | MODULE DATA-GROUPS SPRINT-2 #568

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 5 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
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working
// address is not an array so the index can't use to retrieve data from address object.
// We should use key value of object like address.houseNumber or address["houseNumber"].

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

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);
7 changes: 5 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// 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

// At the first, for loop syntax is not correct. We should use "in" instead of "of".
// Then to retrieve property value of property, we should use [].

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

for (const value of author) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}
10 changes: 7 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
// Each ingredient should be logged on a new line
// How can you fix it?

// There is a recipe object and inside the recipe, ingredient property is an array.
// We should use for loop to log out the ingredients.

const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`+'\n'+'Ingredients:');
for (const ingredient of recipe.ingredients) {
console.log(`- ${ingredient}`);
}
10 changes: 8 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(list, prop) {
return (
typeof list === "object" &&
!Array.isArray(list) &&
list[prop] != null
);
}

module.exports = contains;
module.exports = contains;
69 changes: 53 additions & 16 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,61 @@ as the object doesn't contains a key of 'c'
*/

// Acceptance criteria:
describe("find contains", () => {
// 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
it("the object contains the property, returns true", () => {
const list = { a: 1, b: 2 };
let prop = "a";
const result = contains(list, prop);
expect(result).toEqual(true);
});

// 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
it("the object does not contain the property, returns false", () => {
const list = { a: 1, b: 2 };
let prop = "c";
const result = contains(list, prop);
expect(result).toEqual(false);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
// Given an empty object
// When passed to contains
// Then it should return false
it("contains on empty object returns false", () => {
const list = {};
let prop = "a";
const result = contains(list, prop);
expect(result).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
it("the property is contained returns true", () => {
const list = { x: 10, y: 20 };
let prop = "y";
const result = contains(list, prop);
expect(result).toEqual(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
it("contains returns false when the property does not exist", () => {
const list = { x: 10, y: 20 };
let prop = "z";
const result = contains(list, prop);
expect(result).toEqual(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
it("returns false when the input is not an object (array)", () => {
const list = ["a", "b", "c"];
let prop = "a";
const result = contains(list, prop);
expect(result).toEqual(false);
});
});
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const queryParams = {};
for (const item of countryCurrencyPairs) {
const key = item[0];
const value = item[1];
queryParams[key] = value;
}
return queryParams;
}

module.exports = createLookup;
24 changes: 24 additions & 0 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,27 @@ It should return:
'CA': 'CAD'
}
*/

// Acceptance criteria:
describe("creates a country currency code lookup for multiple codes", () => {
it("the object contains the property, returns true", () => {
const countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
];
let expected = { US: "USD", CA: "CAD" };
const result = createLookup(countryCurrencyPairs);
expect(result).toEqual(expected);
});

it("the object contains the three properties, returns true", () => {
const countryCurrencyPairs = [
["TR", "TRY"],
["JP", "JPY"],
["DK", "DKK"],
];
let expected = { TR: "TRY", JP: "JPY", DK: "DKK" };
const result = createLookup(countryCurrencyPairs);
expect(result).toEqual(expected);
});
});
6 changes: 4 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const keyValuePairs = queryString.split(",");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const [rawKey, ...rawValueParts] = pair.split("=");
const key = decodeURIComponent(rawKey || "");
const value = decodeURIComponent(rawValueParts.join("=") || "");
queryParams[key] = value;
}

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

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
});
});
9 changes: 8 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function tally() {}
function tally(input) {
if(!Array.isArray(input)) throw new Error("Input is not valid.");;
const queryParams = {};
for (const char of input) {
queryParams[char] = queryParams[char] !== undefined ? queryParams[char] +1 : 1;
}
return queryParams;
}

module.exports = tally;
37 changes: 35 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,49 @@ 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
describe("take a list of items and count the frequency of each item", () => {
[
{ input: ["a"], expected: { a: 1 }},
{ input: ["a", "a", "a"], expected: { a: 3 } },
{ input: ["a", "a", "b", "c"], expected: { a : 2, b: 1, c: 1 }},
].forEach(({ input, expected }) =>
it(`returns an object`, () => expect(tally(input)).toEqual(expected))
);
});

// 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");
it("tally on an empty array returns an empty object", () => {
const input = [];
const result = tally(input);
expect(result).toEqual({});
});

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

describe("take a list of items and count the frequency of duplicate items", () => {
[
{ input: ["a"], expected: { a: 1 }},
{ input: ["a", "b", "c", "a", "b", "c"], expected: { a: 2, b:2, c:2 } },
{ input: ["a", "a", "b", "c","a", "a", "b", "c"], expected: { a : 4, b: 2, c: 2 }},
{ input: ["apple", "banana", "apple", "orange", "banana", "apple"], expected: { apple: 3, banana: 2, orange: 1 } },
].forEach(({ input, expected }) =>
it(`returns correct counts for input which are duplicated`, () => expect(tally(input)).toEqual(expected))
);
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
describe("an invalid input like string, number, boolean or object", () => {
[
{ input: 5 },
{ input: "Hello" },
{ input: true },
{ input: { a: 2, b: 1, c: 1 } },
].forEach(({ input }) =>
it(`tally should throw an error for input is not array }`, () =>
expect(() => tally(input)).toThrow("Input is not valid."))
);
});
16 changes: 10 additions & 6 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ 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 }
module.exports = invert;

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

// entries method used for converting the object to an array object as key, value pair.
// d) Explain why the current return value is different from the target output

// current return value is actual return of function and target output is expected.
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// I created separated test file

18 changes: 18 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const invert = require("./invert.js");

describe("creates a invert object", () => {
it("the object contains one key, value pair", () => {
const obj = { a : 1 };
let expected = {"1":"a"};
const result = invert(obj);
expect(result).toEqual(expected);
});

it("the object contains the two pairs", () => {
const obj = { a: 1, b: 2 };
let expected = {"1":"a" , "2":"b"};
const result = invert(obj);
expect(result).toEqual(expected);
});
});