We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 254f0b4 commit 3d3aabbCopy full SHA for 3d3aabb
Sprint-2/implement/contains.js
@@ -1,3 +1,15 @@
1
-function contains() {}
+function contains(object, name) {
2
+ if (object == null || Object.keys(object).length === 0) {
3
+ return false;
4
+ }
5
+
6
+ for (let key in object) {
7
+ if (key === name) {
8
+ return true;
9
10
11
12
13
+}
14
15
module.exports = contains;
Sprint-2/implement/contains.test.js
@@ -20,7 +20,6 @@ as the object doesn't contains a key of 'c'
20
// Given an empty object
21
// When passed to contains
22
// Then it should return false
23
-test.todo("contains on empty object returns false");
24
25
// Given an object with properties
26
// When passed to contains with an existing property name
@@ -33,3 +32,15 @@ test.todo("contains on empty object returns false");
33
32
// Given invalid parameters like an array
34
35
// Then it should return false or throw an error
36
+describe("contains", () => {
37
+ [
38
+ { input: [{}, "a"], expected: false },
39
+ { input: [{ a: 1, b: 2 }, "a"], expected: true },
40
+ { input: [{ a: 1, b: 2 }, "c"], expected: false },
41
+ { input: [null, "a"], expected: false },
42
+ ].forEach(({ input, expected }) =>
43
+ it(`return if object contains the key for ${input[1]}`, () =>
44
+ expect(contains(...input)).toEqual(expected))
45
+ );
46
+});
Sprint-2/implement/lookup.js
@@ -1,5 +1,13 @@
-function createLookup() {
- // implementation here
+function createLookup(countryCurrencyPairs) {
+ const array = {};
+ for (const pair of countryCurrencyPairs) {
+ const country = pair[0];
+ const currency = pair[1];
+ array[country] = currency;
+ return array;
}
module.exports = createLookup;
Sprint-2/implement/querystring.js
@@ -6,7 +6,8 @@ function parseQueryString(queryString) {
const keyValuePairs = queryString.split("&");
for (const pair of keyValuePairs) {
- const [key, value] = pair.split("=");
+ const [key, ...rest] = pair.split("=");
+ const value = rest.join("=");
queryParams[key] = value;
Sprint-2/implement/querystring.test.js
@@ -3,10 +3,23 @@
// Below is one test case for an edge case the implementation doesn't handle well.
// Fix the implementation for this test, 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("parses querystring values containing =", () => {
- expect(parseQueryString("equation=x=y+1")).toEqual({
- "equation": "x=y+1",
+describe("parseQueryString", () => {
+ test("returns an empty object when query is empty", () => {
+ expect(parseQueryString("")).toEqual({});
+ });
+ test("parses query string values containing =", () => {
+ expect(parseQueryString("equation=x=y+1")).toEqual({
+ equation: "x=y+1",
16
17
18
19
+ test("checks for keys without values", () => {
+ expect(parseQueryString("foo&bar=2")).toEqual({
+ foo: "",
+ bar: "2",
});
Sprint-2/implement/tally.js
@@ -1,3 +1,19 @@
-function tally() {}
+function tally(itemsList) {
+ const counts = {};
+ if (itemsList.length === 0) {
+ return {};
+ if (!Array.isArray(itemsList)) {
+ throw new Error("Input must be an array");
+ for (const item of itemsList) {
+ counts[item] = (counts[item] || 0) + 1;
+ return counts;
module.exports = tally;
Sprint-2/implement/tally.test.js
@@ -32,3 +32,21 @@ test.todo("tally on an empty array returns an empty object");
// Given an invalid input like a string
// When passed to tally
// Then it should throw an error
+describe("tally", () => {
+ test("returns an empty object when given an empty array", () => {
+ expect(tally([])).toEqual({});
+ test("returns counts for each unique item", () => {
+ expect(tally(["a", "a", "b", "c"])).toEqual({
+ a: 2,
+ b: 1,
+ c: 1,
47
48
49
+ test("throws an error when input is not an array", () => {
50
+ expect(() => tally("abc")).toThrow();
51
52
0 commit comments