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

// The code is trying to log the house number from the address object, but it is using the wrong syntax to access the property. The code is currently using address[0], which is incorrect because it is trying to access the first element of an array, but address is an object, not an array.

// 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}`);
6 changes: 4 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// This program attempt to log out all the property values in the object, It isnt working because we are trying to iterate over an object using a for... of loop, which is not valid. To fix this, we can use a for... in loop to iterate over the keys of the object and then access the corresponding values.

// 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 key in author) {
console.log(author[key]);
}
10 changes: 7 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// The code is trying to log out the title, how many it serves and the ingredients of a recipe. However, it is not working because when we try to log the recipe object directly, it will not format the output as intended. Instead, we should access each property of the recipe object separately and format the output accordingly.

// 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 +12,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}`);
}
8 changes: 7 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(object, property) {
if (object === null || typeof object !== "object" || Array.isArray(object)) {
return false;
}

return Object.prototype.hasOwnProperty.call(object, property);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • The function is expected to throw an error or return false when object is not an object or is an array.

  • Could also explore Object.hasOwn().

}

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

/*
Implement a function called contains that checks an object contains a
particular property
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}, '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'
*/
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("contains checks if an object contains a particular property", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).toBe(true);
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("contains an object with existing property returns true", () => {
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("contains an object with non-existent property returns false", () => {
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("contains with invalid parameters returns false", () => {
expect(contains([], "a")).toBe(false);
});

test("contains with invalid parameters returns false", () => {
expect(contains([1, 2, 3], "1")).toBe(false);
});
8 changes: 6 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
function createLookup() {
// implementation here
function createLookup(countryCurrency) {
const lookup = {};
for (const [country, currency] of countryCurrency) {
lookup[country] = currency;
}
return lookup;
}

module.exports = createLookup;
16 changes: 15 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
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 expectedOutput = {
US: "USD",
CA: "CAD",
};

const result = createLookup(input);

expect(result).toEqual(expectedOutput);
});

/*

Expand Down
17 changes: 15 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
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;
if (pair === "") {
continue;
}

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

if (indexOfEquals === -1) {
queryParams[pair] = "";
} else {
const key = pair.slice(0, indexOfEquals);
const value = pair.slice(indexOfEquals + 1);
queryParams[key] = value;
}
}

return queryParams;
Expand Down
46 changes: 44 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,52 @@
// 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", () => {
expect(parseQueryString("")).toEqual({});
});

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

test("handles a key with an empty value", () => {
expect(parseQueryString("name=")).toEqual({
name: "",
});
});

test("handles a key with no equals sign", () => {
expect(parseQueryString("name")).toEqual({
name: "",
});
});

test("handles empty pairs in query string", () => {
expect(parseQueryString("a=b&&c=d")).toEqual({
a: "b",
c: "d",
});
});

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

test("handles empty key", () => {
expect(parseQueryString("=b")).toEqual({
"": "b",
});
});
18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Invalid input");
}

const result = Object.create(null);

for (const item of items) {
if (result[item]) {
result[item]++;
} else {
result[item] = 1;
}
}

return result;
}

module.exports = tally;
20 changes: 19 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,30 @@ 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("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally counts duplicate items correctly", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 1,
});
});

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

test("handles inherited property names like toString", () => {
expect(tally(["toString", "toString"])).toEqual(
Object.assign(Object.create(null), { toString: 2 })
);
});
20 changes: 19 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,38 @@ 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 }

//{ key: 1 }

// b) What is the current return value when invert is called with { a: 1, b: 2 }

//{ key: 2 }

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

//{ "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 from the object.
// Object.entries({ a: 1, b: 2 });
// returns: [["a", 1], ["b", 2]]
//It is needed so we can loop through both the keys and values of the object at the same time.

// d) Explain why the current return value is different from the target output

//The current implementation is incorrect because it uses:
// invertedObj.key = value;
// This creates a property literally called "key" instead of using the actual key/value dynamically.
//So it overwrites the same property each time instead of building the correct object.

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
18 changes: 18 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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("returns empty object when given empty object", () => {
expect(invert({})).toEqual({});
});
26 changes: 26 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,29 @@

3. Order the results to find out which word is the most common in the input
*/
function countWords(str) {
const result = Object.create(null);

const cleaned = str
.toLowerCase()
.replace(/[.,!?]/g, "")
.trim();

if (cleaned === "") {
return result;
}

const words = cleaned.split(/\s+/);

for (const word of words) {
if (result[word]) {
result[word]++;
} else {
result[word] = 1;
}
}

return result;
}

module.exports = countWords;
Loading
Loading