Skip to content
Closed
5 changes: 3 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

//Answer: The console.log will print "My house number is undefined"" because we are trying to access the house number with the index of an array,
// but our address in as object with key and value. We can fix this by replacing console.log(`My house number is ${address[0]}`); with console.log(`My house number is ${address.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 +13,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address["houseNumber"]}`);
8 changes: 5 additions & 3 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// Answer" In this code there is a problem on how the for..loop was used. For... loop can only be used with an iterable objects such as string,arrays etc but in this code author is just an ordinary object
// and in console.log we aretrying to log value inside the loop, but there's no value being defined as part of for...of. The loop is designed to loop over iterable elements, not object properties.
// To fix this we can use for...in to access properties directly.
// 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,6 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const property in author) {
console.log(author[property]);
}
11 changes: 7 additions & 4 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Predict and explain first...

//The problem here is that the code is logging the entire recipe object instead of the individual ingredients.
//To fix this we have to used for..of loop to list each ingredient on a new 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?
Expand All @@ -10,6 +11,8 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`);
console.log("ingredients:");
for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
9 changes: 7 additions & 2 deletions Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}

function contains(object, key) {
if (typeof object === "object" && object !== null && !Array.isArray(object)) {
return key in object;
} else {
return false;
}
}
module.exports = contains;
32 changes: 31 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,46 @@ 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", () => {
const input = {};
const key = "newKey";
const currentOutput = contains(input, key);
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});

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

test("contains on object with properties returns true", () => {
const input = { x: 1, y: 2 };
const key = "x";
const currentOutput = contains(input, key);
const targetOutput = true;

expect(currentOutput).toEqual(targetOutput);
});
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains on object with non -existent properties returns false", () => {
const input = { x: 1, y: 2 };
const key = "z";
const currentOutput = contains(input, key);
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains on object with invalid parameters returns false", () => {
const input = [123, "yellowfruit", null, undefined];
const key = "color";
const currentOutput = contains(input, key);
const targetOutput = false;

expect(currentOutput).toEqual(targetOutput);
});
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(pairs) {
const lookUp = {};
for (pair of pairs) {
const key = pair[0];
const value = pair[1];
lookUp[key] = value;
}
return lookUp;
}

module.exports = createLookup;
16 changes: 14 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
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 input = [
["US", "USD"],
["CA", "CAD"],
];
const currentOutput = createLookup(input);
const targetOutput = {
US: "USD",
CA: "CAD",
};

expect(currentOutput).toEqual(targetOutput);
});

/*

Expand All @@ -18,7 +30,7 @@ When
Then
- It should return an object where:
- The keys are the country codes
- The values are the corresponding currency codes
- The values are the correspondiSng currency codes

Example
Given: [['US', 'USD'], ['CA', 'CAD']]
Expand Down
11 changes: 10 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
function parseQueryString(queryString) {
const queryParams = {};

// If the query string is empty, return an empty object
if (queryString.length === 0) {
return queryParams;
}

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
// Split by the first "=" into key and the rest into value
const [key, ...valueParts] = pair.split("=");

// Join any remaining parts of the value after the first "="
const value = valueParts.join("=");

// Assign key-value pair to the result object
queryParams[key] = value;
}

Expand Down
32 changes: 30 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,38 @@
// 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",
});
});

test("returns an empty object for an empty query string", () => {
const input = "";
const output = parseQueryString(input);
const expectedOutput = {};
expect(output).toEqual(expectedOutput);
});

test("parses querystring with one key-value pair", () => {
const input = "name=John";
const output = parseQueryString(input);
const expectedOutput = { name: "John" };
expect(output).toEqual(expectedOutput);
});

test("parses querystring with multiple key-value pairs", () => {
const input = "name=John&age=30&city=New York";
const output = parseQueryString(input);
const expectedOutput = { name: "John", age: "30", city: "New York" };
expect(output).toEqual(expectedOutput);
});

test("parses querystring with empty values", () => {
const input = "key1=&key2=";
const output = parseQueryString(input);
const expectedOutput = { key1: "", key2: "" };
expect(output).toEqual(expectedOutput);
});
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;
29 changes: 28 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,42 @@ const tally = require("./tally.js");
// When passed an array of items
// Then it should return an object containing the count for each unique item

test("tally returns an object with counts for each unique item", () => {
const input = ["a", "a", "b", "c"];
const currentOutput = tally(input);
const targetOutput = { a: 2, b: 1, c: 1 };
expect(currentOutput).toEqual(targetOutput);
});

// 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", () => {
const input = [];
const currentOutput = tally(input);
const targetOutput = {};

expect(currentOutput).toEqual(targetOutput);
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally on an array with duplicate itemsreturns an object containing counts for each unique item", () => {
const input = ["a", "a", "a"];
const currentOutput = tally(input);
const targetOutput = { a: 3 };

expect(currentOutput).toEqual(targetOutput);
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("tally throws an error on invalid inputs (null, string, undefined)", () => {
const invalidInputs = [null, "a,a,a", undefined];

invalidInputs.forEach((input) => {
expect(() => tally(input)).toThrow("Input must be an array");
});
});
16 changes: 15 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,34 @@ function invert(obj) {
const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
invertedObj[value] = key;
}

return invertedObj;
}

console.log(invert({ a: 1 }));
console.log(invert({ a: 1, b: 2 }));
console.log(invert({ x: 10, y: 20 }));
console.log(invert({ x: 11, y: 11 }));

// a) What is the current return value when invert is called with { a : 1 }
// The return value is{ key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// The return value is { key: 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 of key-value pairs (arrays), where each pair consists of a key and its corresponding value from the object.
// his is needed in the program because it allows you to iterate over each key-value pair in the object, so you can swap the keys and values.

// d) Explain why the current return value is different from the target output
// We are always assigning the value to the property "key", rather than assigning the property name to the key from the original object.
//In the loop, we overwrite the key each time, which leads to only one property (the last one in the loop) being saved in the invertedObj.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)

module.exports = invert;
38 changes: 38 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const invert = require("./invert");

describe("invert function", () => {
// Test case 1: Invert a single key-value pair
test("should invert a single key-value pair", () => {
const input = { a: 1 };
const expected = { 1: "a" };
expect(invert(input)).toEqual(expected);
});

// Test case 2: Invert multiple key-value pairs
test("should invert multiple key-value pairs", () => {
const input = { a: 1, b: 2 };
const expected = { 1: "a", 2: "b" };
expect(invert(input)).toEqual(expected);
});

// Test case 3: Invert object with different types of values
test("should invert an object with mixed types", () => {
const input = { x: 10, y: 20 };
const expected = { 10: "x", 20: "y" };
expect(invert(input)).toEqual(expected);
});

// Test case 4: Invert an empty object
test("should return an empty object when input is empty", () => {
const input = {};
const expected = {};
expect(invert(input)).toEqual(expected); // An empty object should return an empty object
});

// Test case 5: Invert object with the same values
test("should invert and handle duplicate values", () => {
const input = { a: 1, b: 1 };
const expected = { 1: "b" };
expect(invert(input)).toEqual(expected);
});
});