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
3 changes: 2 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ const address = {
postcode: "XYZ 123",
};

console.log(`My house number is ${address[0]}`);
console.log(`My house number is ${address.houseNumber}`);
//
5 changes: 4 additions & 1 deletion Sprint-2/debug/author.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

// 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
// Because objects are not iterable, so they need to be converted to arrays so as we can loop through them so, with Object.entries() method as this method converts the objects into key and value pairs within an array.

const author = {
firstName: "Zadie",
Expand All @@ -11,6 +12,8 @@ const author = {
alive: true,
};

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

// so the first recipe.title prints the correct title, the second recipe.serves also prints the correct number of serves but the last recipe in template literals will throw error or perhaps undefined as recipe is an object and needs different method to access it. so basically inorder to access the key and values inside an object, it should be accessed by using object name followed by a dot and one of the values or keys inside it (whatever we want to use).
// 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 @@ -9,7 +9,14 @@ const recipe = {
serves: 2,
ingredients: ["olive oil", "tomatoes", "salt", "pepper"],
};

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
let recipeEntries = Object.entries(recipe);
for (const [reicpeKey, recipeValue] of recipeEntries) {
if (reicpeKey === "ingredients") {
console.log("Ingredients:");
for (const ingredient of recipeValue) {
console.log(ingredient);
}
} else {
console.log(`${reicpeKey}: ${recipeValue}`);
}
}
9 changes: 8 additions & 1 deletion Sprint-2/implement/contains.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
function contains() {}
function contains(obj, key) {
for (const currentKey of Object.keys(obj)) {
if (currentKey === key) {
return true;
}
}
return false;
}

module.exports = contains;
20 changes: 17 additions & 3 deletions Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,40 @@ as the object contains a key of 'a'
E.g. contains({a: 1, b: 2}, 'c') // returns false
as the object doesn't contains a key of 'c'
*/
console.log("THIS IS THE REAL contains.js FILE");

// 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("contains both existent and non-existent properties, returns true if existent or false if otherwise", () => {
expect(contains({ z: 2, e: 5 }, "a")).toBe(false);
expect(contains({ y: 6, x: 3, s: 5 }, "s")).toBe(true);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("returns false for an empty object", () => {
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 object contains the property", () => {
expect(contains({ a: 1, b: 3 }, "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 object does not contain the property", () => {
expect(contains({ w: 1, x: 8 }, "r")).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 invalid parameters like an array", () => {
expect(contains([], "a")).toBe(false);
});
Loading