From 966d236990cf5b7cb7f51ff9fe3e169ad68e54f2 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Tue, 10 Nov 2026 00:27:57 +0000 Subject: [PATCH 01/12] fix address to working --- Sprint-2/debug/address.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 940a6af832..36d2f865db 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -12,4 +12,4 @@ const address = { postcode: "XYZ 123", }; -console.log(`My house number is ${address[0]}`); +console.log(`My house number is ${address.houseNumber}`); From 47f9997294c4b030054f67e4450433555ac34b8f Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Tue, 10 Nov 2026 00:41:49 +0000 Subject: [PATCH 02/12] Fix object iteration using for...in loop --- Sprint-2/debug/author.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 8c2125977d..a8334d54e2 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,6 +11,9 @@ const author = { alive: true, }; -for (const value of author) { - console.log(value); +for (const value in author) { + console.log(author[value]); } +//author is Object and it is not iterable +//for...of loop excepts array and string but it not working with object type +//we should Use for... in for object From 8eade996909a2a67aa29c00b6e5b6e9c8731beab Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Tue, 10 Nov 2026 00:47:50 +0000 Subject: [PATCH 03/12] Fix recipe ingredients output formatting --- Sprint-2/debug/recipe.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 6cbdd22cdc..691a73cdd1 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -2,7 +2,7 @@ // 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? +// How can you fix it? The join() method converts an array into a string. const recipe = { title: "bruschetta", @@ -12,4 +12,4 @@ const recipe = { console.log(`${recipe.title} serves ${recipe.serves} ingredients: -${recipe}`); +${recipe.ingredients.join("\n")}`); From cf7eb8c1139671dd0d26e9cc1e1e180aaafebb99 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Wed, 11 Nov 2026 21:02:10 +0000 Subject: [PATCH 04/12] solve contains and querystring --- Sprint-2/implement/contains.js | 16 +++++- Sprint-2/implement/contains.test.js | 73 ++++++++++++++++++++------ Sprint-2/implement/querystring.js | 36 +++++++++++-- Sprint-2/implement/querystring.test.js | 2 +- 4 files changed, 105 insertions(+), 22 deletions(-) diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cd779308a0..cefa841127 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,3 +1,17 @@ -function contains() {} +function contains(cont, str) { + // if (cont === "") { + // return false; + // } + if (typeof cont !== "object" || cont === null || Array.isArray(cont)) { + throw new Error("contains requires an object"); + } + const keyArray = Object.keys(cont); + for (const item of keyArray) { + if (item === str) { + return true; + } + } + return false; +} module.exports = contains; diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 719272787d..362f87a803 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -16,25 +16,66 @@ as an array isn't an object // 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 - // Given an empty object // When passed to contains // Then it should return false -test.todo("contains on empty object returns false"); +describe("contains", () => { + test("given an empty object, returns false", function () { + const input = contains({}, ""); + const currentOutput = input; + const targetOutput = false; -// Given an object with properties -// When passed to contains with an existing property name -// Then it should return true + expect(currentOutput).toEqual(targetOutput); + }); + // 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("given an object, returns true as the object contains a key of 'a'", function () { + const input = contains({ a: 1, b: 2 }, "a"); + const currentOutput = input; + const targetOutput = true; -// Given an object with properties -// When passed to contains with a non-existent property name -// Then it should return false + expect(currentOutput).toEqual(targetOutput); + }); + // Given an object with properties + // When passed to contains with a non-existent property name + // Then it should return false + test("given an object, returns true as the object contains a key of 'a'", function () { + const input = contains({ a: 1, b: 2 }, "c"); + const currentOutput = input; + const targetOutput = false; -// Given a value that isn't an object - an array, a string, a number, -// null, or no argument at all -// When passed to contains -// Then it should throw Error("contains requires an object") -// (careful: typeof [] and typeof null are both "object") + expect(currentOutput).toEqual(targetOutput); + }); + // Given a value that isn't an object - an array, a string, a number, + // null, or no argument at all + // When passed to contains + // Then it should throw Error("contains requires an object") + // (careful: typeof [] and typeof null are both "object") + it("throws when given an array containing a value that isn't an object", () => { + expect(() => contains(["banana"])).toThrow( + new Error("contains requires an object") + ); + }); + it("throws when given an array containing a value that isn't an object", () => { + expect(() => contains(123)).toThrow( + new Error("contains requires an object") + ); + }); + + it("throws when given an array containing a value that isn't an object", () => { + expect(() => contains(null)).toThrow( + new Error("contains requires an object") + ); + }); + + it("throws when given an array containing a value that isn't an object", () => { + expect(() => contains()).toThrow(new Error("contains requires an object")); + }); + + it("throws when given an empty array", () => { + expect(() => contains([])).toThrow( + new Error("contains requires an object") + ); + }); +}); diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 45ec4e5f37..7985f3de8d 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,16 +1,44 @@ function parseQueryString(queryString) { const queryParams = {}; + if (queryString.length === 0) { return queryParams; } - const keyValuePairs = queryString.split("&"); + + const newQueryStr = queryString.replaceAll("+", " "); + const keyValuePairs = newQueryStr.split("&"); for (const pair of keyValuePairs) { - const [key, value] = pair.split("="); - queryParams[key] = value; + if (pair.length === 0) { + continue; + } + + const separateIndex = pair.indexOf("="); + + let key; + let value; + + if (separateIndex === -1) { + key = pair; + value = ""; + } else { + key = pair.slice(0, separateIndex); + value = pair.slice(separateIndex + 1); + } + key = decodeURIComponent(key); + value = decodeURIComponent(value); + + if (Object.hasOwn(queryParams, key)) { + if (Array.isArray(queryParams[key])) { + queryParams[key].push(value); + } else { + queryParams[key] = [queryParams[key], value]; + } + } else { + queryParams[key] = value; + } } return queryParams; } - module.exports = parseQueryString; diff --git a/Sprint-2/implement/querystring.test.js b/Sprint-2/implement/querystring.test.js index 328b8df617..e9e0e9abd7 100644 --- a/Sprint-2/implement/querystring.test.js +++ b/Sprint-2/implement/querystring.test.js @@ -3,7 +3,7 @@ // Below are some test cases the implementation doesn't handle well. // Fix the implementation for these tests, 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("should parse values containing '='", () => { expect(parseQueryString("equation=a=b-2")).toEqual({ From 2d99aef90e39da9efd724e9df8e44ebdc69666bb Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Wed, 11 Nov 2026 21:37:28 +0000 Subject: [PATCH 05/12] write lookup function and test --- Sprint-2/implement/lookup.js | 15 ++++++++++++--- Sprint-2/implement/lookup.test.js | 14 ++++++++++++-- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index a6746e07f5..32c766fad8 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -1,5 +1,14 @@ -function createLookup() { - // implementation here +function createLookup(countryCurrencyPairs) { + const lookUp = {}; + for (const item of countryCurrencyPairs) { + lookUp[item[0]] = item[1]; + } + return lookUp; } - +console.log( + createLookup([ + ["US", "USD"], + ["CA", "CAD"], + ]) +); module.exports = createLookup; diff --git a/Sprint-2/implement/lookup.test.js b/Sprint-2/implement/lookup.test.js index 547e06c5a7..18daff60c0 100644 --- a/Sprint-2/implement/lookup.test.js +++ b/Sprint-2/implement/lookup.test.js @@ -1,7 +1,17 @@ const createLookup = require("./lookup.js"); -test.todo("creates a country currency code lookup for multiple codes"); - +describe("lookup", () => { + test("creates a country currency code lookup for multiple codes", function () { + const input = [ + ["US", "USD"], + ["CA", "CAD"], + ]; + const currentOutput = createLookup(input); + const targetOutput = { US: "USD", CA: "CAD" }; + + expect(currentOutput).toEqual(targetOutput); + }); +}); /* Create a lookup object of key value pairs from an array of code pairs From 2efb219444181fad2bd26cd85a0fe47931533480 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Wed, 11 Nov 2026 23:48:57 +0000 Subject: [PATCH 06/12] Add tally function with input validation and error handling --- Sprint-2/implement/tally.js | 20 +++++++++++++++- Sprint-2/implement/tally.test.js | 41 ++++++++++++++++++++++++++------ 2 files changed, 53 insertions(+), 8 deletions(-) diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index f47321812e..b6b0c78427 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,3 +1,21 @@ -function tally() {} +function tally(arr) { + const tallyObj = {}; + if (!Array.isArray(arr)) { + throw new Error("tally requires an array"); + } + if (arr.length === 0) { + return {}; + } + let key; + let value; + for (const item of arr) { + if (tallyObj[item]) { + tallyObj[item] = tallyObj[item] + 1; + } else { + tallyObj[item] = 1; + } + } + return tallyObj; +} module.exports = tally; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index ca763c2965..aec57646c7 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -23,12 +23,39 @@ 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"); +describe("contains", () => { + test("tally on an empty array returns an empty object", function () { + const input = tally([]); + const currentOutput = input; + const targetOutput = {}; -// Given an array with duplicate items -// When passed to tally -// Then it should return counts for each unique item + expect(currentOutput).toEqual(targetOutput); + }); -// Given an invalid input like a string, a number, or no argument at all -// When passed to tally -// Then it should throw Error("tally requires an array") + // 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("tally on aan array of items, returns an object containing the count for each unique item", function () { + expect(tally(["a"])).toEqual({ a: 1 }); + expect(tally(["a", "b", "c"])).toEqual({ a: 1, b: 1, c: 1 }); + expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 }); + }); + + // Given an array with duplicate items + // When passed to tally + // Then it should return counts for each unique item + test("tally with duplicate items returns counts for each unique item", function () { + expect(tally(["a", "a"])).toEqual({ a: 2 }); + expect(tally(["a", "a", "b", "b"])).toEqual({ a: 2, b: 2 }); + expect(tally(["a", "a", "b", "b", "c", "c"])).toEqual({ a: 2, b: 2, c: 2 }); + }); + // Given an invalid input like a string, a number, or no argument at all + // When passed to tally + // Then it should throw Error("tally requires an array") + it("throws when given an invalid input like a string, a number, or no argument", () => { + expect(() => tally(123)).toThrow(new Error("tally requires an array")); + expect(() => tally("hello")).toThrow(new Error("tally requires an array")); + expect(() => tally()).toThrow(new Error("tally requires an array")); + }); +}); From c442abe32f67210788a54126ce47b92b49947daf Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 12 Nov 2026 00:20:38 +0000 Subject: [PATCH 07/12] Implement invert function and add tests --- Sprint-2/interpret/invert.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Sprint-2/interpret/invert.js b/Sprint-2/interpret/invert.js index bb353fb1fd..f177225543 100644 --- a/Sprint-2/interpret/invert.js +++ b/Sprint-2/interpret/invert.js @@ -5,25 +5,34 @@ // Then it should swap the keys and values in the object // E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"} - +const assert = require("node:assert/strict"); function invert(obj) { const invertedObj = {}; for (const [key, value] of Object.entries(obj)) { - invertedObj.key = value; + invertedObj[value] = key; } return invertedObj; } - +assert.deepEqual(invert({ a: 1, b: 2 }), { 1: "a", 2: "b" }); +console.log(invert({ a: 1, b: 2 })); // 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 containing the object's key-value pairs. +//[["a", 1], ["b", 2]] +// We use Object.entries() because we need to access both the keys and values of an object so we can swap them. // d) Explain why the current return value is different from the target output +// because in this line (invertedObj.key = value;) javascript create a property literally called "key +// it does not use the value stored inside the key, in this case we need to use [] // e) Fix the implementation of invert (and write tests to prove it's fixed!) From 48fdeebc29f970da42acffaf5196eb40f20a58bd Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Thu, 12 Nov 2026 12:22:43 +0000 Subject: [PATCH 08/12] handle inherited and special object keys in tally and querystring --- Sprint-2/implement/querystring.js | 2 +- Sprint-2/implement/tally.js | 2 +- Sprint-2/implement/tally.test.js | 4 ++++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Sprint-2/implement/querystring.js b/Sprint-2/implement/querystring.js index 7985f3de8d..a366f4fa6f 100644 --- a/Sprint-2/implement/querystring.js +++ b/Sprint-2/implement/querystring.js @@ -1,5 +1,5 @@ function parseQueryString(queryString) { - const queryParams = {}; + const queryParams = Object.create(null); if (queryString.length === 0) { return queryParams; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index b6b0c78427..bd27228cb9 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -9,7 +9,7 @@ function tally(arr) { let key; let value; for (const item of arr) { - if (tallyObj[item]) { + if (Object.hasOwn(tallyObj, item)) { tallyObj[item] = tallyObj[item] + 1; } else { tallyObj[item] = 1; diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index aec57646c7..494a9bda87 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -58,4 +58,8 @@ describe("contains", () => { expect(() => tally("hello")).toThrow(new Error("tally requires an array")); expect(() => tally()).toThrow(new Error("tally requires an array")); }); + test("tally handles object prototype property names", function () { + expect(tally(["toString"])).toEqual({ toString: 1 }); + expect(tally(["constructor"])).toEqual({ constructor: 1 }); + }); }); From 8901aed8ebca66430f719c4f42e9e571df0e37f6 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Wed, 18 Nov 2026 11:24:32 +0000 Subject: [PATCH 09/12] fix: address code review feedback and improve tests --- Sprint-2/debug/author.js | 4 ++-- Sprint-2/implement/contains.test.js | 8 ++++---- Sprint-2/implement/lookup.js | 7 +------ Sprint-2/implement/tally.js | 2 +- Sprint-2/implement/tally.test.js | 5 +++++ 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index a8334d54e2..7ecd02eaa7 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -11,8 +11,8 @@ const author = { alive: true, }; -for (const value in author) { - console.log(author[value]); +for (const value of Object.values(author)) { + console.log(value); } //author is Object and it is not iterable //for...of loop excepts array and string but it not working with object type diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index 362f87a803..b97820e17f 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -40,7 +40,7 @@ describe("contains", () => { // Given an object with properties // When passed to contains with a non-existent property name // Then it should return false - test("given an object, returns true as the object contains a key of 'a'", function () { + test("given an object, returns false when the object does not contain key 'c'", function () { const input = contains({ a: 1, b: 2 }, "c"); const currentOutput = input; const targetOutput = false; @@ -57,19 +57,19 @@ describe("contains", () => { new Error("contains requires an object") ); }); - it("throws when given an array containing a value that isn't an object", () => { + it("throws when given a number", () => { expect(() => contains(123)).toThrow( new Error("contains requires an object") ); }); - it("throws when given an array containing a value that isn't an object", () => { + it("throws when given null", () => { expect(() => contains(null)).toThrow( new Error("contains requires an object") ); }); - it("throws when given an array containing a value that isn't an object", () => { + it("throws when called without an argument", () => { expect(() => contains()).toThrow(new Error("contains requires an object")); }); diff --git a/Sprint-2/implement/lookup.js b/Sprint-2/implement/lookup.js index 32c766fad8..7085cf1ca4 100644 --- a/Sprint-2/implement/lookup.js +++ b/Sprint-2/implement/lookup.js @@ -5,10 +5,5 @@ function createLookup(countryCurrencyPairs) { } return lookUp; } -console.log( - createLookup([ - ["US", "USD"], - ["CA", "CAD"], - ]) -); + module.exports = createLookup; diff --git a/Sprint-2/implement/tally.js b/Sprint-2/implement/tally.js index bd27228cb9..2c670cb456 100644 --- a/Sprint-2/implement/tally.js +++ b/Sprint-2/implement/tally.js @@ -1,5 +1,5 @@ function tally(arr) { - const tallyObj = {}; + const tallyObj = Object.create(null); if (!Array.isArray(arr)) { throw new Error("tally requires an array"); } diff --git a/Sprint-2/implement/tally.test.js b/Sprint-2/implement/tally.test.js index 494a9bda87..7e2c358299 100644 --- a/Sprint-2/implement/tally.test.js +++ b/Sprint-2/implement/tally.test.js @@ -62,4 +62,9 @@ describe("contains", () => { expect(tally(["toString"])).toEqual({ toString: 1 }); expect(tally(["constructor"])).toEqual({ constructor: 1 }); }); + test("counts __proto__ correctly", () => { + expect(tally(["__proto__"])).toEqual( + expect.objectContaining({ __proto__: 1 }) + ); + }); }); From 8491ca3c1fcca25d35551c2a2fb96139190a8629 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Mon, 23 Nov 2026 12:12:20 +0000 Subject: [PATCH 10/12] Add comments to explain TypeError in author.js Added prediction and explanation comments for code behavior. --- Sprint-2/debug/author.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index 7ecd02eaa7..de13669762 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -1,4 +1,13 @@ // Predict and explain first... +// Prediction: +// I think this code will produce a TypeError because +// a regular JavaScript object is not iterable. + +// Explanation: +// The for...of loop works with iterable values such as +// arrays and strings, but it cannot loop directly over +// a regular object. We can use Object.values() to access +// all the property values. // 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 From a4455f9ee063637189a382e77d208a4df05d023e Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Mon, 23 Nov 2026 12:33:18 +0000 Subject: [PATCH 11/12] fix the error :wq# Changes to be committed: :wq# --- Sprint-2/debug/address.js | 9 +++++++++ Sprint-2/debug/author.js | 10 ++++------ Sprint-2/implement/contains.js | 3 --- Sprint-2/implement/contains.test.js | 29 +++++++++++++++-------------- 4 files changed, 28 insertions(+), 23 deletions(-) diff --git a/Sprint-2/debug/address.js b/Sprint-2/debug/address.js index 36d2f865db..de1f2bd9cc 100644 --- a/Sprint-2/debug/address.js +++ b/Sprint-2/debug/address.js @@ -1,4 +1,13 @@ // Predict and explain first... +// Prediction: +// I expect the code to output: +// My house number is 42 + +// Explanation: +// The address object contains a property called houseNumber +// with the value 42. We can access this property using +// dot notation (address.houseNumber) and display it +// inside a template literal using console.log(). // This code should log out the houseNumber from the address object // but it isn't working... diff --git a/Sprint-2/debug/author.js b/Sprint-2/debug/author.js index de13669762..9bc17b9c18 100644 --- a/Sprint-2/debug/author.js +++ b/Sprint-2/debug/author.js @@ -9,9 +9,6 @@ // a regular object. We can use Object.values() to access // all the property values. -// 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", @@ -23,6 +20,7 @@ const author = { for (const value of Object.values(author)) { console.log(value); } -//author is Object and it is not iterable -//for...of loop excepts array and string but it not working with object type -//we should Use for... in for object + +// author is an object and it is not iterable. +// for...of works with arrays and strings, but not directly with objects. +// We can use for...in to loop through an object's keys. diff --git a/Sprint-2/implement/contains.js b/Sprint-2/implement/contains.js index cefa841127..e2c38da9b4 100644 --- a/Sprint-2/implement/contains.js +++ b/Sprint-2/implement/contains.js @@ -1,7 +1,4 @@ function contains(cont, str) { - // if (cont === "") { - // return false; - // } if (typeof cont !== "object" || cont === null || Array.isArray(cont)) { throw new Error("contains requires an object"); } diff --git a/Sprint-2/implement/contains.test.js b/Sprint-2/implement/contains.test.js index b97820e17f..65aab79807 100644 --- a/Sprint-2/implement/contains.test.js +++ b/Sprint-2/implement/contains.test.js @@ -52,30 +52,31 @@ describe("contains", () => { // When passed to contains // Then it should throw Error("contains requires an object") // (careful: typeof [] and typeof null are both "object") - it("throws when given an array containing a value that isn't an object", () => { - expect(() => contains(["banana"])).toThrow( - new Error("contains requires an object") + it("throws when given an array", () => { + expect(() => contains(["banana"], "a")).toThrow( + "contains requires an object" ); }); + it("throws when given a number", () => { - expect(() => contains(123)).toThrow( - new Error("contains requires an object") - ); + expect(() => contains(123, "a")).toThrow("contains requires an object"); + }); + + it("throws when given a string", () => { + expect(() => contains("hello", "a")).toThrow("contains requires an object"); }); it("throws when given null", () => { - expect(() => contains(null)).toThrow( - new Error("contains requires an object") - ); + expect(() => contains(null, "a")).toThrow("contains requires an object"); }); - it("throws when called without an argument", () => { - expect(() => contains()).toThrow(new Error("contains requires an object")); + it("throws when called without an object argument", () => { + expect(() => contains(undefined, "a")).toThrow( + "contains requires an object" + ); }); it("throws when given an empty array", () => { - expect(() => contains([])).toThrow( - new Error("contains requires an object") - ); + expect(() => contains([], "a")).toThrow("contains requires an object"); }); }); From 1a111e8d1d1c760e94d31404fdf19909e02212f7 Mon Sep 17 00:00:00 2001 From: Shirin Panahian Date: Mon, 23 Nov 2026 12:48:10 +0000 Subject: [PATCH 12/12] add predict and explain --- Sprint-2/debug/recipe.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Sprint-2/debug/recipe.js b/Sprint-2/debug/recipe.js index 691a73cdd1..70f7966d52 100644 --- a/Sprint-2/debug/recipe.js +++ b/Sprint-2/debug/recipe.js @@ -1,4 +1,15 @@ // Predict and explain first... +// Prediction: +// I think this code will print the recipe title and the number +// of servings correctly, but it will print [object Object] +// instead of the ingredients. + +// Explanation: +// A regular JavaScript object cannot be displayed directly +// as a readable string using template literals. +// We need to access the ingredients property using +// recipe.ingredients. +// We can use join("\n") 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