Skip to content
Closed
2 changes: 2 additions & 0 deletions Sprint-1/fix/median.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ function calculateMedian(list) {
}

module.exports = calculateMedian;


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]}`);
// Accessing the houseNumber property from the address object
console.log(`My house number is ${address.houseNumber}`);

24 changes: 22 additions & 2 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
// 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

//author contains properties (key-value pairs), but a simple for...of loop treats it as if it's an array of values, which it isn't.



// The for...in loop treats `author` as an object and iterates over its keys.
// We then use those keys to log out their corresponding values.

const author = {
firstName: "Zadie",
lastName: "Smith",
Expand All @@ -11,6 +18,19 @@ const author = {
alive: true,
};

for (const value of author) {
console.log(value);

// Iterate over the object's keys and log their values
for (const value in author) {
// Logs the value associated with each key
console.log(author[value]);
}




// ================= access to its keys ==============

for (const key in author){
console.log(key);
// console.log(key, author[key]);
}
29 changes: 20 additions & 9 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@
// Each ingredient should be logged on a new line
// How can you fix it?

const recipe = {
title: "bruschetta",
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
// const recipe = {
// title: "bruschetta",
// serves: 2,
// ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
// };

// console.log(`${recipe.title} serves ${recipe.serves}`);

// let ingredients = recipe["ingredients"];

// for (let i = 0; i < ingredients.length; i++) {
// console.log(ingredients[i]);
// }

// ==================== one line with join method ====================

console.log(`${recipe.title} serves ${recipe.serves}`);

// Display the ingredients in one line with a new line (\n) between each ingredient
console.log(recipe.ingredients.join("\n"));
13 changes: 12 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function contains() {}
function contains(obj, property) {
// check if it is an Array
if (typeof obj !== "object" || obj === null || Array.isArray(obj) || obj === undefined ) {
return false;
}
for (let key in obj) {
if (key === property) {
return true;
}
}
return false;
}

module.exports = contains;
37 changes: 36 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ 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");

// Given an object with properties
// When passed to contains with an existing property name
Expand All @@ -33,3 +32,39 @@ test.todo("contains on empty object returns false");
// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error



describe("contains function", () => {
// When the object is empty, it should return false
test("should return false for an empty object", () => {
expect(contains({}, "a")).toBe(false);
});

// When the key exists in the object, it should return true
test("should return true if the object contains the property", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
});

// When the key does not exist in the object, it should return false
test("should return false if the object does not contain the property", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// When an array is passed as a parameter, it should return false
test("should return false for invalid parameters like arrays", () => {
expect(contains([1, 2, 3], "a")).toBe(false);
});

// It should handle null and undefined inputs gracefully (throw an error)
test("should handle null or undefined inputs gracefully", () => {
expect(contains(null, "a")).toBe(false);
expect(contains(undefined, "a")).toBe(false);
});
});


test.todo("contains on empty object returns false");



23 changes: 21 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
function createLookup() {
// implementation here
function createLookup(input) {
const objInput = {};

// Validate input is an array of arrays
if (!Array.isArray(input)) {
throw new Error("Input must be an array of arrays.");
}

// Populate the object
for (const pair of input) {
if (!Array.isArray(pair) || pair.length !== 2) {
throw new Error(
"Each element of the input array must be a key-value pair."
);
}

const [key, value] = pair;
objInput[key] = value;
}

return objInput;
}

module.exports = createLookup;
21 changes: 20 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
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 multiple codes");

/*

Expand Down Expand Up @@ -33,3 +33,22 @@ It should return:
'CA': 'CAD'
}
*/


// The tests below already cover multiple codes functionality.
test("Given empty array", () => {
expect(createLookup([])).toEqual({});
});

test("Given single array", () => {
expect(createLookup([["US", "USD"]])).toEqual({ US: "USD" });
});

test("Given multiple array", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({ US: "USD", CA: "CAD" });
});
7 changes: 5 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ function parseQueryString(queryString) {
return queryParams;
}
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
const [key, ...valueParts] = pair.split("=");
const value =
valueParts.length > 0
? decodeURIComponent(valueParts.join("="))
: undefined;
queryParams[key] = value;
}

Expand Down
76 changes: 74 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,82 @@
// 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");

test("parses querystring values containing =", () => {
expect(parseQueryString("equation=x=y+1")).toEqual({
"equation": "x=y+1",
equation: "x=y+1",
});
});

describe("parseQueryString", () => {
test("parses a simple query string", () => {
expect(parseQueryString("name=Sajad")).toEqual({
name: "Sajad",
});
});

test("handles multiple key-value pairs", () => {
expect(parseQueryString("name=Sajad&age=25")).toEqual({
name: "Sajad",
age: "25",
});
});

test("handles empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

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

test("parses query string with duplicate keys, keeps the last value", () => {
expect(parseQueryString("name=Sajad&name=Ali")).toEqual({
name: "Ali",
});
});

test("handles keys with special characters", () => {
expect(parseQueryString("key@name=value")).toEqual({
"key@name": "value",
});
});

test("handles values with special characters", () => {
expect(parseQueryString("name=Sonia@25")).toEqual({
name: "Sonia@25",
});
});

test("handles query string with only a key", () => {
expect(parseQueryString("name")).toEqual({
name: undefined,
});
});

test("handles query string with no '=' or '&'", () => {
expect(parseQueryString("name&age")).toEqual({
name: undefined,
age: undefined,
});
});

test("handles query string with mixed valid and invalid parts", () => {
expect(parseQueryString("name=Sonia&age&=25")).toEqual({
name: "Sonia",
age: undefined,
"": "25",
});
});

test("handles special characters correctly", () => {
const specialQueryString = "name=%20G%C3%BCnter";
expect(parseQueryString(specialQueryString)).toEqual({
name: " Günter",
});
});
});
22 changes: 21 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
function tally() {}
function tally(array) {
// check if input is an array
if (!Array.isArray(array)) {
return { error: "Input must be an array" };
}

// create an empty object to store counts
let counts = {};

// Loop through the array and count items
for (let item of array) {
if (counts[item]) {
//the loop checks if the current item already exists as a key in the counts object.
counts[item] += 1; // Increase count if item exists
} else {
counts[item] = 1; //Initialize count if item is new
}
}

return counts;
}

module.exports = tally;
14 changes: 12 additions & 2 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,22 @@ 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.todo("tally on an empty array returns an empty object");// Then it should return an empty object
test("Given an empty array", () =>{
expect(tally([])).toEqual({});
});

// Given an array with duplicate items
// When passed to tally
// Then it should return counts for each unique item

test("Given an array with duplicate items", () =>{
expect(tally(['a'])).toEqual({a: 1});
expect(tally(['a', 'a', 'a'])).toEqual({a: 3 });
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("Given an invalid input like a string", () =>{
expect(tally(1)).toEqual({ error: "Input must be an array"});
});
Loading
Loading