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
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// I think that it will not run because we're trying to access an object on line 15
// Instead it should be ${adddress[houseNumber]}

// This code should log out the houseNumber from the address object
// but it isn't working...
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: 3 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// Because it's trying to iterate through an object
// It should just print author without a loop

// 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 +13,4 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
}
console.log(Object.values(author));
9 changes: 7 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// I think it should use recipe.ingredients instead of recipe at line 15
// Ok I tried it and now I think that it should be done with a loop to list the ingredients

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -11,5 +13,8 @@ const recipe = {
};

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

for (const i of recipe.ingredients) {
console.log(i);
}
20 changes: 19 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
function contains() {}
function contains(toCheck, input) {
if (
typeof toCheck !== "object" ||
Array.isArray(toCheck) ||
toCheck === null
) {
return false;
}

for (const key in toCheck) {
if (key === input) {
return true;
}
}

return false;
}

console.log(contains({ a: 1, b: 2 }, "a"));

module.exports = contains;
13 changes: 12 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,17 @@ as the object doesn't contains a key of 'c'
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

test(`Should return true if object contains the property`, () => {
expect(contains({ a: 1, b: 2 }, "a")).toEqual(true);
expect(contains({ b: 2 }, "a")).toEqual(false);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test(`Should return false if object is empty`, () => {
expect(contains({}, "a")).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
Expand All @@ -33,3 +40,7 @@ test.todo("contains on empty object returns false");
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error

test(`Should return false if given wron type`, () => {
expect(contains(["a", "b"], "0")).toEqual(false);
});
19 changes: 18 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
function createLookup() {
function createLookup(array) {
// implementation here

if (!Array.isArray(array)) {
throw new Error("Wrong input type, expected array");
}

if (array.length === 0) {
throw new Error("Array is empty");
}

let output = {};
for (const pair of array) {
const key = pair[0];
const value = pair[1];

output[key] = value;
}
return output;
}

module.exports = createLookup;
17 changes: 15 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,18 @@ It should return:
'CA': 'CAD'
}
*/

test(`Should return an object with correct keys`, () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({ US: "USD", CA: "CAD" });
});

test(`Should throw an error if no input given`, () => {
expect(() => {
createLookup();
}).toThrow();
});
26 changes: 24 additions & 2 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 = {};

// return if input is 0
if (queryString.length === 0) {
return queryParams;
}

// replace all + by ' '
queryString = queryString.replaceAll("+", " ");

// split by &
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
if (pair.length > 0) {
const parts = pair.split("=");
// first part as the key
const key = decodeURIComponent(parts[0]);
// second to last parts as the value
const value = decodeURIComponent(parts.slice(1).join("="));

// if no key then assign new value
if (!Object.hasOwn(queryParams, key)) {
queryParams[key] = value;
} else if (Array.isArray(queryParams[key])) {
queryParams[key].push(value);
// if not array then turn into array with old and new values
} else {
queryParams[key] = [queryParams[key], value];
}
}
}

return queryParams;
Expand Down
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(input) {
let out = {};

if (!Array.isArray(input)) {
throw new Error(
`Wrong input type, expected an array but got ${typeof input}`
);
}

for (const i of input) {
if (!Object.hasOwn(out, i)) {
out[i] = 1;
} else {
out[i] = out[i] + 1;
}
}

return out;
}

console.log(tally(["a", "a", "b"]));

module.exports = tally;
13 changes: 12 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,23 @@ 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(`Should an empty object when 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(`Should return counts for each unique item`, () => {
expect(tally(["a", "a", "b"])).toEqual({ a: 2, b: 1 });
expect(tally(["toString", "a", "b"])).toEqual({ toString: 1, a: 1, b: 1 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test(`Should return an error when given wrong input type`, () => {
expect(() => {
tally("hello");
}).toThrow();
});
10 changes: 9 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ 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 }
// { 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(obj) returns an array of [key, value] pairs, one small array for each property in the object

// d) Explain why the current return value is different from the target output
// because the key and value wasn't inverted in the code

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
// ok!
30 changes: 30 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const invert = require("./invert.js");

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

test("should work with a single key-value pair", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("should return an empty object when given an empty object", () => {
expect(invert({})).toEqual({});
});

test("should overwrite earlier keys when two values are the same", () => {
// if two keys share the same value, the later key wins after inversion
expect(invert({ a: 1, b: 1 })).toEqual({ 1: "b" });
});

test("should coerce numeric and boolean values to string keys", () => {
expect(invert({ a: true, b: false })).toEqual({ true: "a", false: "b" });
});

test("should not mutate the original object", () => {
const original = { a: 1, b: 2 };
invert(original);
expect(original).toEqual({ a: 1, b: 2 });
});
});
Loading