Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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: 3 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// I predict it will cause an error at console.log because it is wanting to print address[0] but
// address is not an array it is an object...

// This code should log out the houseNumber from the address object
// but it isn't working...
Expand All @@ -12,4 +13,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
5 changes: 3 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// the for ... of only works when iterating through data and this is not what is required
// it will cause an error

// 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);
}
4 changes: 2 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Predict and explain first...
// Didn't put recipe.ingredients in console.log so will cause error because cannot access recipe

// 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 @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
7 changes: 6 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}
function contains(obj, key) {
if (typeof obj !== "object" || Array.isArray(obj)) {
Comment thread
cjyuan marked this conversation as resolved.
Outdated
throw new Error("Input must not be an array");
}
return key in obj;
Comment thread
cjyuan marked this conversation as resolved.
Outdated
}

module.exports = contains;
56 changes: 36 additions & 20 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,39 @@ 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", () => {
test.each([
// 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

{ input: [{}, "a"], expected: false },

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true

{ input: [{ a: 1, b: 2 }, "a"], expected: true },

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

{ input: [{ a: 1, b: 2 }, "c"], expected: false },

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
])("finds object contains properties", ({ input, expected }) => {
expect(contains(...input)).toBe(expected);
});
test("throws error when input is an array", () => {
expect(() => contains(["a", "b"], "a")).toThrow(
"Input must not be an array"
);
});
});
11 changes: 8 additions & 3 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function createLookup() {
// implementation here
}
function createLookup(CountryCurrencyPairs) {
const result = {};

for (const pair of CountryCurrencyPairs) {
const [country, currency] = pair;
result[country] = currency;
}
return result;
}
module.exports = createLookup;
16 changes: 13 additions & 3 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");
test("creates lookup object", () => {
const input = [
["US", "USD"],
["CA", "CAD"],
];

/*
const result = createLookup(input);

Create a lookup object of key value pairs from an array of code pairs
expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});

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

Acceptance Criteria:

Expand Down
3 changes: 2 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const [key, ...rest] = pair.split("=");
const value = rest.join("=");
queryParams[key] = value;
Comment thread
cjyuan marked this conversation as resolved.
}

Expand Down
18 changes: 18 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@

const parseQueryString = require("./querystring.js")


test("returns empty object for empty string", () => {
expect(parseQueryString("")).toEqual({});
});

test("parses a single key-value pair", () => {
expect(parseQueryString("name=John")).toEqual({
name: "John",
});
});

test("parses multiple key-value pairs", () => {
expect(parseQueryString("name=John&age=30")).toEqual({
name: "John",
age: "30",
});
});

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
Expand Down
17 changes: 16 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array"); //checks input is an array if not throws error
}

const result = {};

for (const item of arr) {
if (item in result) {
result[item] = result[item] + 1;
} else {
result[item] = 1;
}
}
Comment thread
cjyuan marked this conversation as resolved.
Outdated

return result;
}
module.exports = tally;
36 changes: 24 additions & 12 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,27 @@ const tally = require("./tally.js");
// 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
describe("tally", () => {
test.each([
// Given an empty array
// When passed to tally
// Then it should return an empty object

{ input: [], expected: {} },

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

{ input: ["a", "b", "a"], expected: { a: 2, b: 1 } },

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
])("counts items correctly", ({ input, expected }) => {
expect(tally(input)).toEqual(expected);
});
test("throws error for invalid input", () => {
expect(() => tally("not an array")).toThrow("Input must be an array");
});
});
Loading
Loading