Skip to content

Commit b63c6de

Browse files
committed
made up tests and then created functions to pass the required tests in implement folder
1 parent b058970 commit b63c6de

File tree

8 files changed

+185
-12
lines changed

8 files changed

+185
-12
lines changed

Sprint-2/implement/contains.js

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,34 @@
1-
function contains() {}
1+
function contains(checkObj, checkProp) {
2+
if (
3+
typeof checkObj === "object" &&
4+
checkObj !== null &&
5+
!Array.isArray(checkObj)
6+
) {
7+
const checkKeys = Object.keys(checkObj);
8+
let isProperty = false;
9+
10+
if (checkKeys.length === 0) {
11+
return false;
12+
}
13+
14+
if (Object.hasOwn(checkObj, checkProp)) {
15+
return true;
16+
} else {
17+
for (let key of checkKeys) {
18+
if (key === checkProp) {
19+
isProperty = true;
20+
}
21+
}
22+
23+
if (isProperty === false) {
24+
return false;
25+
}
26+
}
27+
} else {
28+
throw new Error("error invalid parameter please provide an object");
29+
}
30+
}
31+
32+
contains({ d: "me" }, 4);
233

334
module.exports = contains;

Sprint-2/implement/contains.test.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,25 @@ as the object doesn't contains a key of 'c'
2020
// Given an empty object
2121
// When passed to contains
2222
// Then it should return false
23-
test.todo("contains on empty object returns false");
23+
test("contains on empty object returns false", () =>
24+
expect(contains({}, "a")).toBe(false));
2425

2526
// Given an object with properties
2627
// When passed to contains with an existing property name
2728
// Then it should return true
29+
test("returns true when an object contains a property that matches the one passed to contains", () =>
30+
expect(contains({ area: "Manchester" }, "area")).toBe(true));
2831

2932
// Given an object with properties
3033
// When passed to contains with a non-existent property name
3134
// Then it should return false
35+
test("returns false when an object with properties is passed to contains with a non-existent property name", () =>
36+
expect(contains({ area: "Manchester" }, "town")).toBe(false));
3237

3338
// Given invalid parameters like an array
3439
// When passed to contains
3540
// Then it should return false or throw an error
41+
test("should throw an error when when an invalid parameter like an array is passed to contains", () =>
42+
expect(() => contains([1, 4], 4)).toThrow(
43+
"error invalid parameter please provide an object"
44+
));

Sprint-2/implement/lookup.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
function createLookup() {
1+
function createLookup(inputArray) {
22
// implementation here
3+
const returnObject = {};
4+
5+
if (Array.isArray(inputArray)) {
6+
if (inputArray.length === 0) {
7+
return "passed array was empty, no values to display";
8+
}
9+
10+
for (let item of inputArray) {
11+
returnObject[item[0]] = item[1];
12+
}
13+
14+
return returnObject;
15+
} else {
16+
throw new Error("error incorrect parameter passed please provide an array");
17+
}
318
}
419

520
module.exports = createLookup;

Sprint-2/implement/lookup.test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
const createLookup = require("./lookup.js");
22

3-
test.todo("creates a country currency code lookup for multiple codes");
3+
test("creates a country currency code lookup for multiple codes", () => {
4+
expect(
5+
createLookup([
6+
["US", "USD"],
7+
["CA", "CAD"],
8+
["MW", "MWK"],
9+
["ZW", "ZWD"],
10+
])
11+
).toEqual({ US: "USD", CA: "CAD", MW: "MWK", ZW: "ZWD" });
12+
});
13+
14+
test("if passed an empty array , should print a message telling user array is empty", () => {
15+
expect(createLookup([])).toBe("passed array was empty, no values to display");
16+
});
17+
18+
test("if passed parameter which is not an array throw an error", () => {
19+
expect(() => createLookup("US, USD")).toThrow(
20+
"error incorrect parameter passed please provide an array"
21+
);
22+
});
423

524
/*
625

Sprint-2/implement/querystring.js

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,42 @@ function parseQueryString(queryString) {
33
if (queryString.length === 0) {
44
return queryParams;
55
}
6-
const keyValuePairs = queryString.split("&");
76

8-
for (const pair of keyValuePairs) {
9-
const [key, value] = pair.split("=");
10-
queryParams[key] = value;
7+
if (queryString.includes("&")) {
8+
const keyValuePairs = queryString.split("&");
9+
10+
for (const pair of keyValuePairs) {
11+
let countMatch = 0;
12+
13+
if (pair.includes("=")) {
14+
const equalSignIndex = pair.indexOf("=");
15+
16+
queryParams[pair.slice(0, equalSignIndex)] = pair.slice(
17+
equalSignIndex + 1
18+
);
19+
} else {
20+
throw new Error(
21+
"error invalid format string, no = to separate key value pairs"
22+
);
23+
}
24+
}
25+
}
26+
27+
if (queryString.includes("=")) {
28+
const equalSignIndex = queryString.indexOf("=");
29+
30+
queryParams[queryString.slice(0, equalSignIndex)] = queryString.slice(
31+
equalSignIndex + 1
32+
);
33+
} else {
34+
throw new Error(
35+
"error invalid format string, no = to separate key value pairs"
36+
);
1137
}
1238

1339
return queryParams;
1440
}
1541

42+
console.log(`with ${parseQueryString("equation=x=y+1")}`);
43+
1644
module.exports = parseQueryString;

Sprint-2/implement/querystring.test.js

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,30 @@
33
// Below is one test case for an edge case the implementation doesn't handle well.
44
// Fix the implementation for this test, and try to think of as many other edge cases as possible - write tests and fix those too.
55

6-
const parseQueryString = require("./querystring.js")
6+
const parseQueryString = require("./querystring.js");
7+
8+
test("parseQueryString receives an empty string", () => {
9+
expect(parseQueryString("")).toEqual({});
10+
});
711

812
test("parses querystring values containing =", () => {
913
expect(parseQueryString("equation=x=y+1")).toEqual({
10-
"equation": "x=y+1",
14+
equation: "x=y+1",
1115
});
1216
});
17+
18+
test("if our function is passed only one key - value pair", () => {
19+
expect(parseQueryString("color=brown")).toEqual({ color: "brown" });
20+
});
21+
22+
test("parses querystring without an =, it should throw an error", () => {
23+
expect(() => parseQueryString("colorisequaltobrown")).toThrow(
24+
"error invalid format string, no = to separate key value pairs"
25+
);
26+
});
27+
28+
test("if our function is passed only one string but there is no =", () => {
29+
expect(parseQueryString("color,brown")).toEqual(
30+
"error invalid format string, no = to separate key value pair"
31+
);
32+
});

Sprint-2/implement/tally.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,39 @@
1-
function tally() {}
1+
function tally(inputArray) {
2+
if (Array.isArray(inputArray)) {
3+
if (inputArray.length === 0) {
4+
return {};
5+
}
6+
7+
let itemCount = 0;
8+
const tallyObject = {};
9+
let n = 0;
10+
11+
while (inputArray.length > 0) {
12+
const tempArray = [];
13+
let i = 0;
14+
let currentArrayItem = inputArray[0];
15+
16+
while (i < inputArray.length) {
17+
if (currentArrayItem === inputArray[i]) {
18+
itemCount++;
19+
tempArray.push(inputArray.splice(i, 1));
20+
i--;
21+
}
22+
23+
i++;
24+
}
25+
26+
if (tempArray.length > 0) {
27+
tallyObject[tempArray[0]] = tempArray.length;
28+
}
29+
}
30+
console.log(`the new array and the new object ${tallyObject}`);
31+
return tallyObject;
32+
} else {
33+
throw new Error("error invalid input passed, please provide an array");
34+
}
35+
}
36+
37+
console.log(`new object is ${tally(["a", "a", "b", "c", "c", "d", "a"])}`);
238

339
module.exports = tally;

Sprint-2/implement/tally.test.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,27 @@ const tally = require("./tally.js");
2323
// Given an empty array
2424
// When passed to tally
2525
// Then it should return an empty object
26-
test.todo("tally on an empty array returns an empty object");
26+
test("tally on an empty array returns an empty object", () => {
27+
expect(tally([])).toEqual({});
28+
});
2729

2830
// Given an array with duplicate items
2931
// When passed to tally
3032
// Then it should return counts for each unique item
33+
test("given an array with duplicate items it should return counts for each unique item ", () => {
34+
expect(tally(["a", "a", "b", "c", "c", "d", "a"])).toEqual({
35+
a: 3,
36+
b: 1,
37+
c: 2,
38+
d: 1,
39+
});
40+
});
3141

3242
// Given an invalid input like a string
3343
// When passed to tally
3444
// Then it should throw an error
45+
test("should throw an error when tally is passed an invalid input like a string", () => {
46+
expect(() => tally("b,b,c,d,e,e,f")).toThrow(
47+
"error invalid input passed, please provide an array"
48+
);
49+
});

0 commit comments

Comments
 (0)