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
Expand Up @@ -12,4 +12,6 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
//console.log(`My house number is ${address["houseNumber"]}`);
//console.log(`My house number is ${address.houseNumber}`);
//both work
10 changes: 9 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Predict and explain first...
//it will have an errors

// 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
//The problem is that the variable author is an object which is not iterable in for.. of loops. You have to use either values, keys or entries.

const author = {
firstName: "Zadie",
Expand All @@ -11,6 +13,12 @@ const author = {
alive: true,
};

for (const value of author) {
for (const value of Object.values(author)) {
console.log(value);
//Prints the following output:
//Zadie
//Smith
//writer
//40
//true
}
5 changes: 3 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Predict and explain first...
// It will print bruschetta serves 2 ingredients error

// This program should log out the title, how many it serves and the ingredients.
// Each ingredient should be logged on a new line
Expand All @@ -11,5 +12,5 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
${recipe.ingredients.join("\n")}`);
// make the ingredients into a string and use next line for each ingredient.
19 changes: 18 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
function contains() {}
function contains(object, property) {
// Check if object is a valid object (not null, array, number, string, etc.)
//if so return false
if (typeof object !== 'object' || object === null) {
return false;
}
// Check if the object has the specified property as its own property
return object.hasOwnProperty(property);
}

module.exports = contains;

// console.log(contains({ a: 1, b: 2 }, 'a')); // true
// console.log(contains({ a: 1, b: 2 }, 'c')); // false
// console.log(contains({}, 'a')); // false
// console.log(contains([], 'a')); // false
// console.log(contains(1, 'a')); // false
// console.log(contains("NotANumber", 'a')); // false
// console.log(contains(null, 'a')); // false

25 changes: 24 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,43 @@ 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("When object contains the property, it returns true", () => {
const obj = { a: 1, b: 2 };
expect(contains(obj, "a")).toBe(true);
expect(contains(obj, "b")).toBe(true);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("When given an empty object, it 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);
expect(contains(obj, "b")).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 inputs", () => {
expect(contains([], "a")).toBe(false);
expect(contains(2, "a")).toBe(false);
expect(contains("notANumber", "a")).toBe(false);
expect(contains(null, "a")).toBe(false);
});
20 changes: 18 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
// Create an empty object
const lookupPairs = {};
// Loop through each pair in the array
for (let i = 0; i < countryCurrencyPairs.length; i++) {
const pair = countryCurrencyPairs[i]; // Get the current pair
const country = pair[0]; // First item is the country code
const currency = pair[1]; // Second item is the currency code
lookupPairs[country] = currency; // Add it to the object
}

return lookupPairs; // Return the final object
}
//check that it works
const pairs = [
["US", "USD"],
["CA", "CAD"],
];
console.log(createLookup(pairs)); // Output: { US: 'USD', CA: 'CAD'}

module.exports = createLookup;
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
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" };
expect(createLookup(input)).toEqual(expectedOutput);
});
/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down
18 changes: 15 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
function parseQueryString(queryString) {
// create an empty object to store results
const queryParams = {};
// if the string is empty, return the empty object
if (queryString.length === 0) {
return queryParams;
}
// split the string into parts at each "&"
const keyValuePairs = queryString.split("&");
// loop through each part

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
// Find the first "=" sign
const index = pair.indexOf("=");
// throw error if "=" is missing
if (index === -1) {
throw new Error("Invalid query string: missing '='");
}// Everything before "=" is the key
const key = decodeURIComponent(pair.slice(0, index));
// Everything after "=" is the value
const value = decodeURIComponent(pair.slice(index + 1));
// Put key and value into the object
queryParams[key] = value;
}

// return the object with results.
return queryParams;
}

module.exports = parseQueryString;
23 changes: 23 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,26 @@ test("parses querystring values containing =", () => {
"equation": "x=y+1",
});
});

test("decodes URL-encoded characters like spaces", () => {
expect(parseQueryString("month=March%202026&user=John%20Doe")).toEqual({
"month": "March 2026",
"user": "John Doe"
});
});
// no equals, treats flag as an invalid input so it needs to be fixed.
test("throws error when no equals sign is present", () => {
expect(() => parseQueryString("flag")).toThrow("Invalid query string");
});

// Empty Values
test("handles keys with an equals sign but no value", () => {
expect(parseQueryString("user=")).toEqual({
"user": ""
});
});

// Empty String Case
test("returns an empty object for an empty string", () => {
expect(parseQueryString("")).toEqual({});
});
23 changes: 22 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
function tally() {}
function tally(list) {
//This object will store each item as a key and how many times it appears as a value. First declare an empty array
var counts = {};
//validate list
if (!Array.isArray(list)) {
throw new Error("Input must be an array");
}
//for...of loop, to go through each element in list one by one.
for (var item of list) {
//counts[item] checks if the object already has a key named after the current item.
if (counts[item]) {
//If it exists, increase the count.
counts[item] += 1;
} else {
// create a new key in the object and set its value to 1.
counts[item] = 1;
}
}

return counts;
}

console.log(tally(["a", "a", "c", "a", "d"]));
module.exports = tally;
26 changes: 25 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,36 @@ 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("counts each unique item correctly", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({
a: 2,
b: 1,
c: 1,
});

expect(tally(["a", "b", "b", "a", "c", "a"])).toEqual({
a: 3,
b: 2,
c: 1,
});

expect(tally(["a", "a", "a"])).toEqual({
a: 3,
});
});
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("throws error if input is not an array", () => {
expect(() => tally({})).toThrow("Input must be an array");
expect(() => tally("This is a string")).toThrow("Input must be an array");
expect(() => tally(123)).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;
//create new keys and values to make them dynamic
let newKey = value.toString();
let newValue = key.toString();
invertedObj[newKey] = newValue;
}

return invertedObj;
}
console.log("Hello");
console.log(invert({ a: 1, b: 2 }));

module.exports = invert;
// a) What is the current return value when invert is called with { a : 1 }
//Answer: { key: 1 }

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

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

// c) What does Object.entries return? Why is it needed in this program?
// Answer:[ ["a", 1], ["b", 2] ].
// It turns an object into an array of pairs.
// It is needed so you can loop through both key and value easily.

// d) Explain why the current return value is different from the target output
// The current return value is different because it uses a fixed property name "key", so each loop overwrites the previous value, resulting in only one property.
// The target output correctly swaps keys and values, creating a new property for each key-value pair.

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

test("inverts objects correctly", () => {
expect(invert({ a: 1, b: 2 })).toEqual({
"1": "a",
"2": "b"
});
});

test("handles duplicate values and uses the last key overwrites the first", () => {
expect(invert({ a: 1, b: 1 })).toEqual({
"1": "b"
});
});

test("works when keys are numbers", () => {
expect(invert({ 1: "alpha", 2: "beta" })).toEqual({
"alpha": "1",
"beta": "2"
});
});

test("returns an empty object when input is empty", () => {
expect(invert({})).toEqual({});
});

Loading
Loading