Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
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
@@ -1,8 +1,10 @@
// Predict and explain first...
//the original code is using the wrong notation to access our object this will return undefined as we do not have a 0 property.

// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working
//fixed it by putting "houseNumber" in the square brackets.

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"]}`);
6 changes: 4 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...
//the method for of is an array method and it would be looking for an indexed list of items rather than a key value pair so an error will be thrown since we are using an object.

// 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
//i can fix this by changing for ... of to for ... in.

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

for (const value of author) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}
17 changes: 14 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
// Predict and explain first...
//it will display the title and number of people it serves on one line then the whole recipe on the next line

// 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?
//by using the string literal to ensure multiple lines, then having the title on the first line, the number served on the next line and ingredients on the third line. using the dot notation to access the properties needed.

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

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
//changed console to display for any number of ingredients
console.log(`${recipe.title} serves ${recipe.serves}
ingredients: ${recipe.ingredients[0]}
${recipe.ingredients[1]}
${recipe.ingredients[2]}
${recipe.ingredients[3]}`);

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:`);
for (let ingredient of recipe.ingredients) {
console.log(` ${ingredient}`);
}
16 changes: 15 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
function contains() {}
function contains(checkObj, checkProp) {
if (
typeof checkObj === "object" &&
checkObj !== null &&
!Array.isArray(checkObj)
) {
if (Object.hasOwn(checkObj, checkProp)) {
return true;
} else {
return false;
}
} else {
throw new Error("error invalid parameter please provide an object");
}
}

module.exports = contains;
11 changes: 10 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,25 @@ 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("contains on empty object returns false", () =>
expect(contains({}, "a")).toBe(false));

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("returns true when an object contains a property that matches the one passed to contains", () =>
expect(contains({ area: "Manchester" }, "area")).toBe(true));

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("returns false when an object with properties is passed to contains with a non-existent property name", () =>
expect(contains({ area: "Manchester" }, "town")).toBe(false));

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("should throw an error when when an invalid parameter like an array is passed to contains", () =>
expect(() => contains([1, 4, 5], 0)).toThrow(
"error invalid parameter please provide an object"
));
17 changes: 16 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
function createLookup() {
function createLookup(inputArray) {
// implementation here
const returnObject = {};

if (!Array.isArray(inputArray)) {
throw new Error("error incorrect parameter passed please provide an array");
}

if (inputArray.length === 0) {
return returnObject;
}

for (let item of inputArray) {
returnObject[item[0]] = item[1];
}

return returnObject;
}

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

test.todo("creates a country currency code lookup for multiple codes");
test("creates a country currency code lookup for multiple codes", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
["MW", "MWK"],
["ZW", "ZWD"],
])
).toEqual({ US: "USD", CA: "CAD", MW: "MWK", ZW: "ZWD" });
});

test("if passed an empty array , should return an empty object", () => {
expect(createLookup([])).toEqual({});
});

test("if passed parameter which is not an array throw an error", () => {
expect(() => createLookup("US, USD")).toThrow(
"error incorrect parameter passed please provide an array"
);
});

/*

Expand Down
30 changes: 26 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
function parseQueryString(queryString) {
const queryParams = {};

if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
if (queryString.includes("&")) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
if (!pair.includes("=")) {
throw new Error(
"error invalid format string, no = to separate key value pairs"
);
} else {
const keyValuePair = pair.split("=");
console.log(keyValuePair + "the single pair");

queryParams[keyValuePair[0]] = keyValuePair[1];
}
}
} else if (queryString.includes("=")) {
const equalSignIndex = queryString.indexOf("=");

queryParams[queryString.slice(0, equalSignIndex)] = queryString.slice(
equalSignIndex + 1
);
} else {
throw new Error(
"error invalid format string, no = to separate key value pairs"
);
}

return queryParams;
Expand Down
31 changes: 29 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,37 @@
// 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("parseQueryString receives an empty string", () => {
expect(parseQueryString("")).toEqual({});
});

test("if our function is passed 2 key - value pairs", () => {
expect(parseQueryString("color=brown&width=100")).toEqual({
color: "brown",
width: "100",
});
});

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

test("if our function is passed only one key - value pair", () => {
expect(parseQueryString("color=brown")).toEqual({ color: "brown" });
});

test("parses querystring without an =, it should throw an error", () => {
expect(() => parseQueryString("colorisequaltobrown")).toThrow(
"error invalid format string, no = to separate key value pairs"
);
});

test("if our function is passed only one string but there is no =", () => {
expect(() => parseQueryString("color,brown")).toThrow(
"error invalid format string, no = to separate key value pair"
);
});
36 changes: 35 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
function tally() {}
function tally(inputArray) {
if (Array.isArray(inputArray)) {
if (inputArray.length === 0) {
return {};
}

let itemCount = 0;
const tallyObject = {};
let n = 0;

while (inputArray.length > 0) {
itemCount = 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

itemCount is used only inside the while loop, so it is best practice to declare the variable here (in the block { ... } where the variable is being used)

const tempArray = [];
Copy link
Contributor

Choose a reason for hiding this comment

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

Can your code work without using tempArray?

let i = 0;
let currentArrayItem = inputArray[0];

while (i < inputArray.length) {
if (currentArrayItem === inputArray[i]) {
itemCount++;
tempArray.push(inputArray.splice(i, 1));
i--;
}

if (itemCount > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this check necessary? Under what circumstances will itemCount be 0 or less?

tallyObject[currentArrayItem] = itemCount;
}

i++;
}
}
return tallyObject;
} else {
throw new Error("error invalid input passed, please provide an array");
}
}

module.exports = tally;
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,27 @@ 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("tally on an empty array returns 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("given an array with duplicate items it should return counts for each unique item ", () => {
expect(tally(["a", "a", "b", "c", "c", "d", "a"])).toEqual({
a: 3,
b: 1,
c: 2,
d: 1,
});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("should throw an error when tally is passed an invalid input like a string", () => {
expect(() => tally("b,b,c,d,e,e,f")).toThrow(
"error invalid input passed, please provide an array"
);
});
24 changes: 22 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,41 @@
function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
if (obj !== null && !Array.isArray(obj) && typeof obj === "object") {
for (const [key, value] of Object.entries(obj)) {
if (typeof value != "string" && typeof value !== "number") {
throw new Error(
"error invalid input entered, expecting an object to have only strings as values"
);
}

invertedObj[value] = key;
}
} else {
throw new Error("error invalid input entered, expecting an object");
}

return invertedObj;
}

module.exports = invert;

// a) What is the current return value when invert is called with { a : 1 }
//it returns a string describing the object.

// b) What is the current return value when invert is called with { a: 1, b: 2 }
//it returns an object with one key value pair, the key is key and value is 2

// c) What is the target return value when invert is called with {a : 1, b: 2}
//the target return value is {"1": "a", "2": "b"}.

// c) What does Object.entries return? Why is it needed in this program?
//it returns an array made up of arrays of the original objects key - value pairs.
//it allows us to unpack the contents of the object into an array which can then be used to create the new object

// d) Explain why the current return value is different from the target output
//because we used dot notation to assign a value to our key, this creates a property called key
//and assigns it a value. we want our key to get its name from a variable so we need to use bracket notation.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
//we can fix it by using invertedObj[value] = key.
28 changes: 28 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const invert = require("./invert.js");

describe("tests to see if invert function swaps around the keys and values in a given object", () => {
test("if invert is passed a value which is not an object, it should throw an error", () => {
expect(() =>
invert([
["x", 10],
["y", 20],
])
).toThrow("error invalid input entered, expecting an object");
});

test("if we are passed an empty object we should return an empty object", () => {
expect(invert({})).toEqual({});
});

test("given an object with key value pairs, it should swap the keys and values in the object", () => {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
expect(
invert({ city: "Birmingham", population: "345908", boroughs: "20" })
).toEqual({ Birmingham: "city", 345908: "population", 20: "boroughs" });
});
});
test("if invert is passed an object which has an array or object as one of its values, it should throw an error", () => {
expect(() => invert({ cars: { toyota: 2, bmw: 1, benz: 4 } })).toThrow(
"error invalid input entered, expecting an object to have only strings as values"
);
});
2 changes: 2 additions & 0 deletions Sprint-2/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading