Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
9b10ba6
made a prediction about the error in the file address.js
MaharitB Nov 6, 2026
586987d
made the necessary change to fix the error
MaharitB Nov 6, 2026
9dbc72a
gave my prediction and explained about the bug
MaharitB Nov 6, 2026
59f4ca7
made further explanation for the error
MaharitB Nov 6, 2026
759cd2f
edited the code to make the code bug free. works well now
MaharitB Nov 6, 2026
5a26afc
gave my prediction,explanation and edited the code to get the desire…
MaharitB Nov 6, 2026
fc407f2
added test tha t should return true when it contains the proerty. tes…
MaharitB Nov 8, 2026
ec28696
added a test just to pass the test. test passed
MaharitB Nov 8, 2026
b1637b2
added a test for empty object test failed
MaharitB Nov 8, 2026
d14c2b1
added a code to make the test pass and.tess passed
MaharitB Nov 8, 2026
f48a0d2
1 test failed that should return false when a proprety name doesn't …
MaharitB Nov 8, 2026
0fd541f
added a test that throws an error if value is not object. test failed
MaharitB Nov 8, 2026
5d3bf95
edited the code and tests passed except for throwing an error
MaharitB Nov 8, 2026
6ac330d
added to make a test pass to throw an error and it did
MaharitB Nov 8, 2026
10992d0
added some code for throwing error properly, it worked and test passed
MaharitB Nov 8, 2026
9e03adc
added some more tests on giving null ,numbers and no agrument .test p…
MaharitB Nov 8, 2026
74bd5bc
added a test that wold return empty objects when given empty array
MaharitB Nov 9, 2026
409fe5a
declared and initialized variables. added a return value just to pass…
MaharitB Nov 9, 2026
5623be9
added a test for on value of an array. test failed
MaharitB Nov 9, 2026
6101574
added the cod to make the test pass.test passed
MaharitB Nov 9, 2026
a117269
added a test that returnan objectkey value pairs from an array of thr…
MaharitB Nov 9, 2026
5cb8310
addeda a code that would allow a search in the given array to much th…
MaharitB Nov 9, 2026
7b4ff8f
made a change on the coding and two tests passed
MaharitB Nov 9, 2026
13ec1b1
added some more code to decode the encoded values and keys.test passed
MaharitB Nov 10, 2026
c91022e
added a code to replace ' +' to ' ' .it worked test pass
MaharitB Nov 10, 2026
b36b46c
added a code to ignore an empty key valu pair after &. it worked .te…
MaharitB Nov 10, 2026
cbf65e0
added a test that returns empty object when empty array is given.test…
MaharitB Nov 10, 2026
0c1d9b3
added a code to pass the test.test passed
MaharitB Nov 10, 2026
bb13213
added a test to return an object containg the count of one item
MaharitB Nov 10, 2026
d399dd1
added a code to pass the test to count one lement in an array.test pa…
MaharitB Nov 10, 2026
af3f0eb
added a test for multiple same item and test failed
MaharitB Nov 10, 2026
9907a22
changed the equal sign to greater to accomodate multiple items.test p…
MaharitB Nov 10, 2026
2aac822
added a tests for multiple items and test that throws error when giv…
MaharitB Nov 10, 2026
f0eefc8
added a code to count the items .test passed
MaharitB Nov 10, 2026
c04e5e6
added a code to throw error and test passed
MaharitB Nov 10, 2026
c250694
refractored the code
MaharitB Nov 10, 2026
43f3f65
answered a nd b
MaharitB Nov 10, 2026
5249da4
answered question C
MaharitB Nov 11, 2026
a96ef2d
answerd question d and e
MaharitB Nov 11, 2026
f163306
fixed the implementation now it is printing out the target values
MaharitB Nov 11, 2026
276e30c
added the export to the function
MaharitB Nov 11, 2026
9c2fa1d
created te file for testing and imported the function invert fron in…
MaharitB Nov 11, 2026
4513817
added a test when given empty object to return empty object .test passed
MaharitB Nov 11, 2026
a05704c
added a test for one and more properties and test passed
MaharitB Nov 11, 2026
5d5fb7e
added a test to show that "first+name=Mary+Jane" can pass the test .t…
MaharitB Nov 11, 2026
e03d59d
edited the code as advised now it takes the array from the input .all…
MaharitB Nov 11, 2026
cc041a1
made changes to this file as well,all test pass
MaharitB Nov 11, 2026
3afcdc4
made changes to the code as advised all 6 test passed
MaharitB Nov 14, 2026
f162460
made the correction
MaharitB Nov 14, 2026
1b17717
made changes to the test names
MaharitB Nov 14, 2026
a7b8c0d
made small changes
MaharitB Nov 14, 2026
a6c4e31
add a test to replace all the '+' sign , test failed
MaharitB Nov 14, 2026
7828f7b
changed a code to make the test that should replave all the + sign to…
MaharitB Nov 14, 2026
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
4 changes: 3 additions & 1 deletion Sprint-2/debug/address.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Predict and explain first...

// my prediction is that this is not indexed array. it is an object that uses key.

// 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 +14,4 @@ const address = {
postcode: "XYZ 123",
};

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

// my prediction here is that we can not use for..of loop because author is an object.
// The reason is, it doesnt use indices it uses keys.instead we will use for..in loop.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Its true that author is not iterable.

however the reason for that isn't that it uses keys instead. More on this in the following article.

https://medium.com/@mohammedelaouri/understanding-why-objects-are-not-iterable-in-javascript-and-the-best-ways-to-iterate-over-them-5048f8edf5d8

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for the article! It is insightful.


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

// As I said earlier it is not using index, we can not iterate it with
// for ..of loop. The output also going to be the key so we have to change that as well.

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

for (const value of author) {
console.log(value);
for (const key in author) {
console.log(author[key]);
}
12 changes: 9 additions & 3 deletions Sprint-2/debug/recipe.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
// Predict and explain first...
// My prediction is that ingredients are not gonna be printed out. because the object
// is not attached to the key in the console.log.

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

// We can fix it by using for..of loop for the array .

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

console.log(`${recipe.title} serves ${recipe.serves}
ingredients:
${recipe}`);
console.log(`${recipe.title} serves ${recipe.serves}`);

for (const item of recipe.ingredients) {
console.log(item);
}
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(objIn, property) {
if (Array.isArray(objIn) || objIn === null || typeof objIn !== "object") {
throw new Error("contains requires an object");
}

for (const key in objIn) {
if (key === property) {
return true;
}
}
return false;
}

module.exports = contains;
46 changes: 45 additions & 1 deletion Sprint-2/implement/contains.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,66 @@ as an array isn't an object
// 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("should return true when an object contains the property", () => {
const object1 = { a: 1, b: 2, c: 3 };
const property = "a";
const result = contains(object1, property);
expect(result).toEqual(true);
});

// Given an empty object
// When passed to contains
// Then it should return false
test.todo("contains on empty object returns false");
test("should return false when empty object is passed", () => {
const object1 = {};
const property = "a";
const result = contains(object1, property);
expect(result).toEqual(false);
});

// Given an object with properties
// When passed to contains with an existing property name
// Then it should return true
test("should return true when object is passed and property name matches", () => {
const object1 = { a: 1, b: 2, c: 3, d: 4 };
const property = "b";
const result = contains(object1, property);
expect(result).toEqual(true);
});
test("should return true when object is passed and property name matches", () => {
const object1 = { name: "sharon", age: 30, hobby: "photography" };
const property = "hobby";
const result = contains(object1, property);
expect(result).toEqual(true);
});

// Given an object with properties
// When passed to contains with a non-existent property name
// Then it should return false

test("should return false when object is passed and property name doesnt matches ", () => {
const object1 = { a: 1, b: 2, c: 3, d: 4 };
const property = "e";
const result = contains(object1, property);
expect(result).toEqual(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")
test("should throw an error when the value passed is Array", () => {
expect(() => contains([1, 2, 3])).toThrow("contains requires an object");
});
test("should throw an error when the value passed is null", () => {
expect(() => contains(null)).toThrow("contains requires an object");
});
test("should throw an error when the valu passed is a string", () => {
expect(() => contains("null")).toThrow("contains requires an object");
});
test("should throw an error when the value passed is number", () => {
expect(() => contains(7)).toThrow("contains requires an object");
});
test("should throw an error when there is no argument", () => {
expect(() => contains()).toThrow("contains requires an object");
});
9 changes: 7 additions & 2 deletions Sprint-2/implement/lookup.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
function createLookup() {
// implementation here
function createLookup(array) {
const countryCurrency = {};

for (let i = 0; i < array.length; i++) {
countryCurrency[array[i][0]] = array[i][1];
}
return countryCurrency;
}

module.exports = createLookup;
32 changes: 30 additions & 2 deletions Sprint-2/implement/lookup.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const createLookup = require("./lookup.js");

test.todo("creates a country currency code lookup for multiple codes");

/*

Create a lookup object of key value pairs from an array of code pairs
Expand Down Expand Up @@ -33,3 +31,33 @@ It should return:
'CA': 'CAD'
}
*/
test("should return an empty object key from an empty array ", () => {
expect(createLookup([])).toEqual({});
});
test("should return an object key value pairs from an array of one country currency code ", () => {
expect(createLookup([["US", "USD"]])).toEqual({ US: "USD" });
});
test("should return an object key value pairs from an array of multiple code pairs", () => {
expect(
createLookup([
["US", "USD"],
["CA", "CAD"],
])
).toEqual({
US: "USD",
CA: "CAD",
});
});
test("should return an object key value pairs from an array of three code pairs", () => {
expect(
createLookup([
["ERI", "NAK"],
["CA", "CAD"],
["EU", "EUR"],
])
).toEqual({
ERI: "NAK",
CA: "CAD",
EU: "EUR",
});
});
18 changes: 16 additions & 2 deletions Sprint-2/implement/querystring.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
function parseQueryString(queryString) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

what happens when you pass the following input to the fn?

parseQueryString("a+b+c=x+y+z")

is this the correct output?

const queryParams = {};

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

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

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

const lastThreeCharKey = key.slice(-2);
if (!lastThreeCharKey.includes("+") || key.includes("%")) {
key = decodeURIComponent(key.replaceAll("+", " "));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you're on the right track using replaceAll fn, however there is no need to detect if the key or value contains a "+". Since replaceAll replaces all the instances of "+" we don't need to worry about how many times it appears in the string.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

what I am doing right on that if comparison is that I am checking the last two characters, fore example if the key value has an equation like in one of my tests.(a+b+c=x+y+z) so it will check if the last characters include plus sign and then replace it if it is not equation**( first+name, last+name)**. I am not counting rather I am checking. (There is no other way to change +sign even if I decode it).
before I edited the code I used last three to check for equations with an ending of two characters lets say , 2x + 4x. Can you please explain more about what you want me to do here. I need guidance. Thank you .

}
const lastThreeCharVal = value.slice(-2);
if (!lastThreeCharVal.includes("+")) {
value = decodeURIComponent(value.replaceAll("+", " "));
}
queryParams[key] = value;
}

return queryParams;
}

Expand Down
27 changes: 20 additions & 7 deletions Sprint-2/implement/querystring.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -37,12 +37,25 @@ test("should replace '+' by ' '", () => {
});
});

// Stretch exercise: Handling query strings that contain identical keys
test("should replace '+' by ' '", () => {
expect(parseQueryString("first+name=Mary+Jane")).toEqual({
"first name": "Mary Jane",
});
});

// 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("should replace multiple plus'+' by ' '", () => {
expect(parseQueryString("name=John+Doe+Foo&age=30&city=New+York")).toEqual({
name: "John Doe Foo",
age: "30",
city: "New York",
});
});
// Stretch exercise: Handling query strings that contain identical keys

// 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",
//});
//});
16 changes: 14 additions & 2 deletions Sprint-2/implement/tally.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
function tally() {}

function tally(arrayItems) {
const countItem = {};
if (!Array.isArray(arrayItems)) {
throw new Error("tally rquires an input");
}
for (const item of arrayItems) {
if (countItem[item]) {
countItem[item] += 1;
} else {
countItem[item] = 1;
}
}
return countItem;
}
module.exports = tally;
27 changes: 27 additions & 0 deletions Sprint-2/implement/tally.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,43 @@ 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("should return an object containing the count for each unique item", () => {
const array = ["a"];
const countArray = tally(array);
expect(countArray).toEqual({ a: 1 });
});

// 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(" should return an empty object when empty array is given", () => {
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 an object containing the count for each unique item", () => {
const array = ["a", "a", "a"];
const countArray = tally(array);
expect(countArray).toEqual({ a: 3 });
});
test("should return an object containing the count for each unique item", () => {
const array = ["a", "a", "b", "b", "b", "c"];
const countArray = tally(array);
expect(countArray).toEqual({ a: 2, b: 3, c: 1 });
});

// 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")
test("should throw an error when given invalid input:string", () => {
expect(() => tally("a")).toThrow("tally rquires an input");
});
test("should throw an error when given invalid input:number", () => {
expect(() => tally(5)).toThrow("tally rquires an input");
});
test("should throw an error when given invalid input: no argument", () => {
expect(() => tally()).toThrow("tally rquires an input");
});
26 changes: 21 additions & 5 deletions Sprint-2/interpret/invert.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,36 @@ function invert(obj) {
const invertedObj = {};

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

return invertedObj;
}

module.exports = invert;
// 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}

// c) What does Object.entries return? Why is it needed in this program?
//{'1' :'a','2':'b' }

// d) What does Object.entries return? Why is it needed in this program?

//Objet.entries takes objects and returne array with in array or sub arrays.
//in this program it is needed to put the properties in an array as key and value.
// After that line though, the next line takes the key and value and place it in an object again by changing key and value
// position when stored in the object

// e) Explain why the current return value is different from the target output
//The crrent output is different becouse it is only storing the last element in the loop
// with the key name "key" . So when it is printed out it comes forexample {'key': 1}
//and also the value and key dont change position.

// d) Explain why the current return value is different from the target output
// f) 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!)
// fixed the implementation and now it is printing the target value
14 changes: 14 additions & 0 deletions Sprint-2/interpret/invert.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const invert = require("./invert.js");
// for an empty object
test(" should return an empty object if given empty object", () => {
expect(invert({})).toEqual({});
});
//test for one proprty in an object
test(" should return an object inverted when given one property object", () => {
expect(invert({ a: 1 })).toEqual({ 1: "a" });
});
//test for two property in an object
test(" should return an object that is inverted when given two property object", () => {
expect(invert({ a: 1, b: 2, c: 3 })).toEqual({ 1: "a", 2: "b", 3: "c" });
});
//
Loading