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
10 changes: 8 additions & 2 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// Predict and explain first...
// undefind is expect. because index access is for arrays not for object.
// for object you mention the key to get the value.


// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working


const address = {
houseNumber: 42,
street: "Imaginary Road",
city: "Manchester",
country: "England",
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
// either access via (.) properties
console.log(`My house number is ${address.houseNumber}`);
//OR prakets notation
console.log(`My house number is ${address['houseNumber']}`);
17 changes: 16 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
// 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

// i know it will not work because we cant access these values this way.
// what i learned new is the 3 properties

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

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

for (const key in author) {
console.log(author[key]);
}
for (const value of Object.values(author)) {
console.log(value);
}
for (const key of Object.keys(author)) {
console.log(author[key]);
}
for (const [key, value] of Object.entries(author)) {
console.log(value); // or console.log(`${key}: ${value}`)
}
10 changes: 8 additions & 2 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// 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?
//i tried to use map but could not write the code itself i checked it online. AI help me with another solution.
//.join

const recipe = {
title: "bruschetta",
Expand All @@ -11,5 +13,9 @@ const recipe = {
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
ingredients:
${recipe.ingredients.join("\n")}`);

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe.ingredients.map((ingredient) => ` ${ingredient}`).join("\n")}`);
10 changes: 9 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
function contains() {}
function contains(obj, propertyName) {
// Check if obj is a valid plain object (not array, not null, not other object types)
if (obj === null || typeof obj !== "object" || Array.isArray(obj)) {
return false;
}

// Use hasOwnProperty to check if the property exists directly on the object
return Object.prototype.hasOwnProperty.call(obj, propertyName);
}

module.exports = contains;
44 changes: 44 additions & 0 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,47 @@ 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
// const contains = require("./contains.js");

// Given an empty object
// When passed to contains
// Then it should return false
test("contains on empty object 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("contains with existing property returns true", () => {
expect(contains({ a: 1, b: 2 }, "a")).toBe(true);
expect(contains({ a: 1, b: 2 }, "b")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("contains with non-existent property returns false", () => {
expect(contains({ a: 1, b: 2 }, "c")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("contains with invalid parameters returns false", () => {
// Test with array
expect(contains([1, 2, 3], "0")).toBe(false); // arrays are objects but we treat them as invalid for this use case
expect(contains([1, 2, 3], "length")).toBe(false);

// Test with null
expect(contains(null, "a")).toBe(false);

// Test with undefined
expect(contains(undefined, "a")).toBe(false);

// Test with string
expect(contains("hello", "0")).toBe(false);

// Test with number
expect(contains(123, "toString")).toBe(false);
});
12 changes: 10 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = {};

for (const pair of countryCurrencyPairs) {
const countryCode = pair[0];
const currencyCode = pair[1];
lookup[countryCode] = currencyCode;
}

return lookup;
}

module.exports = createLookup;
13 changes: 13 additions & 0 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,16 @@ It should return:
'CA': 'CAD'
}
*/

test("creates a country currency code lookup for multiple codes", () => {
const countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
];
const result = createLookup(countryCurrencyPairs);

expect(result).toEqual({
US: "USD",
CA: "CAD",
});
});
10 changes: 8 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;
// Split only on the first '=' to handle values containing '='
const equalsIndex = pair.indexOf("=");
if (equalsIndex !== -1) {
const key = pair.substring(0, equalsIndex);
const value = pair.substring(equalsIndex + 1);
queryParams[key] = value;
}
// If there's no '=', treat the whole string as key with undefined value
}

return queryParams;
Expand Down
18 changes: 16 additions & 2 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,24 @@
// 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("handles multiple parameters with special characters", () => {
expect(parseQueryString("a=1&b=2&equation=x=y+1")).toEqual({
a: "1",
b: "2",
equation: "x=y+1",
});
});

test("handles empty query string", () => {
expect(parseQueryString("")).toEqual({});
});
14 changes: 13 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function tally() {}
function tally(items) {
const counts = {};

for (const item of items) {
if (counts[item] === undefined) {
counts[item] = 1;
} else {
counts[item]++;
}
}

return counts;
}

module.exports = tally;
15 changes: 14 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const tally = require("./tally.js");
//const tally = require("./tally.js");

/**
* tally array
Expand Down Expand Up @@ -32,3 +32,16 @@ test.todo("tally on an empty array returns an empty object");
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
const tally = require("./tally.js");

test("tally on an empty array returns an empty object", () => {
expect(tally([])).toEqual({});
});

test("tally counts duplicate items correctly", () => {
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
});

test("tally counts multiple unique items", () => {
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
});
27 changes: 20 additions & 7 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}

function invert(obj) {
const invertedObj = {};
// function invert(obj) {
// const invertedObj = {};

for (const [key, value] of Object.entries(obj)) {
invertedObj.key = value;
}
// for (const [key, value] of Object.entries(obj)) {
// invertedObj.key = value;
// }

return invertedObj;
}
// return invertedObj;
// }

// a) What is the current return value when invert is called with { a : 1 }

Expand All @@ -27,3 +27,16 @@ function invert(obj) {
// d) Explain why the current return value is different from the target output

// 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; // Use value as key, original key as value
}

return invertedObj;
}

module.exports = invert;
13 changes: 13 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const invert = require("./invert.js");

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

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

test("inverts object with string values", () => {
expect(invert({ x: 10, y: 20 })).toEqual({ 10: "x", 20: "y" });
});
10 changes: 10 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name": "module-data-groups",
"version": "1.0.0",
"scripts": {
"test": "jest"
},
"devDependencies": {
"jest": "^30.2.0"
}
}
19 changes: 19 additions & 0 deletions prep/100.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const ingredients = ["olive oil", "tomatoes", "garlic", "onion", "carrot"];
let ingredientsCopy = ingredients;
ingredientsCopy.push("pasta", "salt", "pepper");
const otherRecipe = [
"olive oil",
"tomatoes",
"garlic",
"onion",
"carrot",
"pasta",
"salt",
"pepper",
];

console.log(ingredients === ingredientsCopy);
console.log(ingredients === otherRecipe);
console.log(otherRecipe === ingredientsCopy);
console.log(otherRecipe.length === ingredients.length);

26 changes: 26 additions & 0 deletions prep/600.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function collectNumbers(list) {
const numbersOnly = [];
for (const item of list) {
if (item === "string") {
numbersOnly.push(item);
}
}
return numbersOnly;
}

test("only collects numbers in the array", () => {
const currentOutput = collectNumbers([
10.1,
"hello",
6.1,
8.0,
9.7,
10.1,
"hi",
3.5,
"oops",
]);
const targetOutput = [10.1, 6.1, 8.0, 9.7, 10.1, 3.5];

expect(currentOutput).toEqual(targetOutput);
});
26 changes: 26 additions & 0 deletions prep/600.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
function collectNumbers(list) {
const numbersOnly = [];
for (const item of list) {
if (typeof item === "number") {
numbersOnly.push(item);
}
}
return numbersOnly;
}

test("only collects numbers in the array", () => {
const currentOutput = collectNumbers([
10.1,
"hello",
6.1,
8.0,
9.7,
10.1,
"hi",
3.5,
"oops",
]);
const targetOutput = [10.1, 6.1, 8.0, 9.7, 10.1, 3.5];

expect(currentOutput).toEqual(targetOutput);
});
Loading
Loading