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
28 changes: 28 additions & 0 deletions Sprint-2/Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Predict and explain first...

// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working

/*const address = {
houseNumber: 42,
street: "Imaginary Road",
city: "Manchester",
country: "England",
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`); */

// The line "address[0]" is wrong. Because address is an object not an array, so "address[0]" is undefined. The output will be "My house number is undefined".

// Corrected code
const address = {
houseNumber: 42,
street: "Imaginary Road",
city: "Manchester",
country: "England",
postcode: "XYZ 123",
};

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

// 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

/* const author = {
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};

for (const value of author) {
console.log(value);
}
*/

// Prediction and Explanation
/* The line "for (const value of author) {console.log(value);}" will not work because for-of is used only for iterable things, e.g: arrays, strings, maps, sets.
But author is a plain object (created with {}), and plain objects are not iterable. So Javascript throws: TypeError: author is not iterable because objects don't have a natural order to loop through. */

// Corrected code
const author = {
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};

for (const value of Object.values(author)) {
console.log(value);
}
38 changes: 38 additions & 0 deletions Sprint-2/Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Predict and explain first...

// 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?

/*const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

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

// Prediction and Explanation
/*
The line inside the template string: ${recipe} does not print the ingredients.
Instead, JavaScript converts the entire recipe object into a string, which becomes "[object Object]". This happens
because plain objects {} are not automatically converted into readable text. The program should print each ingredient on a new line, but the current code
never loops through recipe.ingredients, so nothing is printed correctly.
*/

// Corrected code
const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

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

for (const ingredient of recipe.ingredients) {
console.log(ingredient);
}
10 changes: 10 additions & 0 deletions Sprint-2/Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function contains(obj, prop) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}

return obj.hasOwnProperty(prop);
}

module.exports = contains;

62 changes: 62 additions & 0 deletions Sprint-2/Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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
test("returns true when object contains the property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).toBe(true);
});

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


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

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("returns true when object contains the property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("returns false when object does not contain the property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("returns false for invalid parameters like an array", () => {
expect(contains([], "a")).toBe(false);
expect(contains(null, "a")).toBe(false);
expect(contains(123, "a")).toBe(false);
expect(contains("hello", "a")).toBe(false);
});
5 changes: 5 additions & 0 deletions Sprint-2/Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function createLookup() {
// implementation here
}

module.exports = createLookup;
35 changes: 35 additions & 0 deletions Sprint-2/Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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

Acceptance Criteria:

Given
- An array of arrays representing country code and currency code pairs
e.g. [['US', 'USD'], ['CA', 'CAD']]

When
- createLookup function is called with the country-currency array as an argument

Then
- It should return an object where:
- The keys are the country codes
- The values are the corresponding currency codes

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

When
createLookup(countryCurrencyPairs) is called

Then
It should return:
{
'US': 'USD',
'CA': 'CAD'
}
*/
16 changes: 16 additions & 0 deletions Sprint-2/Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

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

return queryParams;
}

module.exports = parseQueryString;
48 changes: 48 additions & 0 deletions Sprint-2/Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// In the prep, we implemented a function to parse query strings.
// Unfortunately, it contains several bugs!
// Below are some test cases the implementation doesn't handle well.
// Fix the implementation for these tests, and try to think of as many other edge cases as possible - write tests and fix those too.

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

test("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
equation: "a=b-2",
});
});

test("should ignore empty key-value pairs", () => {
expect(parseQueryString("key1=value1&&key2=value2&")).toEqual({
key1: "value1",
key2: "value2",
});
});

test("should accept empty string as key or as value", () => {
expect(parseQueryString("=value")).toEqual({ "": "value" });
expect(parseQueryString("key")).toEqual({ key: "" });
expect(parseQueryString("key=")).toEqual({ key: "" });
expect(parseQueryString("=")).toEqual({ "": "" });
});

test("should decode percent-encoded characters", () => {
expect(parseQueryString("%24half=1%2F2")).toEqual({
$half: "1/2",
});
});

test("should replace '+' by ' '", () => {
expect(parseQueryString("full+name=John+Doe")).toEqual({
"full name": "John Doe",
});
});

// Stretch exercise: Handling query strings that contain identical keys

// Delete this test if you are not working on this optional case
test("should store values of a key in an array when the key has 2 or more values", () => {
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
key: ["value1", "value2", "value3"],
foo: "bar",
});
});
3 changes: 3 additions & 0 deletions Sprint-2/Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function tally() {}

module.exports = tally;
34 changes: 34 additions & 0 deletions Sprint-2/Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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 }
*/

// Acceptance criteria:

// Given a function called tally
// 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
45 changes: 45 additions & 0 deletions Sprint-2/Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Let's define how invert should work

// Given an object
// When invert is passed this object
// Then it should swap the keys and values in the object

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
/*
function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}
*/

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

// b) What is the current return value when invert is called with { a: 1, b: 2 } - The current 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?
/* Object.entries(obj) returns an array of [key, value] pairs. Example: Object.entries({ a:1, b:2 }) returns [["a", 1], ["b", 2]].
It is needed because the loop "for (const [key, value] of Object.entries(obj))" let's each pair destructure easily.
*/

// d) Explain why the current return value is different from the target output - It is because this line "invertedObj.key = value;" uses literal string "key instead of the variable key" which is a wrong property name,
// instead of this "invertedObj[value] = key;"

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

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

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

test("inverts multiple key-value pairs", () => {
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
});

test("handles empty objects", () => {
expect(invert({})).toEqual({});
});

test("handles string values", () => {
expect(invert({ x: "hello" })).toEqual({ hello: "x" });
});
Loading
Loading