Skip to content
6 changes: 4 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Predict and explain first...

// I think, for object we have to use key as property value instead of array index in console.log.
// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working

//Correct Code:

const address = {
houseNumber: 42,
street: "Imaginary Road",
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}`);
13 changes: 9 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

// I think we have to use the dot notation associated with the key with variable author to see the property values as output.
// Besides, we can omit for...of loop for this time cause there is no function for this program.
// 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 +12,10 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
}
console.log(author.firstName);
console.log(author.lastName);
console.log(author.occupation);
console.log(author.age);
console.log(author.alive);


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

// My prediction is for log out the title, how many it serves and the ingridients we have to use several console.log for each of the property
// value. Besides, for each ingredient should be logged on a new line, we can use array index for the ingredients array.
// 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?
Expand All @@ -10,6 +11,10 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);

console.log(recipe.title);
console.log(recipe.serves);
console.log(recipe.ingredients[0]);
console.log(recipe.ingredients[1]);
console.log(recipe.ingredients[2]);
console.log(recipe.ingredients[3]);
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(input, propertyName) {
const result = Object.hasOwn(input, propertyName);
return result;
}

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

/*
Implement a function called contains that checks an object contains a
particular property

E.g. contains({a: 1, b: 2}, 'a') // returns true
as the object contains a key of 'a'

E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

// Acceptance criteria:

// 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

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns 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 a non-existent property name
// Then it should return false

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains on empty object returns false", function () {
const object = {};
const currentOutput = contains(object, "a");
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});

test("contains with an existing property name should return true", function () {
const object = {
a: 1,
b: 2,
flower: "red",
};
const currentOutput = contains(object, "a");
const targetOutput = true;

expect(currentOutput).toBe(targetOutput);
});

test("contains with a non-existing property name should return false", function () {
const object = {
a: 1,
b: 2,
flower: "red",
};
const currentOutput = contains(object, "c");
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});

test("contains with an array should return false", function () {
const object = ["a", "b", "c"];

const currentOutput = contains(object, "c");
const targetOutput = false;

expect(currentOutput).toBe(targetOutput);
});
5 changes: 3 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const keyValuePairs = Object.fromEntries(countryCurrencyPairs);
return keyValuePairs;
}

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

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

/*

Create a lookup object of key value pairs from an array of code pairs

Acceptance Criteria:

Given
- An array of arrays representing country code and currency code pairs
e.g. [['US', 'USD'], ['CA', 'CAD']]

When
- createLookup function is called with the country-currency array as an argument

Then
- It should return an object where:
- The keys are the country codes
- The values are the corresponding currency codes

Example
Given: [['US', 'USD'], ['CA', 'CAD']]

When
createLookup(countryCurrencyPairs) is called

Then
It should return:
{
'US': 'USD',
'CA': 'CAD'
}
*/
test("creates a country currency code lookup for multiple codes", function () {
const arr = [
["US", "USD"],
["CA", "CAD"],
["BD", "BDT"],
];
const currentOutput = createLookup(arr);
const targetOutput = {
US: "USD",
CA: "CAD",
BD: "BDT",
};

expect(currentOutput).toEqual(targetOutput);
});
33 changes: 30 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,37 @@ function parseQueryString(queryString) {
return queryParams;
}
const keyValuePairs = queryString.split("&");
const emptyElement = "";

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
for (i = 0; i < keyValuePairs.length; i++) {
const currentPair = keyValuePairs[i];
if (currentPair !== emptyElement) {
const index = currentPair.indexOf("=");
if (index !== -1) {
const key = currentPair.slice(0, index);
const value = currentPair.slice(index + 1);
const decodeKey = decodeURIComponent(key);
const decodeValue = decodeURIComponent(value);
const replacedKey = decodeKey.replace("+", " ");
const replacedValue = decodeValue.replace("+", " ");
if (queryParams.hasOwnProperty(replacedKey)) {
if (Array.isArray(queryParams[replacedKey])) {
queryParams[replacedKey].push(replacedValue);
} else {
queryParams[replacedKey] = [
queryParams[replacedKey],
replacedValue,
];
}
} else {
queryParams[replacedKey] = replacedValue;
}
} else if (index === -1) {
const key = currentPair;
const value = "";
queryParams[key] = value;
}
}
}

return queryParams;
Expand Down
2 changes: 1 addition & 1 deletion Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ test("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
equation: "a=b-2",
});
});
});

test("should ignore empty key-value pairs", () => {
expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({
Expand Down
20 changes: 19 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
function tally() {}
function tally(array) {
if (array.length === 0) {
return {};
}
if (typeof array === "string") {
throw new Error();
}
const object = {};
for (i = 0; i < array.length; i++) {
const currentItem = array[i];
if (object.hasOwnProperty(currentItem)) {
object[currentItem] += 1;
} else {
object[currentItem] = 1;
}
}

return object;
}

module.exports = tally;
44 changes: 14 additions & 30 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
const tally = require("./tally.js");

/**
* tally array
*
* In this task, you'll need to implement a function called tally
* that will take a list of items and count the frequency of each item
* in an array
*
* For example:
*
* tally(['a']), target output: { a: 1 }
* tally(['a', 'a', 'a']), target output: { a: 3 }
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
*/
test("should return an empty object when given an empty array", () => {
expect(tally([])).toEqual({});
});

// Acceptance criteria:
test("should return counts for each unique item when given an array with one item and with duplicate items", () => {
expect(tally(["a"])).toEqual({ a: 1 });
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});

// Given a function called tally
// When passed an array of items
// Then it should return an object containing the count for each unique item

// 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");

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

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("should throw an error when given an invalid input", () => {
function invalidInput() {
tally("string");
}
expect(invalidInput).toThrow(Error);
});
15 changes: 14 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,33 @@ 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?
// Object.entries() returns an array of an object's own enumerable string-keyed property [key, value] pairs.
// It is needed in this program because the invert function needs access to both the original keys and values
// so it can swap them and create a new inverted 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 invertedObj.key = value uses the word "key" as the property
// name instead of using the actual key variable. It stores the values under the same property name "key",
// so it does not swap the original key and value. The correct code should use invertedObj[value] = key to create value:key pairs.

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

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

test("should return swaped keys and values for an given object", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ "1": "a", "2": "b"});
});
Loading