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

//This code is using the wrong syntax to access the houseNumber property of the address object. It will therefore return undefined.
// 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 +12,4 @@ const address = {
postcode: "XYZ 123",
};

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

// This program attempts to log out all the property values in the object. its not going to work because we cannot use "for of" loop in this situation.
// 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 +11,6 @@ const author = {
alive: true,
};

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

// This program will not log out the ingredients correctly because we are trying to log the whole recipe object instead of the ingredients array.
// 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 @@ -12,4 +12,4 @@ const recipe = {

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
7 changes: 6 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
function contains() {}
function contains(obj, key) {
if (typeof obj !== 'object' || obj === null || Array.isArray(obj)) {
return false;
}
return Object.hasOwn(obj, key);
}

module.exports = contains;
8 changes: 5 additions & 3 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ as the object doesn't contains a key of 'c'
// 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.todo("contains returns true for existing property");
test.todo("contains returns false for non-existent property");
// Given an empty object
// When passed to contains
// Then it should return false
Expand All @@ -25,11 +26,12 @@ 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

test.todo("contains returns true for existing property");
// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test.todo("contains returns false for non-existent property");
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test.todo("contains with invalid parameters returns false or throws error");
7 changes: 6 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function createLookup() {
function createLookup(pairs) {
// implementation here
const lookup = {};
for (const [country, currency] of pairs) {
lookup[country] = currency;
}
return lookup;
}

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

test.todo("creates a country currency code lookup for multiple codes");
test.todo("creates a country currency code lookup for an empty array");
test.todo("creates a country currency code lookup for duplicate country codes");
test.todo("creates a country currency code lookup for invalid input");

/*

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

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
const index = pair.indexOf("=");
if (index === -1) {
queryParams[pair] = "";
} else {
const key = pair.substring(0, index);
const value = pair.substring(index + 1);
queryParams[key] = value;
}
}

return queryParams;
}

Expand Down
19 changes: 19 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,22 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});
test("parses multiple parameters", () => {
expect(parseQueryString("a=1&b=2")).toEqual({ a: "1", b: "2" });
});
test("parses empty querystring", () => {
expect(parseQueryString("")).toEqual({});
});
test("parses parameter without value", () => {
expect(parseQueryString("flag")).toEqual({ flag: "" });
});
test("parses parameter with empty value", () => {
expect(parseQueryString("empty=")).toEqual({ empty: "" });
});
test("parses parameter with multiple =", () => {
expect(parseQueryString("data=a=b=c")).toEqual({ data: "a=b=c" });
});
test("parses parameter with special characters", () => {
expect(parseQueryString("name=John%20Doe&age=30")).toEqual({ name: "John%20Doe", age: "30" });
});

15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("Input must be an array");
}
const counts = Object.create(null);
for (const item of arr) {
if (counts[item] === undefined) {
counts[item] = 1;
} else {
counts[item] = counts[item] + 1;
}
}
return counts;
}

module.exports = tally;
3 changes: 3 additions & 0 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const tally = require("./tally.js");
// 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.todo("tally returns correct counts for unique items in an array");

// Given an empty array
// When passed to tally
Expand All @@ -28,7 +29,9 @@ 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
test.todo("tally returns correct counts for duplicate items in an array");

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test.todo("tally with invalid input throws an error");
40 changes: 35 additions & 5 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,43 @@ function invert(obj) {
}

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

//It returns an array of [key, value] pairs from the object. It is needed so we can loop over each key/value pair.
// d) Explain why the current return value is different from the target output

//The .key is incorrect syntax for setting a key in the object. It should be in brackets.
// 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;

//jest test cases
const invert = require("./invert.js");

test("inverts a single object", () => {
expect(invert({ a: 1 })).toEqual({ "1": "a" });
});

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

test("inverts string values", () => {
expect(invert({ a: "hello", b: "world" })).toEqual({ "hello": "a", "world": "b" });
});

test("inverts an empty object", () => {
expect(invert({})).toEqual({});
});
Loading