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
20 changes: 20 additions & 0 deletions Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,23 @@ const address = {
};

console.log(`My house number is ${address[0]}`);

//my prediction is "undefined" because objects use names. When we js executes this code it is seeing address[0], it looks
//for 0 in objects, won't find it and will return , undefined.

// i have run the code in devtools and got the "My house number is undefined" message.

//the fixed code

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

console.log(`My house number is ${address.houseNumber}`);



16 changes: 16 additions & 0 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,19 @@ const author = {
for (const value of author) {
console.log(value);
}

// this code will throw an error because on line for 14 it is trying to loop a plain object.

const author = {
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};

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

//we fix this by using Object.values, i.e. turning plain object in an array.
16 changes: 16 additions & 0 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ const recipe = {
console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);

//Prediction:
//output will be, [object Object]. Because recipe is an object, when js converts objects to a string (template literals can only insert strings) it calls on toString() method, which
//by default returns [Object Object]. We can fix this code by using join(\n) method.

//fixed code:

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

console.log(`${recipe.title} serves ${recipe.serves}
Ingredients:
${recipe.ingredients.join("\n")}`);
8 changes: 7 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
function contains() {}
function contains(obj, property) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)) {
return false;
}

return Object.hasOwn(obj, property);
}

module.exports = contains;
30 changes: 29 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,47 @@ as the object doesn't contains a key of 'c'
// When passed an object and a property name
// Then it should return true if the object contains the property, false otherwise

test("returns true if the object contains the property", () => {
const object = { a: 1, b: 2 };

expect(contains(object, "a")).toBe(true);
});

test("returns false if the object does not contain the property", () => {
const object = { a: 1, b: 2 };

expect(contains(object, "c")).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", () => {
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 property exists", () => {
expect(contains({ a: 1, b: 2 }, "a")).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 property does not exist", () => {
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("returns false for an array", () => {
expect(contains(["a", "b"], "0")).toBe(false);
});
10 changes: 8 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
function createLookup() {
// implementation here
function createLookup(countryCurrencyPairs) {
const lookup = {};

for (const [countryCode, currencyCode] of countryCurrencyPairs) {
lookup[countryCode] = currencyCode;
}

return lookup;
}

module.exports = createLookup;
15 changes: 14 additions & 1 deletion Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
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 countryCurrencyPairs = [
["US", "USD"],
["CA", "CAD"],
["GB", "GBP"],
];
const result = createLookup(countryCurrencyPairs);

expect(result).toEqual({
US: "USD",
CA: "CAD",
GB: "GBP",
});
});

/*

Expand Down
36 changes: 33 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,43 @@
function parseQueryString(queryString) {
const queryParams = {};
if (queryString.length === 0) {

if (queryString === "") {
return queryParams;
}

const keyValuePairs = queryString.split("&");

for (const pair of keyValuePairs) {
const [key, value] = pair.split("=");
queryParams[key] = value;

if (pair === "") continue;


const equalsIndex = pair.indexOf("=");

let key, value;

if (equalsIndex === -1) {
key = pair;
value = "";
} else {
key = pair.slice(0, equalsIndex);
value = pair.slice(equalsIndex + 1);
}


key = decodeURIComponent(key.replace(/\+/g, " "));
value = decodeURIComponent(value.replace(/\+/g, " "));


if (queryParams.hasOwnProperty(key)) {
if (Array.isArray(queryParams[key])) {
queryParams[key].push(value);
} else {
queryParams[key] = [queryParams[key], value];
}
} else {
queryParams[key] = value;
}
}

return queryParams;
Expand Down
23 changes: 17 additions & 6 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,23 @@ test("should replace '+' by ' '", () => {
});
});

// Stretch exercise: Handling query strings that contain identical keys
test("returns empty object for empty query", () => {
expect(parseQueryString("")).toEqual({});
});

test("ignores multiple empty pairs", () => {
expect(parseQueryString("&&&&")).toEqual({});
});

// 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",
test("decodes encoded spaces", () => {
expect(parseQueryString("name=John%20Doe")).toEqual({
name: "John Doe",
});
});

test("keeps all '=' in values", () => {
expect(parseQueryString("expr=a=b=c=d")).toEqual({
expr: "a=b=c=d",
});
});

18 changes: 17 additions & 1 deletion Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
function tally() {}
function tally(items) {
if (!Array.isArray(items)) {
throw new Error("Input must be an array");
}

const counts = {};

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

return counts;
}

module.exports = tally;
27 changes: 26 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,40 @@ const tally = require("./tally.js");
// When passed an array of items
// Then it should return an object containing the count for each unique item

test("returns an object containing the count for each unique item", () => {
expect(tally([1, 2, 2, 3, 3, 3])).toEqual({
1: 1,
2: 2,
3: 3,
});
});

// 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 duplicate items", () => {
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("throws an error for invalid input", () => {
expect(() => tally("abc")).toThrow();
});

49 changes: 49 additions & 0 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,61 @@ function invert(obj) {

// 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?

//Object.entries() returns an array of an object's key-value pairs, and it is needed in this program
//so the for...of loop can access both each key and its corresponding value to swap them.

// d) Explain why the current return value is different from the target output

//It is different because invertObj.key creates a property literally calley "key". If we want to use the value of
//a variable as the property name, (invertedObj[value] = key) must be used instead.

// 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;
}

return invertedObj;
}



describe("invert", () => {
test("inverts an empty object", () => {
expect(invert({})).toEqual({});
});

test("inverts a single property", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});

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

test("works with other values", () => {
expect(invert({ x: 10, y: 20 })).toEqual({
10: "x",
20: "y",
});
});
});

Loading