Skip to content
Closed
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
Expand Up @@ -4,6 +4,8 @@
// but it isn't working...
// Fix anything that isn't working

// it won't work because this is an object literal with key-value pairs

const address = {
houseNumber: 42,
street: "Imaginary Road",
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"]}`);
7 changes: 5 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const val in author) {
console.log(author[val]);
}
// Changing value to val so it does not conflict with fact that it is the keys that are being logged to
// display the 'value' they hold
//Used for .... in as this will iterate over the keys ie the indexes rather than the values
8 changes: 6 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:`);
for (const items of recipe.ingredients) {
console.log(items);
};
//we have to use for...of as it will loop directly over values of the array
// rather than the for ... in which will loop over the indexes.
14 changes: 13 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function contains() {}
function contains(obj,item) {
if (obj === null || typeof obj !== 'object' || Array.isArray(obj)) {
return false;
}

if (typeof item !== 'string') {
return false;
}

// this checks that the property exists in the object
return obj.hasOwnProperty(item);
}


module.exports = contains;
19 changes: 18 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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");

// Given an object with properties
// When passed to contains with an existing property name
Expand All @@ -33,3 +32,21 @@ 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

describe('contains', () => {
test('returns false when given an empty object', () => {
expect(contains({}, 'a')).toBe(false);
});

test('returns true if the object has the property', () => {
expect(contains({ a: 1, b: 2, c: 3, d: 4 }, 'a')).toBe(true);
});

test('returns false if the object does not have the property', () => {
expect(contains({ a: 1, b: 2, c: 3 }, 'e')).toBe(false);
});

test('should return false when passed an array', () => {
expect(contains([1, 2, 3], 'a')).toBe(false);
});
});
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(countryCurrencyPairs) {
let lookup = {}; //begins with an empty object to populate

for (let pair of countryCurrencyPairs) {
let country = pair[0];
let currency = pair[1];

lookup[country] = currency;
}
//this iterates through all pairs returning them into the created empty object
return lookup;
}

module.exports = createLookup;
20 changes: 19 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
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", () => {
const pairs = [
['US', 'USD'],
['CA', 'CAD'],
['GB', 'GBP'],
['JP', 'JPY'],
['IN', 'INR'],
];

const expected = {
US: 'USD',
CA: 'CAD',
GB: 'GBP',
JP: 'JPY',
IN: 'INR',
};

expect(createLookup(pairs)).toEqual(expected);
});

/*

Expand Down
17 changes: 15 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,21 @@ function parseQueryString(queryString) {
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
for (let i = 0; i < keyValuePairs.length; i++) {
const pair = keyValuePairs[i];
const equalIndex = pair.indexOf("=");

let key, value;

if (equalIndex === -1) {
key = pair;
value = "";
} // if no "=" is found key is assigned with a value of empty string
else {
key = pair.substring(0, equalIndex);
value = pair.substring(equalIndex + 1);
} // Split into key and value for as many "=" in the value using substring

queryParams[key] = value;
}

Expand Down
25 changes: 23 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,28 @@
const parseQueryString = require("./querystring.js")

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

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

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

test("parses key without value (assigns empty string)", () => {
expect(parseQueryString("a=&b=2")).toEqual({a: "", b: "2"});
});

test("handles key with no '=' at all", () => {
expect(parseQueryString("hello")).toEqual({hello: ""});
});
20 changes: 19 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input an array!");
}

const counts = {};

for (let i = 0; i < arr.length; i++) {
const item = arr[i];
if (counts[item]) {
counts[item] += 1;
} else {
counts[item] = 1;
}// if/else ensures add to the count of an item if it has been
// counted in a previous iteration or start at 1.
}

return counts;
}

module.exports = tally;
28 changes: 27 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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.todo("tally on an empty array returns an empty object");

// Given an array with duplicate items
// When passed to tally
Expand All @@ -32,3 +32,29 @@ test.todo("tally on an empty array returns an empty object");
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error

describe("tally function", () => {
test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

test("tally on an array with one item", () => {
expect(tally(["a"])).toEqual({ a: 1 });
});

test("tally on an array with duplicate items", () => {
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
});

test("tally on an array with multiple different items", () => {
expect(tally(["a", "a", "a", "b", "b", "c"])).toEqual({ a: 3, b: 2, c: 1 });
});

test("tally throws an error for a string input", () => {
expect(() => tally("not an array")).toThrow("Input an array!");
});

test("tally throws an error for an object input", () => {
expect(() => tally({ a: 1 })).toThrow("Input an array!");
});
});
13 changes: 12 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,31 @@ 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 }
// the current return will take the value in the key value pair using Object.entries and
// and assign it to a property called key returning {key:1}

// b) What is the current return value when invert is called with { a: 1, b: 2 }
//after the first loop, second iteration reassigns the value to
//the property called key returning {key:2}

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

// c) What does Object.entries return? Why is it needed in this program?
//this function allows for iteration over both keys and values at once
// instead of looping over them seperately.

// d) Explain why the current return value is different from the target output
//for the target output, we want the property name to be the set to the value
// of the original key-value pair.

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

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

test("inverts multiple unique key-value pairs", () => {
expect(invert({ x: 3, y: 4 })).toEqual({ "3": "x", "4": "y" });
});
Loading