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,5 +1,7 @@
// Predict and explain first...

// Adress is not an array its an object, objects use key values so [0] is an array not a key value for a houseNumber.

// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that 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}`);
3 changes: 2 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
//The code fails because:for...of only works with iterable objects,Plain JavaScript objects,which is author here,are not iterable.

// 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);
}
6 changes: 4 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,7 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);
for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
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(obj, prop) {
if (obj === null) {
return false;
}

if (typeof obj !== "object") {
return false;
}

if (Array.isArray(obj)) {
return false;
}

if (prop in obj) {
return true;
} else {
return false;
}
}

module.exports = contains;
59 changes: 26 additions & 33 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,28 @@
const contains = require("./contains.js");

/*
Implement a function called contains that checks an object contains a
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

// 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("returns false for an empty object", () => {
expect(contains({}, "a")).toBe(false);
});

test("returns true when object contains the property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
});

test("returns false when object does not contain the property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

test("returns false when given an array instead of an object", () => {
expect(contains([], "a")).toBe(false);
});

test("returns false when given null", () => {
expect(contains(null, "a")).toBe(false);
});

test("returns false when given a non-object type", () => {
expect(contains(42, "a")).toBe(false);
expect(contains("string", "a")).toBe(false);
});
});
17 changes: 15 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
if (!Array.isArray(pairs)) {
return {};
}

const lookup = {};

for (const pair of pairs) {
if (Array.isArray(pair) && pair.length === 2) {
const [country, currency] = pair;
lookup[country] = currency;
}
}

return lookup;
}

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

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

expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});

test("returns empty object for empty array", () => {
expect(createLookup([])).toEqual({});
});

test("handles a single pair", () => {
const input = [["GB", "GBP"]];
expect(createLookup(input)).toEqual({
GB: "GBP",
});
});

test("ignores invalid pairs (not arrays)", () => {
const input = [["US", "USD"], "invalid", null];
expect(createLookup(input)).toEqual({
US: "USD",
});
});

test("ignores pairs that do not have exactly two elements", () => {
const input = [["US", "USD"], ["CA"], ["MX", "MXN", "extra"]];
expect(createLookup(input)).toEqual({
US: "USD",
});
});

test("overwrites duplicate country codes with the latest value", () => {
const input = [
["US", "USD"],
["US", "USN"],
];
expect(createLookup(input)).toEqual({
US: "USN",
});
});

test("returns empty object for invalid input (not an array)", () => {
expect(createLookup(null)).toEqual({});
expect(createLookup({})).toEqual({});
expect(createLookup("string")).toEqual({});
});
});

/*

Expand All @@ -21,6 +75,7 @@ Then
- The values are the corresponding currency codes

Example

Given: [['US', 'USD'], ['CA', 'CAD']]

When
Expand Down
26 changes: 22 additions & 4 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,34 @@

function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {

if (!queryString || typeof queryString !== "string") {
return queryParams;
}

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

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

const index = pair.indexOf("=");

let key, value;

if (index === -1) {
key = pair;
value = "";
} else {
key = pair.slice(0, index);
value = pair.slice(index + 1);
}

if (key) {
queryParams[key] = value;
}
}

return queryParams;
}

module.exports = parseQueryString;
module.exports = parseQueryString;
53 changes: 51 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,59 @@
// 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",
});
});

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

test("handles multiple key value pairs", () => {
expect(parseQueryString("a=1&b=2")).toEqual({
a: "1",
b: "2",
});
});

test("handles missing value", () => {
expect(parseQueryString("a=")).toEqual({
a: "",
});
});

test("handles missing equals sign", () => {
expect(parseQueryString("a")).toEqual({
a: "",
});
});

test("handles multiple equals signs in value", () => {
expect(parseQueryString("a=1=2=3")).toEqual({
a: "1=2=3",
});
});

test("ignores empty pairs", () => {
expect(parseQueryString("a=1&&b=2")).toEqual({
a: "1",
b: "2",
});
});

test("overwrites duplicate keys with last value", () => {
expect(parseQueryString("a=1&a=2")).toEqual({
a: "2",
});
});

test("returns empty object for invalid input", () => {
expect(parseQueryString(null)).toEqual({});
expect(parseQueryString(undefined)).toEqual({});
});
});
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const counts = {};

for (const item of items) {
counts[item] = (counts[item] || 0) + 1;
}

return counts;
}

module.exports = tally;
51 changes: 24 additions & 27 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
const tally = require("./tally.js");

/**
* tally array
*
* In this task, you'll need to implement a function called tally
* that will take a list of items and count the frequency of each item
* in an array
*
* For example:
*
* tally(['a']), target output: { a: 1 }
* tally(['a', 'a', 'a']), target output: { a: 3 }
* tally(['a', 'a', 'b', 'c']), target output: { a : 2, b: 1, c: 1 }
*/
describe("tally", () => {
test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

// Acceptance criteria:
test("counts duplicate items correctly", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 1,
});
});

// 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("counts single item array", () => {
expect(tally(["a"])).toEqual({ a: 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("counts all same items", () => {
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("throws error for invalid input (string)", () => {
expect(() => tally("abc")).toThrow("Input must be an array");
});

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