Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c9145df
CalculateMedian function implemented
Jul 16, 2026
1cf01db
Comment updated on return null
Jul 16, 2026
256ef50
Function calculateMedian updated so it could calculate even elements …
Jul 16, 2026
84b5c1e
.houseNumber added in order to the house number to be printed.
Jul 18, 2026
51f62a2
Author object for of loop changed into for in tp print al the values …
Jul 19, 2026
ae0b900
Recipe object literal fixed so it can prints the ingredients on new l…
Jul 19, 2026
702b5ba
A fifth element removed from ingredients array which was unneeded
Jul 23, 2026
583bb2c
Tests created for all cases.
Jul 24, 2026
c0f9787
function implemented for checking property exist in an object.
Jul 24, 2026
552c68c
Initial test case created
Jul 24, 2026
9058a37
Function implemented for creating an abject from key-value array.
Jul 24, 2026
ce06e18
Spelling mistakes and bug in CA key fixed. Test passes.
Jul 24, 2026
e6267ac
Missing return obj added.
Jul 24, 2026
14c848c
Commented unneeded code deleted.
Jul 24, 2026
c66861d
First test values containing = tested
Jul 25, 2026
c4dec19
the for loop for testing values containing = fixed
Jul 25, 2026
d5c28aa
If !pair added for checking empty strings.
Jul 25, 2026
b10058d
decodeURLComponent function used to decode percent-encoded characters…
Jul 25, 2026
7fc4886
Using a regular expression .replace(/\+/g, " ") the + sign is remove…
Jul 25, 2026
946461b
Last test commented out
Jul 25, 2026
37e70cb
Test created for all the cases
Jul 25, 2026
1b4063a
function implemented for checking duplicate items.
Jul 25, 2026
af59161
for the last test for invalid input test adjusted so jest can check f…
Jul 25, 2026
0877d2d
if statement added to check the item is number not string.
Jul 25, 2026
4c0bba5
invertedObj value is wrapped in square brackets to access the proper…
Jul 31, 2026
5518e5e
test created to check invert function works as intended
Jul 31, 2026
ed6927a
Restore file to match upstream main
Jul 31, 2026
d438535
Remove prep files and restore files to match upstream main
Jul 31, 2026
3163a9c
Remove prep files from tracking
Jul 31, 2026
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
2 changes: 1 addition & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
7 changes: 5 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);
for (const value in author) {
console.log(author[value]);
}

// The for of loop is trying to iterate through the object like an array.
// For in loop would work better.
9 changes: 7 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,10 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients[0]}
${recipe.ingredients[1]}
${recipe.ingredients[2]}
${recipe.ingredients[3]}`);

// On line 15 we can add recipe.ingredients[]
9 changes: 8 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function contains() {}
function contains(object, property) {
for (let key in object) {
if (key === property) {
return true;
}
}
return false;
}

module.exports = contains;
19 changes: 18 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,37 @@ 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("contains on an object with property returns true", function () {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
});
test("contains on an object without property returns false", function () {
expect(contains({ a: 1, b: 2 }, "d")).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", function () {
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("contain on object with properties", function () {
expect(contains({ c: 4, d: 5 }, "c")).toBe(true);
});

// 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 property name returns false", function () {
expect(contains({ c: 4, d: 5 }, "x")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contain on invalid parameters returns false", function () {
expect(contains([], "0")).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(pairs) {
let obj = {};
for (let pair of pairs) {
obj[pair[0]] = pair[1]
}
return obj;
}

module.exports = createLookup;
9 changes: 8 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +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", function() {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({US: "USD", CA: "CAD",});
});

/*

Expand Down
7 changes: 6 additions & 1 deletion Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
if (!pair) {
continue;
}
const everything = pair.split("=");
const key = decodeURIComponent(everything.shift().replace(/\+/g, " "));
const value = decodeURIComponent(everything.join("=").replace(/\+/g, " "));
queryParams[key] = value;
}

Expand Down
4 changes: 3 additions & 1 deletion Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// 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")
const parseQueryString = require("./querystring.js");

test("should parse values containing '='", () => {
expect(parseQueryString("equation=a=b-2")).toEqual({
Expand Down Expand Up @@ -40,9 +40,11 @@ test("should replace '+' by ' '", () => {
// 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",
});
});
*/
11 changes: 10 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
function tally() {}
function tally(arr) {
if (!Array.isArray(arr)) {
throw new Error("Invalid input");
}
const count = {};
for (const item of arr) {
count[item] = (count[item] || 0) + 1;
}
return count;
}

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

// 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", function() {
expect(tally([])).toEqual({})
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item
test("tally on duplicate items returns count of items", function() {
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("tally on invalid input throws an error", function() {
expect(() => tally("string")).toThrow("Invalid input")
})
10 changes: 9 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,28 @@ 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 }
// - Ony 1 is printed

// b) What is the current return value when invert is called with { a: 1, b: 2 }
// - 1 and 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() returns an array of a given object's own enumerable string keyed property key-value pair

// d) Explain why the current return value is different from the target output
// - invertedObj only referenced the swapped value to key. We should have another variable to swap and reference key to value

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

test("invert on an object with key-value returns key-value swapped", function () {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
expect(invert({ a: 1, b: 2 })).toEqual({ 1: "a", 2: "b" });
expect(invert({ distance1: 105, distance2: 201 }))
.toEqual({ 105: "distance1", 201: "distance2" });
expect(invert({ fruit: "apple", veg: "carrot" }))
.toEqual({ "apple": "fruit", "carrot": "veg" });
});
Loading