Skip to content

Commit e74872a

Browse files
authored
Merge pull request #47 from lhchavez/mocha
Use mocha for tests
2 parents 90396a3 + aa70628 commit e74872a

File tree

13 files changed

+979
-131
lines changed

13 files changed

+979
-131
lines changed

JavaScript/chapter01/1.1 - Is Unique/solution.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ Input: takes an array of integers
33
Output: returns a deduped array of integers
44
*/
55

6-
// Solution using Set
6+
// Solution using Set
77
const isUnique = (arr) => [...new Set(arr)];
88

99
// Test Cases
10-
console.log(isUnique([1,1,1,2,2,2,2,3,3,3,3]) === [1,2,3]);
10+
const assert = require('assert');
11+
12+
describe(module.filename, () => {
13+
it('should deduplicate array', () => {
14+
assert.deepEqual(isUnique([1,1,1,2,2,2,2,3,3,3,3]), [1,2,3]);
15+
});
16+
});

JavaScript/chapter01/1.2 - Check Perm/rroque98_sol.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,19 @@ const isPermutation = (str1, str2) => {
2525
};
2626

2727
// Tests:
28-
console.log(isPermutation('abc', 'abb') === false);
29-
console.log(isPermutation('abb', 'abc') === false);
30-
console.log(isPermutation('aaa', 'abc') === false);
31-
console.log(isPermutation('abc', 'abcd') === false);
32-
console.log(isPermutation('abc', 'bac') === true);
33-
console.log(isPermutation('', '') === true);
34-
console.log(isPermutation('12', '21') === true);
28+
const assert = require('assert');
29+
30+
describe(module.filename, () => {
31+
it('should handle positive cases', () => {
32+
assert.equal(isPermutation('abc', 'bac'), true);
33+
assert.equal(isPermutation('', ''), true);
34+
assert.equal(isPermutation('12', '21'), true);
35+
});
36+
37+
it('should handle negative cases', () => {
38+
assert.equal(isPermutation('abc', 'abb'), false);
39+
assert.equal(isPermutation('abb', 'abc'), false);
40+
assert.equal(isPermutation('aaa', 'abc'), false);
41+
assert.equal(isPermutation('abc', 'abcd'), false);
42+
});
43+
});

JavaScript/chapter01/1.3 - URLify/rroque98_sol.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
const encodeSpaces = (string) => string.replace(/ /g, '%20');
44

55
// Tests:
6-
console.log(encodeSpaces('Hello World') === 'Hello%20World');
7-
console.log(encodeSpaces('') === '');
8-
console.log(encodeSpaces('This is an example') === 'This%20is%20an%20example');
6+
const assert = require('assert');
7+
8+
describe(module.filename, () => {
9+
it('should replace spaces with %20', () => {
10+
assert.equal(encodeSpaces('Hello World'), 'Hello%20World');
11+
assert.equal(encodeSpaces(''), '');
12+
assert.equal(encodeSpaces('This is an example'), 'This%20is%20an%20example');
13+
});
14+
});

JavaScript/chapter01/1.3 - URLify/solution.js

+12-11
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,32 @@
22
function URLify(arr, len) {
33
let spaces = 0;
44
for(let i = 0; i < len; i++) {
5-
if(arr[i] === ' ') spaces++;
6-
}
5+
if(arr[i] === ' ') spaces++;
6+
}
77
//last index
88
let index = len + spaces * 2 - 1;
99

1010
for(let i = len - 1; i >= 0; i--) {
1111
if(arr[i] === ' ') {
12-
arr[index] = '0';
12+
arr[index] = '0';
1313
arr[index - 1] = '2';
1414
arr[index - 2] = '%';
1515
index -= 3
1616
} else {
1717
arr[index] = arr[i];
1818
index--;
19-
}
19+
}
2020
}
2121
return arr;
2222
}
2323

2424
//testing
25-
let arr = ['M', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h', ' ', ' ', ' ', ' '];
26-
27-
//before
28-
console.log(arr);
29-
let ans = URLify(arr, 13);
25+
const assert = require('assert');
3026

31-
//after
32-
console.log(ans);
27+
describe(module.filename, () => {
28+
it('should correctly URLify', () => {
29+
let arr = ['M', 'r', ' ', 'J', 'o', 'h', 'n', ' ', 'S', 'm', 'i', 't', 'h', ' ', ' ', ' ', ' '];
30+
let expected = ['M', 'r', '%', '2', '0', 'J', 'o', 'h', 'n', '%', '2', '0', 'S', 'm', 'i', 't', 'h'];
31+
assert.deepEqual(URLify(arr, 13), expected);
32+
});
33+
});

JavaScript/chapter01/1.4 - PalinPerm/rroque98_sol.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,16 @@ const isPalindromePermutation = (str) => {
1919
return true;
2020
}
2121

22-
console.log(isPalindromePermutation('tact coa') === true);
23-
console.log(isPalindromePermutation('tact cooa') === true);
24-
console.log(isPalindromePermutation('tacr coa') === false);
25-
console.log(isPalindromePermutation('tactr coa') === false);
22+
const assert = require('assert');
23+
24+
describe(module.filename, () => {
25+
it('should handle positive cases', () => {
26+
assert.equal(isPalindromePermutation('tact coa'), true);
27+
assert.equal(isPalindromePermutation('tact cooa'), true);
28+
});
29+
30+
it('should handle negative cases', () => {
31+
assert.equal(isPalindromePermutation('tacr coa'), false);
32+
assert.equal(isPalindromePermutation('tactr coa'), false);
33+
});
34+
});

JavaScript/chapter01/1.5 - OneAway/rroque98_sol.js

+35-29
Original file line numberDiff line numberDiff line change
@@ -35,38 +35,44 @@ const isOneAway = (str1, str2) => {
3535
return true;
3636
};
3737

38-
// ****** TESTS ******
38+
const assert = require('assert');
39+
3940
function runTests(cases, expected) {
4041
for (const [str1, str2] of cases) {
41-
console.log(
42-
isOneAway(str1, str2) === expected && isOneAway(str2, str1) === expected
43-
);
42+
assert.equal(isOneAway(str1, str2), expected);
43+
assert.equal(isOneAway(str2, str1), expected);
4444
}
4545
}
4646

47-
runTests(
48-
[
49-
['pale', 'ple'], // deletion
50-
['pale', 'opale'], // insertion in beginning
51-
['pale', 'palse'], // insertion in middle
52-
['pale', 'pales'], // insertion at end
53-
['pale', 'bale'], // replacement
54-
['p', 'b'],
55-
['p', 'p'],
56-
['p', ''],
57-
['', '']
58-
],
59-
true
60-
);
47+
describe(module.filename, () => {
48+
it('should handle positive cases', () => {
49+
runTests(
50+
[
51+
['pale', 'ple'], // deletion
52+
['pale', 'opale'], // insertion in beginning
53+
['pale', 'palse'], // insertion in middle
54+
['pale', 'pales'], // insertion at end
55+
['pale', 'bale'], // replacement
56+
['p', 'b'],
57+
['p', 'p'],
58+
['p', ''],
59+
['', '']
60+
],
61+
true
62+
);
63+
});
6164

62-
runTests(
63-
[
64-
['pale', 'ae'], // greater than 1 deletions
65-
['pale', 'ppalpe'], // greater than 1 insertions
66-
['pale', 'bake'], // greater than 1 replacements
67-
['pale', 'balpe'], // 1 insertion, 1 replacement
68-
['pale', 'plo'], // 1 deletion, 1 replacement
69-
['pale', 'ales'] // 1 deletion, 1 insertion
70-
],
71-
false
72-
);
65+
it('should handle negative cases', () => {
66+
runTests(
67+
[
68+
['pale', 'ae'], // greater than 1 deletions
69+
['pale', 'ppalpe'], // greater than 1 insertions
70+
['pale', 'bake'], // greater than 1 replacements
71+
['pale', 'balpe'], // 1 insertion, 1 replacement
72+
['pale', 'plo'], // 1 deletion, 1 replacement
73+
['pale', 'ales'] // 1 deletion, 1 insertion
74+
],
75+
false
76+
);
77+
});
78+
});

JavaScript/chapter01/1.6 - String Compression/rroque98_sol.js

+15-7
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ const stringCompression = (str) => {
2929
return compStr;
3030
}
3131

32-
// TESTS
33-
console.log(stringCompression('aabcccccaaa') === 'a2b1c5a3');
34-
console.log(stringCompression('cccccccc') === 'c8');
35-
console.log(stringCompression('') === '');
36-
console.log(stringCompression('AabccCccaaa') === 'AabccCccaaa');
37-
// Explanation: 'A1a1b1c2C1c2a3' length is longer than original string so returns original string
38-
console.log(stringCompression('x') === 'x');
32+
const assert = require('assert');
33+
34+
describe(module.filename, () => {
35+
it('should correctly compress longer strings', () => {
36+
assert.equal(stringCompression('aabcccccaaa'), 'a2b1c5a3');
37+
assert.equal(stringCompression('cccccccc'), 'c8');
38+
});
39+
40+
it('should leave uncompressible strings unmodified', () => {
41+
assert.equal(stringCompression(''), '');
42+
// Explanation: 'A1a1b1c2C1c2a3' length is longer than original string so returns original string
43+
assert.equal(stringCompression('AabccCccaaa'), 'AabccCccaaa');
44+
assert.equal(stringCompression('x'), 'x');
45+
});
46+
});

JavaScript/chapter01/1.7 - Rotate Matrix/rroque98_sol.js

+13-7
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,16 @@ const rotateImage = nestedArr => {
2020
};
2121

2222
// TESTS:
23-
console.log(
24-
JSON.stringify(rotateImage([[1, 2, 3], [4, 5, 6], [7, 8, 9]])) ===
25-
JSON.stringify([[7, 4, 1], [8, 5, 2], [9, 6, 3]])
26-
);
27-
console.log(JSON.stringify(rotateImage([[1]])) === JSON.stringify([[1]]));
28-
console.log(JSON.stringify(rotateImage([[]])) === JSON.stringify([[]]));
29-
console.log(JSON.stringify(rotateImage([])) === JSON.stringify([]));
23+
const assert = require('assert');
24+
25+
describe(module.filename, () => {
26+
it('should rotate matrices', () => {
27+
assert.deepEqual(
28+
rotateImage([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
29+
[[7, 4, 1], [8, 5, 2], [9, 6, 3]]
30+
);
31+
assert.deepEqual(rotateImage([[1]]), [[1]]);
32+
assert.deepEqual(rotateImage([[]]), [[]]);
33+
assert.deepEqual(rotateImage([]), []);
34+
});
35+
});

JavaScript/chapter01/1.8 - Zero Matrix/rroque98_sol.js

+28-33
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,38 @@ function checkForZeroIndex(nestArr) {
3434
}
3535

3636
// **** TESTS ****:
37-
let actual = zeroMatrix([[]]);
38-
let expected = [[]];
39-
isEqual(actual, expected);
37+
const assert = require('assert');
4038

41-
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]);
42-
expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]];
43-
isEqual(actual, expected);
39+
describe(module.filename, () => {
40+
it('should correctly zero out matrices', () => {
41+
let actual = zeroMatrix([[]]);
42+
let expected = [[]];
43+
assert.deepEqual(actual, expected);
4444

45-
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]);
46-
expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
47-
isEqual(actual, expected);
45+
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 5], [2, 2, 2]]);
46+
expected = [[3, 0, 6], [0, 0, 0], [4, 0, 5], [2, 0, 2]];
47+
assert.deepEqual(actual, expected);
4848

49-
actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]);
50-
expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
51-
isEqual(actual, expected);
49+
actual = zeroMatrix([[3, 5, 6], [1, 0, 2], [4, 4, 0], [2, 0, 2]]);
50+
expected = [[3, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
51+
assert.deepEqual(actual, expected);
5252

53-
function isEqual(actual, expected) {
54-
console.log(JSON.stringify(actual) === JSON.stringify(expected));
55-
}
56-
57-
// ****HELPER FUNCTION TESTS ****:
58-
actual = checkForZeroIndex([[1, 2, 0]]);
59-
expected = { rows: { 0: true }, columns: { 2: true } };
60-
testCheckForZeroIndex(actual, expected);
53+
actual = zeroMatrix([[3, 5, 6], [0, 0, 2], [4, 4, 0], [2, 0, 2]]);
54+
expected = [[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]];
55+
assert.deepEqual(actual, expected);
56+
});
6157

62-
actual = checkForZeroIndex([[0]]);
63-
expected = { rows: { 0: true }, columns: { 0: true } };
64-
testCheckForZeroIndex(actual, expected);
58+
it('should correctly find zero indices', () => {
59+
actual = checkForZeroIndex([[1, 2, 0]]);
60+
expected = { rows: { 0: true }, columns: { 2: true } };
61+
assert.deepEqual(actual, expected);
6562

66-
actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]);
67-
expected = { rows: { 1: true, 2: true }, columns: { 1: true } };
68-
testCheckForZeroIndex(actual, expected);
63+
actual = checkForZeroIndex([[0]]);
64+
expected = { rows: { 0: true }, columns: { 0: true } };
65+
assert.deepEqual(actual, expected);
6966

70-
function testCheckForZeroIndex(actual, expected) {
71-
let rows = JSON.stringify(actual.rows);
72-
let cols = JSON.stringify(actual.columns);
73-
let expRows = JSON.stringify(expected.rows);
74-
let expCols = JSON.stringify(expected.columns);
75-
console.log(rows === expRows && cols === expCols);
76-
}
67+
actual = checkForZeroIndex([[1, 2, 3], [4, 0, 5], [6, 0, 8]]);
68+
expected = { rows: { 1: true, 2: true }, columns: { 1: true } };
69+
assert.deepEqual(actual, expected);
70+
});
71+
});

JavaScript/chapter01/1.9 - String Rotation/solution.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@ call to isSubstring (e.g., "waterbottle" is a rotation of"erbottlewat") */
44

55
var StringRotate = function(string1, string2) {
66
if (string1.length !== string2.length ){
7-
return false;
7+
return false;
88
}
99
return ( string2 + string1 ).includes(string1); // one call of Substring
1010
};
1111

12-
//Test
13-
console.log(StringRotate('waterbottle', 'erbottlewat'), true);
12+
//Test
13+
const assert = require('assert');
14+
15+
describe(module.filename, () => {
16+
it('should detect rotated substrings', () => {
17+
assert.equal(StringRotate('waterbottle', 'erbottlewat'), true);
18+
});
19+
});

0 commit comments

Comments
 (0)