Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
5 changes: 2 additions & 3 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
// Predict and explain first...

// The code will log undefined because object properties are not accessed by index but through keys
// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working

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

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

//Predict and explain first...
// The loop method used is for arrays and not for object literals.
// for object literals the "in" key word is used to access the keys.
// 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,6 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
}
12 changes: 7 additions & 5 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
// Predict and explain first...

// The program cant log the ingredient because the object literal was not reference inside the console.log()
// 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?

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}`);

for (const part of recipe.ingredients) {
console.log(part);
}
12 changes: 11 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
function contains() {}
function contains(elements, itemKey) {
if (!elements) return false;
if (typeof elements !== "object" || Array.isArray(elements)) {
return false;
}
if (Object.hasOwn(elements, itemKey)) {
return true;
}

return false;
}

module.exports = contains;
70 changes: 50 additions & 20 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,53 @@ 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
describe("contains()", () => {
// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

[{ elements: { a: 1, b: 2 }, itemKey: "a", expected: true }].forEach(
({ elements, itemKey, expected }) =>
it(` should return ${expected} for key ${itemKey} in ${elements}`, () => {
expect(contains(elements, itemKey)).toEqual(expected);
})
);

// Given an empty object
// When passed to contains
// Then it should return false

[{ elements: {}, itemKey: "c", expected: false }].forEach(
({ elements, itemKey, expected }) =>
it(`contains on empty object returns false`, () => {
expect(contains(elements, itemKey)).toEqual(expected);
})
);

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

[{ elements: { a: 1, b: 2 }, itemKey: "c", expected: false }].forEach(
({ elements, itemKey, expected }) =>
it(`When passed to contains with non-existing property name, should return false`, () => {
expect(contains(elements, itemKey)).toEqual(expected);
})
);

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error

[
{
elements: [3, 4, 5, "kofoworola", "Evan,"],
itemKey: "Evan",
expected: false,
},
].forEach(({ elements, itemKey, expected }) =>
it(`Given invalid parameters it should return false`, () => {
expect(contains(elements, itemKey)).toEqual(expected);
})
);
});
13 changes: 11 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
function createLookup() {
// implementation here
function createLookup(elements) {
if (!Array.isArray(elements) || !elements.every(Array.isArray)) {
return "Invalid input";
}
if (elements.length === 0) return "Invalid input";
let lookup = {};

for (x of elements) {
lookup[x[0]] = x[1];
}
return lookup;
}

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

test.todo("creates a country currency code lookup for multiple codes");
describe("createLookup", () => {
[
{
input: [
["US", "USD"],
["CA", "CAD"],
["NJA", "NGN"],
["RSA", "RND"],
["UK", "GBP"],
],
expected: { US: "USD", CA: "CAD", NJA: "NGN", RSA: "RND", UK: "GBP" },
},
].forEach(({ input, expected }) =>
it(`Should return an object lookup of currency representing country code as keys`, () => {
expect(createLookup(input)).toEqual(expected);
})
);
[{ input: ["US", "USD", "CA", "CAD"], expected: "Invalid input" }].forEach(
({ input, expected }) =>
it(`should return "Invalid input" for any input not following the input format`, () => {
expect(createLookup(input)).toEqual(expected);
})
);
});

/*

Expand Down
17 changes: 13 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,22 @@ function parseQueryString(queryString) {
if (queryString.length === 0) {
return queryParams;
}
if (
typeof queryString !== "string" ||
!queryString.includes("=") ||
queryString === undefined
)
return "invalid input";
const keyValuePairs = queryString.split("&");

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

if (index !== -1) {
const key = pair.slice(0, index);
const value = pair.slice(index + 1);
queryParams[key] = value;
}
}
return queryParams;
}

Expand Down
30 changes: 28 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,36 @@
// 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");

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
equation: "x=y+1",
});
});

test("parses querystring values containing duplicate keys", () => {
expect(parseQueryString("a=3&a=4&a=1")).toEqual({
a: "3",
a: "4",
a: "1",
});
});

test("parses querystring values containing url encoding cases ", () => {
expect(parseQueryString("city=New%20York")).toEqual({
city: "New%20York",
});
});

test("parses querystring values containing invalid type", () => {
expect(parseQueryString({ "a=1": undefined })).toEqual("invalid input");
});

test("parses querystring values containing multiple strings", () => {
expect(parseQueryString("user=alex&age=16&city=London")).toEqual({
user: "alex",
age: "16",
city: "London",
});
});
22 changes: 21 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
function tally() {}
function tally(elements) {
if (!Array.isArray(elements)) {
throw new Error("Invalid input ");
}
if (elements.length === 0) {
return {};
}
let count = 0;
const frequency = Object.create(null);
for (let i = 0; i < elements.length; i++) {
const item = elements[i];
if (frequency[item] === undefined) {
frequency[item] = 1;
} else {
frequency[item]++;
}
}
console.log(frequency);
return frequency;
}

tally(["toString", "toString"]);
module.exports = tally;
44 changes: 31 additions & 13 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,38 @@ const tally = require("./tally.js");
*/

// Acceptance criteria:
test("should return an object containing the count for each unique item", () => {
// 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 a function called tally
// When passed an array of items
// Then it should return an object containing the count for each unique item
expect(tally(["a", "b", "c", "d", 2, "edak"])).toEqual({
a: 1,
b: 1,
c: 1,
d: 1,
2: 1,
edak: 1,
});
});

// 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", () => {
// Given an empty array
// When passed to tally
// Then it should return 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("it should return counts for each unique item", () => {
// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
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("it should throw an error for invalid input", () => {
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
expect(() => tally("ggga")).toThrow("Invalid input");
});
18 changes: 16 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

module.exports = invert;

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

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

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// const secondCall = invert({ a: 1, b: 2 });
//secondCall={key:2}
// c) What is the target return value when invert is called with {a : 1, b: 2}

// target value is {1:"a",2:"b"}

// c) What does Object.entries return? Why is it needed in this program?
//Object.entries() returns an array whose elements are arrays corresponding
// to the enumerable string-keyed property key-value pairs found directly upon object.
// It is needed in this program because we want both the object keys and values

// d) Explain why the current return value is different from the target output
//current about is different from the target about because ofter the loop destructuring
// the key and value is assigned the array element of the objects the property

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

14 changes: 14 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

const invert=require('./invert')

test("should swap the keys and values in the object", () => {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
});

test("should swap the keys and values in the object", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("should swap the keys and values in the object", () => {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
});
Loading
Loading