Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5afdf82
correct access to houseNumber in address object
alexandru-pocovnicu Feb 26, 2026
8a0b3af
correct iteration method to log author properties
alexandru-pocovnicu Feb 26, 2026
f820399
update recipe logging to display ingredients on new lines
alexandru-pocovnicu Feb 26, 2026
6e37fe5
implement contains function and add tests for empty and existing prop…
alexandru-pocovnicu Feb 26, 2026
4fd1b2d
fix contains function to correctly check for property existence in ob…
alexandru-pocovnicu Feb 26, 2026
400346f
fix contains function to correctly return false for empty objects
alexandru-pocovnicu Feb 26, 2026
6e91927
add test case for contains function with non-existing property name
alexandru-pocovnicu Feb 26, 2026
06b3767
add error handling test for contains function with invalid object type
alexandru-pocovnicu Feb 26, 2026
bbb05bc
remove commented-out console.log statement from contains function
alexandru-pocovnicu Feb 26, 2026
8f746c1
implement test for createLookup function to validate country-currency…
alexandru-pocovnicu Feb 26, 2026
202e093
implement createLookup function to map country codes to currency codes
alexandru-pocovnicu Feb 26, 2026
1a3329e
refactor parseQueryString function to improve key-value extraction logic
alexandru-pocovnicu Feb 26, 2026
3796cea
fix: handle edge cases in parseQueryString function and update tests
alexandru-pocovnicu Feb 26, 2026
0c37eb3
test: add case for empty key in querystring parsing
alexandru-pocovnicu Feb 26, 2026
613c044
fix: clean up querystring parsing logic and improve test formatting
alexandru-pocovnicu Feb 26, 2026
c279e01
test: add tests for empty array and invalid input in tally function
alexandru-pocovnicu Feb 27, 2026
b93588f
feat: implement tally function to count occurrences in an array
alexandru-pocovnicu Feb 27, 2026
8acd98b
test: fix assertion in tally function tests and ensure proper formatting
alexandru-pocovnicu Feb 27, 2026
327ba71
fix: add input validation to tally function to ensure array input
alexandru-pocovnicu Feb 27, 2026
48a17dc
fix: improve formatting and consistency in tally function and tests
alexandru-pocovnicu Feb 27, 2026
46c15fa
fix: correct implementation of invert function to properly swap keys …
alexandru-pocovnicu Feb 27, 2026
b669259
test: add initial test suite for invert function
alexandru-pocovnicu Feb 27, 2026
4c94700
fix: add input validation to invert function to handle non-object inp…
alexandru-pocovnicu Feb 27, 2026
b80bd10
fix: update invert function test to handle null input and ensure prop…
alexandru-pocovnicu Feb 27, 2026
5b1dab3
test: add additional test cases for invert function to validate behav…
alexandru-pocovnicu Feb 27, 2026
9935333
fix: improve input validation in invert function and update test case…
alexandru-pocovnicu Feb 27, 2026
12de278
refactor: split calculateMode into smaller functions for improved rea…
alexandru-pocovnicu Feb 27, 2026
fad45be
fix: implement countWords function to count word occurrences in a string
alexandru-pocovnicu Feb 27, 2026
fe23a35
fix: ensure case-insensitive word counting and return correct object …
alexandru-pocovnicu Feb 27, 2026
6fc416e
fix: update countWords function to return sorted word counts
alexandru-pocovnicu Feb 27, 2026
6e3ea51
fix: correct total calculation in totalTill function by parsing coin …
alexandru-pocovnicu Mar 1, 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
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// Predict and explain first...
// it will print : My house number is undefined
//objects aren't indexed

// This code should log out the houseNumber from the address object
// but it 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,4 +1,6 @@
// Predict and explain first...
//for...of works only on iterable data types
// we will get an error

// 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]);
}
14 changes: 11 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
// Predict and explain first...
//this will log: bruschetta serves 2 ingredients: title: "bruschetta",
// serves: 2,
// ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
//instead of the ingredients it prints the whole object again , also there is no \n

// 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 @@ -10,6 +14,10 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
let ingredients=[]
for(let ingredient of recipe.ingredients){
ingredients.push(ingredient)
}
console.log(
`${recipe.title} serves ${recipe.serves} ingredients:\n${ingredients.join("\n")}`)

14 changes: 13 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function contains() {}
function contains(obj, propertyName) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
throw new Error("Invalid object");
}
if (Object.keys(obj).length === 0) {
return false;
}
if (Object.keys(obj).includes(propertyName)) {
return true;
}
return false;
}


module.exports = contains;
18 changes: 17 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,32 @@ as the object doesn't contains a key of 'c'
// 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 and propertyName='k' returns false", () => {
expect(contains({}, "k")).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test(
"when contains is called with an object and an existing property name as arguments,it returns true ",()=>{
expect(contains({a:9},"a")).toEqual(true)
}
);


// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("when contains is called with an object and a non-existing property name as arguments,it returns false ", () => {
expect(contains({ a: 9,color:"pink",day:"Sunday" }, "John")).toEqual(false);
});


// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("when contains is called with the string 'parameter' and propertyName='a' as arguments it throws an error", () => {
expect(() => contains("parameter", "a")).toThrow("Invalid object");
});

18 changes: 16 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
function createLookup() {
// implementation here
function createLookup(array) {
let object={}
for(let element of array){
let key=element[0]
let value=element[1]
object[key]=value
}
return object
}

console.log(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
);


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

/*

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

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

return queryParams;
}
console.log(parseQueryString("=equationtu"));

module.exports = parseQueryString;
19 changes: 17 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@
// 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("parses multiple querystring divided by &", () => {
expect(parseQueryString("equation=x=y+1&color=blue")).toEqual({
equation: "x=y+1",
color: "blue",
});
});

test("it returns the value as an empty string if the querystring has no =", () => {
expect(parseQueryString("day")).toEqual({ day: "" });
});

test("it returns the key as an empty string if the querystring has no nothing before =", () => {
expect(parseQueryString("=day")).toEqual({ "": "day" });
});
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(array) {
if (!Array.isArray(array)) {
throw new Error("Invalid input");
}
let obj = {};
array.forEach((elem) => {
obj[elem] = (obj[elem] || 0) + 1;
});
return obj;
}

module.exports = tally;
10 changes: 9 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ 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("when an empty array is passed to tally it 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("when an array with duplicate items is passed to tally , it returns counts for each unique item", () => {
expect(tally(["t", "d", 5, "t", 5])).toEqual({ t: 2, d: 1, 5: 2 });
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("when a string is passed to tally it throws error", () => {
expect(() => tally("asd")).toThrow("Invalid input");
});
11 changes: 10 additions & 1 deletion Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,32 @@
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}

function invert(obj) {
if (typeof obj !== "object" || Array.isArray(obj) || obj === null)
throw new Error("Invalid input");
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?
//it returns the key:value pairs of obj,it's needed to extract those pairs from obj

// d) Explain why the current return value is different from the target output
//because it's using invertedObj.key = value; instead of invertedObj[value] = key;

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
20 changes: 20 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const invert = require("./invert.js");
test("when passed a string invert will throw error", () => {
expect(() => invert("asd")).toThrow("Invalid input");
});

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

test("when passed an object containing one key:value pair, invert returns an object containing value:key", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

test("when passed an object containing multiple key:value pairs, invert returns an object containing value:key pairs", () => {
expect(invert({ a: 1, apple: "red", month: "September" })).toEqual({
1: "a",
red: "apple",
September: "month",
});
});
12 changes: 12 additions & 0 deletions Sprint-2/stretch/count-words.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@

3. Order the results to find out which word is the most common in the input
*/
function countWords(string) {
const noPunctuation = string.replace(/[.,!?]/g,"");
const array = noPunctuation.toLowerCase().split(" ");


let object = {};
array.forEach((word) => {
object[word] = (object[word] || 0) + 1;
});
return Object.values(object).sort((a,b)=>b-a)/////?????? stuck on how to order it
}
console.log(countWords("You, and .me? and you me me"));
14 changes: 11 additions & 3 deletions Sprint-2/stretch/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
// into smaller functions using the stages above

function calculateMode(list) {
// track frequency of each value
let freqs = elementFrequency(list);

return maxFrequency(freqs);
}

function elementFrequency(list) {
let freqs = new Map();

for (let num of list) {
Expand All @@ -19,8 +24,10 @@ function calculateMode(list) {

freqs.set(num, (freqs.get(num) || 0) + 1);
}
return freqs;
}

// Find the value with the highest frequency
function maxFrequency(freqs) {
let maxFreq = 0;
let mode;
for (let [num, freq] of freqs) {
Expand All @@ -29,8 +36,9 @@ function calculateMode(list) {
maxFreq = freq;
}
}

return maxFreq === 0 ? NaN : mode;
}



module.exports = calculateMode;
2 changes: 1 addition & 1 deletion Sprint-2/stretch/mode.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const calculateMode = require("./mode.js");
// Example:
// Given [2,4,1,2,3,2,1]
// When calculateMode is called on [2,4,1,2,3,2,1]
// Then it should return 2 */
// Then it should return 2 */

describe("calculateMode()", () => {
test("returns the most frequent number in an array", () => {
Expand Down
23 changes: 22 additions & 1 deletion Sprint-2/stretch/till.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ function totalTill(till) {
let total = 0;

for (const [coin, quantity] of Object.entries(till)) {
total += coin * quantity;
const coinWithOutTrailingP = coin.slice(0, -1);
const numberCoin = +coinWithOutTrailingP;

total += numberCoin * quantity;
}

return `£${total / 100}`;
Expand All @@ -21,11 +24,29 @@ const till = {
"20p": 10,
};
const totalAmount = totalTill(till);
console.log(totalAmount);

// a) What is the target output when totalTill is called with the till object
//£4.40

// b) Why do we need to use Object.entries inside the for...of loop in this function?
// to get the key value pairs

// c) What does coin * quantity evaluate to inside the for...of loop?
//NaN

// d) Write a test for this function to check it works and then fix the implementation of totalTill
// const till1 = {
// "1p": 10,
// "5p": 6,
// "50p": 4,
// "20p": 10,
// };

const currentOutput = totalTill(till);
const expectedOutput = "£4.4";

console.assert(
currentOutput === expectedOutput,
`Expected ${expectedOutput}, got ${currentOutput}`
);