Skip to content

Commit a901b1f

Browse files
author
Enice-Codes
committed
modiefied files
2 parents 2a4905f + e39f4df commit a901b1f

15 files changed

Lines changed: 201 additions & 75 deletions

‎Sprint-2/implement/contains.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
function contains() {}
1+
function contains(obj, prop) {
2+
if (typeof obj !== "object" || obj === null) return false;
3+
return Object.prototype.hasOwnProperty.call(obj, prop);
4+
}
25

3-
module.exports = contains;
6+
module.exports = contains;

‎Sprint-2/implement/contains.test.js‎

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const contains = require("./contains.js");
1+
const contains = require("./contains")
22

33
/*
44
Implement a function called contains that checks an object contains a
@@ -16,20 +16,44 @@ as the object doesn't contains a key of 'c'
1616
// Given a contains function
1717
// When passed an object and a property name
1818
// Then it should return true if the object contains the property, false otherwise
19+
test("contains returns true for existing property", () => {
20+
const currentOutput = contains({a:1,b:2}, 'a');
21+
const targetOutput = true;
22+
expect(currentOutput).toEqual(targetOutput);
23+
});
1924

2025
// Given an empty object
2126
// When passed to contains
2227
// Then it should return false
23-
test.todo("contains on empty object returns false");
28+
test("contains on empty object returns false", () => {
29+
const currentOutput = contains({}, 'a');
30+
const targetOutput = false;
31+
expect(currentOutput).toEqual(targetOutput);
32+
});
2433

2534
// Given an object with properties
2635
// When passed to contains with an existing property name
2736
// Then it should return true
37+
test("contains returns true for existing property", () => {
38+
const currentOutput = contains({a:1,b:2}, 'b');
39+
const targetOutput = true;
40+
expect(currentOutput).toEqual(targetOutput);
41+
});
2842

2943
// Given an object with properties
3044
// When passed to contains with a non-existent property name
3145
// Then it should return false
46+
test("contains returns false for non-existent property", () => {
47+
const currentOutput = contains({a:1,b:2}, 'c');
48+
const targetOutput = false;
49+
expect(currentOutput).toEqual(targetOutput);
50+
});
3251

3352
// Given invalid parameters like an array
3453
// When passed to contains
3554
// Then it should return false or throw an error
55+
test("contains returns false for invalid parameters", () => {
56+
const currentOutput = contains([1,2,3], 'a');
57+
const targetOutput = false;
58+
expect(currentOutput).toEqual(targetOutput);
59+
});

‎Sprint-2/implement/lookup.js‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
function createLookup() {
2-
// implementation here
1+
function createLookup(pairs) {
2+
const lookup = {};
3+
for (const [countryCode, currencyCode] of pairs) {
4+
lookup[countryCode] = currencyCode;
5+
}
6+
return lookup;
37
}
48

5-
module.exports = createLookup;
9+
module.exports = createLookup;

‎Sprint-2/implement/lookup.test.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
const createLookup = require("./lookup.js");
22

3-
test.todo("creates a country currency code lookup for multiple codes");
4-
3+
test("creates a country currency code lookup for multiple codes", () => {
4+
const countryCurrencyPairs = [["US", "USD"], ["CA", "CAD"]];
5+
const result = createLookup(countryCurrencyPairs);
6+
expect(result).toEqual({ US: "USD", CA: "CAD" });
7+
});
58
/*
69
710
Create a lookup object of key value pairs from an array of code pairs

‎Sprint-2/implement/querystring.test.js‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,18 @@ test("should replace '+' by ' '", () => {
3737
});
3838
});
3939

40+
test("should return {} for an empty string", () => {
41+
expect(parseQueryString("")).toEqual({});
42+
});
43+
4044
// Stretch exercise: Handling query strings that contain identical keys
4145

4246
// Delete this test if you are not working on this optional case
4347
test("should store values of a key in an array when the key has 2 or more values", () => {
44-
expect(parseQueryString("key=value1&key=value2&key=value3&foo=bar")).toEqual({
48+
expect(
49+
parseQueryString("key=value1&key=value2&key=value3&foo=bar")
50+
).toEqual({
4551
key: ["value1", "value2", "value3"],
4652
foo: "bar",
4753
});
48-
});
54+
});

‎Sprint-2/implement/tally.js‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ function tally(arr) {
22
if (!Array.isArray(arr)) {
33
throw new Error("tally expects an array");
44
}
5+
56
const counts = {};
67
for (const item of arr) {
78
counts[item] = (counts[item] || 0) + 1;
89
}
910
return counts;
1011
}
12+
1113
module.exports = tally;

‎Sprint-2/implement/tally.test.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ describe("tally on an array of items returns counts for each unique item", () =>
2727
expect(tally([])).toEqual({});
2828
});
2929

30+
<<<<<<< HEAD
3031
// Given an array with duplicate items
3132
// When passed to tally
3233
// Then it should return counts for each unique item
@@ -42,4 +43,37 @@ describe("tally on an array of items returns counts for each unique item", () =>
4243
test("tally throws an error for invalid input", () => {
4344
expect(() => tally("not an array")).toThrow();
4445
});
46+
=======
47+
test("tally on an empty array returns an empty object", () => {
48+
expect(tally([])).toEqual({});
49+
});
50+
51+
// Given an empty array
52+
// When passed to tally
53+
// Then it should return an empty object
54+
test("tally on an array with duplicate items returns counts for each unique item", () => {
55+
expect(tally(["a"])).toEqual({ a: 1 });
56+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
57+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
58+
});
59+
60+
// Given an array with duplicate items
61+
// When passed to tally
62+
// Then it should return counts for each unique item
63+
64+
test("tally on an array with duplicate items returns counts for each unique item", () => {
65+
expect(tally(["a"])).toEqual({ a: 1 });
66+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
67+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
68+
});
69+
70+
71+
// Given an invalid input like a string
72+
// When passed to tally
73+
// Then it should throw an error
74+
test("tally on an array with duplicate items returns counts for each unique item", () => {
75+
expect(tally(["a"])).toEqual({ a: 1 });
76+
expect(tally(["a", "a", "a"])).toEqual({ a: 3 });
77+
expect(tally(["a", "a", "b", "c"])).toEqual({ a: 2, b: 1, c: 1 });
78+
>>>>>>> Data-Objects
4579
});

‎Sprint-2/interpret/invert.js‎

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
// Let's define how invert should work
2-
2+
// inverse typically means to reverse the key and value in an object.
33
// Given an object
44
// When invert is passed this object
55
// Then it should swap the keys and values in the object
6-
76
// E.g. invert({x : 10, y : 20}), target output: {"10": "x", "20": "y"}
8-
97
function invert(obj) {
108
const invertedObj = {};
11-
129
for (const [key, value] of Object.entries(obj)) {
13-
invertedObj.key = value;
10+
invertedObj[value] = key;
1411
}
15-
1612
return invertedObj;
1713
}
1814

19-
// a) What is the current return value when invert is called with { a : 1 }
20-
21-
// b) What is the current return value when invert is called with { a: 1, b: 2 }
22-
23-
// c) What is the target return value when invert is called with {a : 1, b: 2}
24-
25-
// c) What does Object.entries return? Why is it needed in this program?
26-
27-
// d) Explain why the current return value is different from the target output
28-
29-
// e) Fix the implementation of invert (and write tests to prove it's fixed!)
15+
module.exports = invert;

‎Sprint-2/interpret/invert.test.js‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const invert = require("./invert");
2+
3+
test("invert swaps keys and values in an object", () => {
4+
const input = { a: 1, b: 2 };
5+
const expectedOutput = { "1": "a", "2": "b" };
6+
expect(invert(input)).toEqual(expectedOutput);
7+
});
8+
9+
test("invert returns an empty object when given an empty object", () => {
10+
expect(invert({})).toEqual({});
11+
});
12+
13+
test("invert works with string values", () => {
14+
const input = { x: "10", y: "20" };
15+
const expectedOutput = { "10": "x", "20": "y" };
16+
expect(invert(input)).toEqual(expectedOutput);
17+
});

‎Sprint-2/stretch/count-words.js‎

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,14 @@
1-
/*
2-
Count the number of times a word appears in a given string.
3-
4-
Write a function called countWords that
5-
- takes a string as an argument
6-
- returns an object where
7-
- the keys are the words from the string and
8-
- the values are the number of times the word appears in the string
9-
10-
Example
11-
If we call countWords like this:
12-
13-
countWords("you and me and you") then the target output is { you: 2, and: 2, me: 1 }
14-
15-
To complete this exercise you should understand
16-
- Strings and string manipulation
17-
- Loops
18-
- Comparison inside if statements
19-
- Setting values on an object
20-
21-
## Advanced challenges
22-
23-
1. Remove all of the punctuation (e.g. ".", ",", "!", "?") to tidy up the results
24-
25-
2. Ignore the case of the words to find more unique words. e.g. (A === a, Hello === hello)
26-
27-
3. Order the results to find out which word is the most common in the input
28-
*/
1+
function countWords(str) {
2+
const wordCount = {};
3+
const words = str
4+
.toLowerCase()
5+
.replace(/[^\w\s]/g, "")
6+
.split(/\s+/)
7+
.filter(Boolean);
8+
for (const word of words) {
9+
wordCount[word] = (wordCount[word] || 0) + 1;
10+
}
11+
return wordCount;
12+
}
13+
14+
module.exports = countWords;

0 commit comments

Comments
 (0)