Skip to content
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
Expand Up @@ -4,6 +4,10 @@
// but it isn't working...
// Fix anything that isn't working

/*Ans: My prediction is that because address is an object and not an array,
therefore, its elements should be accessed by providing the value of key
rather than writing index such as we do in array 0,1,2...*/

const address = {
houseNumber: 42,
street: "Imaginary Road",
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"]}`);
9 changes: 6 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
// 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

/*My prediction is that in the below code we are trying to run a for..of loop
on an object like we do for an array, i am not sure how can we make this work for
object, but this one seems like really not going to work because value is not a
value but a key and value */

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

for (const value of author) {
console.log(value);
}
console.log(Object.values(author));
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?

//Ans: The code will log title and serves but all the ingredients will be printed in the same line
// In order to log each ingredient in a new line we need to access each ingredient
// and add a new line character after that, right now we are just logging recipe object there

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}
ingredients:
${recipe.ingredients[0]}\n${recipe.ingredients[1]}\n${recipe.ingredients[2]}\n${recipe.ingredients[3]}`);
7 changes: 5 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
function contains() {}

function contains(object, property) {
if (object === null || typeof object !== "object" || Array.isArray(object))
throw new Error("Input is not a valid object");
else return Object.hasOwn(object, property);
}
module.exports = contains;
91 changes: 73 additions & 18 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,84 @@ 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
describe("containsProperty", () => {
// 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
[
{ input: { a: 1, b: 2 }, property: "a", expected: true },
{ input: { a: 1, b: 2 }, property: "c", expected: false },
{ input: { a: 1, b: 2, sdk: 28299 }, property: "what", expected: false },
].forEach(({ input, property, expected }) =>
it(`Should return true if the object ${input} contains the property ${property} otherwise false`, () =>
expect(contains(input, property)).toEqual(expected))
);

// 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
[
{ input: {}, property: "a", expected: false },
{ input: {}, property: "", expected: false },
{ input: {}, property: null, expected: false },
].forEach(({ input, property, expected }) =>
it(`Should return false if the object ${input} is empty`, () =>
expect(contains(input, property)).toEqual(expected))
);

// 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
[
{ input: { a: 1, dog: 2 }, property: "dog", expected: true },
{
input: { a: 1, b: 2, property3: "here you go" },
property: "property3",
expected: true,
},
{ input: { a: 1, b: 2, sdk: 28299 }, property: "sdk", expected: true },
].forEach(({ input, property, expected }) =>
it(`Should return true if the object ${input} contains the property ${property}`, () =>
expect(contains(input, property)).toEqual(expected))
);

// 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
[
{ input: { a: 1, dog: 2 }, property: "cat", expected: false },
{
input: { a: 1, b: 2, property3: "here you go" },
property: "",
expected: false,
},
{
input: { a: 1, b: 2, thatProperty: 28299 },
property: "",
expected: false,
},
].forEach(({ input, property, expected }) =>
it(`Should return false if the object ${input} doesn't contain the property ${property}`, () =>
expect(contains(input, property)).toEqual(expected))
);

// 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
[
{ input: [1], property: "a" },
{ input: null, property: "c" },
{ input: undefined, property: "what" },
{ input: [1, 2, 3], property: "what" },
].forEach(({ input, property }) =>
it(`Should return false as ${input} is not an object`, () =>
expect(() => contains(input, property)).toThrow(
"Input is not a valid object"
))
);
});
6 changes: 4 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function createLookup() {
function createLookup(array) {
// implementation here
if (!Array.isArray(array) || !array.every((item) => Array.isArray(item)))
throw new Error("Invalid Input");
return Object.fromEntries(array);
}

module.exports = createLookup;
40 changes: 38 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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
Expand Down Expand Up @@ -33,3 +31,41 @@ It should return:
'CA': 'CAD'
}
*/

describe("lookup", () => {
const countryCurrencyArray = [
["US", "USD"],
["CA", "CAD"],
["PAK", "RS"],
];
const countryCurrencyObject = {
US: "USD",
CA: "CAD",
PAK: "RS",
};

// CASE 1: Should return an object with key as country codes and value
// as currency codes when an array is passed containing arrays of countries
// codes and their currencies codes
test(`should return an object of the array [${countryCurrencyArray}]`, () =>
expect(createLookup(countryCurrencyArray)).toEqual(countryCurrencyObject));

// CASE 2: Should return an empty object if empty array is passed
test(`should return an empty object if the array is empty`, () =>
expect(createLookup([[]])).toEqual({}));

// CASE 3: Input is null
test(`should throw an error if null is passed`, () =>
expect(() => createLookup(null)).toThrow("Invalid Input"));
// CASE 4: Input is undefined
test(`should throw an error if undefined is passed`, () =>
expect(() => createLookup(undefined)).toThrow("Invalid Input"));

// CASE 5: Input is empty object {}
test(`should throw an error if undefined is passed`, () =>
expect(() => createLookup({})).toThrow("Invalid Input"));

// CASE 5: Input is single value {}
test(`should throw an error if single value array is passed`, () =>
expect(() => createLookup([123])).toThrow("Invalid Input"));
});
3 changes: 2 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
function parseQueryString(queryString) {
if (typeof queryString !== "string") throw new Error("Invalid Input");
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const [key, value] = pair.split(/=(.*)/);
queryParams[key] = value;
}

Expand Down
54 changes: 52 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,60 @@
// Below is one test case for an edge case the implementation doesn't handle well.
// 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")
const parseQueryString = require("./querystring.js");

//Case 1 : equal sign within the value of query string
test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
equation: "x=y+1",
});
});

//Case 2 : multiple params in the query string
test("parses querystring containing multiple params", () => {
expect(parseQueryString("equation=x=y+1&job=student")).toEqual({
equation: "x=y+1",
job: "student",
});
});

//Case 3 : empty or missing value in the param
test("parses querystring containing empty or missing value in one of the params", () => {
expect(parseQueryString("equation=x=y+1&job=")).toEqual({
equation: "x=y+1",
job: "",
});
});

//Case 4 : extra & in the start, mid or at the end
test("parses querystring containing empty or missing value in one of the params", () => {
expect(parseQueryString("&equation=x=y+1&&job=&")).toEqual({
equation: "x=y+1",
job: "",
});
});

//Case 4 : querystring is empty
test("an empty query string should return an empty object", () => {
expect(parseQueryString("")).toEqual({});
});

//Case 5 : querystring is null
test("if the querystring is null then an error should be thrown", () => {
expect(() => parseQueryString(null)).toThrow("Invalid Input");
});

//Case 6 : querystring is undefined
test("if the querystring is undefined then an error should be thrown", () => {
expect(() => parseQueryString(undefined)).toThrow("Invalid Input");
});

//Case 7 : querystring is a number
test("if the querystring is undefined then an error should be thrown", () => {
expect(() => parseQueryString(123)).toThrow("Invalid Input");
});

//Case 8 : querystring is an object
test("if the querystring is an object then an error should be thrown", () => {
expect(() => parseQueryString({})).toThrow("Invalid Input");
});
11 changes: 9 additions & 2 deletions Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function tally() {}

function tally(array) {
if (!Array.isArray(array)) throw new Error("Not an array");
const tallyObject = {};
for (const item of array) {
if (Object.hasOwn(tallyObject, item)) tallyObject[item] += 1;
else tallyObject[item] = 1;
}
return tallyObject;
}
module.exports = tally;
34 changes: 33 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,48 @@ 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("returns an object containing the count for each unique item in the array", () => {
[
{ input: ["a"], expected: { a: 1 } },
{ input: ["a", "b", "c"], expected: { a: 1, b: 1, c: 1 } },
{ input: ["a", 1, 3, 5], expected: { a: 1, 1: 1, 3: 1, 5: 1 } },
{
input: ["", " ", "what", 10],
expected: { "": 1, " ": 1, what: 1, 10: 1 },
},
].forEach(({ input, expected }) => 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");
test("returns an empty object when an empty array is passed", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("returns an object containing the count for each unique item in the array", () => {
[
{ input: ["a"], expected: { a: 1 } },
{ input: ["a", "a", "a"], expected: { a: 3 } },
{ input: ["a", "a", "b", "c"], expected: { a: 2, b: 1, c: 1 } },
{
input: [1, 1, "q", 1, "q", 5, "javascript", "what", "javascript"],
expected: { 1: 3, q: 2, 5: 1, javascript: 2, what: 1 },
},
{
input: [0, "", 0, 5, "", "", "not_empty", " ", " "],
expected: { 0: 2, "": 3, 5: 1, not_empty: 1, " ": 2 },
},
].forEach(({ input, expected }) => expect(tally(input)).toEqual(expected));
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
["just a string", 123, null, undefined, {}].forEach((input) =>
test(`Should throw an error as ${input} is not a valid input`, () =>
expect(() => tally(input)).toThrow("Not an array"))
);
Loading
Loading