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
7 changes: 6 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
// Predict and explain first...
/*
Objects don't use indexes like arrays. If we try to access address[0], it returns undefined because address
is an object, not an array. Objects store values using keys (property names). To retrieve a value, we can use
either dot notation, such as address.houseNumber, or bracket notation, such as address["houseNumber"].

*/
// This code should log out the houseNumber from the address object
// but it isn't working...
// Fix anything that isn't working
Expand All @@ -12,4 +17,4 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
12 changes: 8 additions & 4 deletions Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Predict and explain first...

/*
author is an object. The original code tries to use a for...of loop directly on the object, but normal objects
cannot be directly iterated using for...of. Since we only want the property values, we can use Object.values(author)
to get all the values from the object.
*/
// 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



const author = {
firstName: "Zadie",
lastName: "Smith",
occupation: "writer",
age: 40,
alive: true,
};
console.log(Object.values(author));

for (const value of author) {
console.log(value);
}
13 changes: 9 additions & 4 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
// Predict and explain first...

/*
The printing of title and serves is absolutely correct, but to print ingredients on a new line isn't correct.
To retrieve values, we use Object.values(recipe). To print ${recipe} isn't the right way. We can use a for...of
loop to print each ingredient on a new line.
*/
// 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?
Expand All @@ -10,6 +14,7 @@ const recipe = {
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`);
for(let values of Object.values(recipe.ingredients)){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Is it necessary to use Object.values() on line 18?

  • If we do not need to reassign a value to the loop variable, common practice is to declare it using const.

console.log(values);
}
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, item) {
if (typeof obj !== "object" || obj === null || Array.isArray(obj)){
return false;
}
let getKey = Object.keys(obj);
for (let element of getKey){
if(element === item){
return true
}
}
return false;
}
Comment on lines +5 to +12

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This works.

Do check out Object.hasOwn() and also
use AI to find out the trade-off among different ways to check if an object contains a particular key.


module.exports = contains;
18 changes: 15 additions & 3 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,37 @@ E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/

// Acceptance criteria:

// 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("return true if object's property exists otherwise false", () => {
expect(contains({name: "maryam", city: "Derby"}, "city")).toBe(true);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("return false if object is empty", () => {
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("return true if object's property contains the property", () => {
expect(contains({name: "maryam", city: "Derby"}, "city")).toBe(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false
test("return false if property is non-existent", () => {
expect(contains({name: "maryam", city: "Derby"}, "age")).toBe(false);
});

// Given invalid parameters like an array
// When passed to contains
// Then it should return false or throw an error
test("return false if it's not an object", () => {
expect(contains([], 6)).toBe(false);
});
Comment on lines +45 to +47

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a function does not test if the first argument is an array, contains([], 6) could also return false simply because 6 is not a key of the empty array.

A proper test should use a non-empty array along with a valid
key to ensure the function returns false specifically because the first argument is an array, not because the key is missing.

4 changes: 3 additions & 1 deletion Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
function createLookup() {
function createLookup(arr) {
// implementation here
const obj = Object.fromEntries(arr);
return obj;
}

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", () =>{
expect(createLookup([['US', 'USD'], ['CA', 'CAD'], ['PAK', 'PKR'], ['SAUDI', 'RIYAL']])).toEqual({
'US': 'USD',
'CA': 'CAD',
'PAK' : 'PKR',
'SAUDI' : 'RIYAL'
});
});
test("the array is empty", () =>{
expect(createLookup([])).toEqual({});
});
test("creates a country currency code lookup for single code", () =>{
expect(createLookup([['Uk','POUND']])).toEqual({'Uk': 'POUND'});
});

/*

Expand Down
38 changes: 35 additions & 3 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,48 @@
function parseQueryString(queryString) {
const queryParams = {};

if (queryString.length === 0) {
return queryParams;
}

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

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

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

let key;
let value;

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

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

key = decodeURIComponent(key);
value = decodeURIComponent(value);

if (Object.prototype.hasOwnProperty.call(queryParams, key)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also use Object.hasOwn().

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

return queryParams;
}

module.exports = parseQueryString;
module.exports = parseQueryString;
30 changes: 30 additions & 0 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,33 @@ test("should store values of a key in an array when the key has 2 or more values
foo: "bar",
});
});
// Multiple normal key-value pairs
test("should parse multiple key-value pairs", () => {
expect(parseQueryString("name=Maryam&age=25")).toEqual({
name: "Maryam",
age: "25",
});
});

test("should return empty object for empty query string", () => {
expect(parseQueryString("")).toEqual({});
});

test("should replace multiple '+' characters with spaces", () => {
expect(parseQueryString("name=John+Michael+Doe")).toEqual({
name: "John Michael Doe",
});
});

test("should decode encoded keys and values", () => {
expect(parseQueryString("hello%20world=good%20morning")).toEqual({
"hello world": "good morning",
});
});

test("should ignore multiple empty key-value pairs", () => {
expect(parseQueryString("name=Maryam&&&age=25&&&")).toEqual({
name: "Maryam",
age: "25",
});
});
15 changes: 13 additions & 2 deletions Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
function tally() {}

function tally(arr) {
const result = arr.reduce((obj, item) => {
if (obj [item]) {
obj[item] = obj[item] + 1;
}
else{
obj[item] = 1;
}
return obj;
}, {});
Comment on lines +2 to +10

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the following function call returns the value you expect?

tally(["toString", "toString"]);

Suggestion:

  • Look up an approach to create an empty object with no inherited properties, or
  • use Object.hasOwn()


return result;
}
module.exports = tally;
38 changes: 37 additions & 1 deletion Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,52 @@ 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("when an array is passed, it should return an object containing the count for each unique item", () => {
expect(tally(["banana", "apple", "cherry", "apple", "cherry"])).toEqual({
banana: 1,
apple: 2,
cherry: 2
});
});

// 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("should return the count for duplicate items", () => {
expect(tally(["apple", "apple", "apple", "banana"])).toEqual({
apple: 3,
banana: 1
});
});

// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
test("should throw an error when passed a string", () => {
expect(() => tally("apple")).toThrow();
});

// Case: array containing numbers
test("should count occurrences of numbers", () => {
expect(tally([1, 2, 2, 3, 3, 3])).toEqual({
1: 1,
2: 2,
3: 3
});
});

// Case: array containing mixed data types
test("should count occurrences of mixed items", () => {
expect(tally(["apple", 1, "apple", 1, "banana"])).toEqual({
apple: 2,
1: 2,
banana: 1
});
});
24 changes: 22 additions & 2 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,40 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

// 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 all the key-value pairs of an object. We need it to get both the keys and values.

// d) Explain why the current return value is different from the target output
/*Because we are not swapping it correctly. We haven't handled the key correctly,
so it's giving us the "key" keyword, not the way we need it.
Plus, we aren't storing each value correctly, so the previous value gets overwritten
when there are multiple values.*/

// e) Fix the implementation of invert (and write tests to prove it's fixed!)
//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;
}
const obj = {x : 10, y : 20};
console.log(invert(obj));
Loading