Skip to content

Commit 4cdcfe4

Browse files
committed
Refactoring
1 parent f058a8b commit 4cdcfe4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+499
-495
lines changed

README.MD

Lines changed: 33 additions & 33 deletions
Large diffs are not rendered by default.

helpers/listNode.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33
*
44
* @constructor
55
* @param {number} val - The numeric value of the node.
6+
* @param {ListNode} [next=null] - The next node in the list. Defaults to null.
67
* @property {number} val - The numeric value of the node.
78
* @property {ListNode | null} next - The next node in the linked list, or null if this is the last node.
89
*/
9-
function ListNode(val) {
10+
function ListNode(val, next = null) {
1011
this.val = val;
11-
this.next = null;
12+
this.next = next;
1213
}
1314

1415
module.exports = ListNode;

solutions/backspaceCompare/index.js renamed to solutions/backspaceCompare/backspaceCompare.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,12 @@
44
*/
55
function modify(str) {
66
let backspaces = 0,
7-
result = '';
7+
result = '';
88

99
for (let i = str.length - 1; i >= 0; i--) {
10-
if (str[i] === '#') {
11-
backspaces++;
12-
} else if (backspaces > 0) {
13-
backspaces--;
14-
} else {
15-
result = str[i] + result;
16-
}
10+
if (str[i] === '#') backspaces++;
11+
else if (backspaces > 0) backspaces--;
12+
else result = str[i] + result;
1713
}
1814

1915
return result;
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const backspaceCompare = require('./backspaceCompare');
2+
3+
describe('Backspace String Compare', () => {
4+
it('returns true for identical strings', () => {
5+
const s = 'abc#d';
6+
const t = 'abc#d';
7+
expect(backspaceCompare(s, t)).toBe(true);
8+
});
9+
10+
it('returns false for different strings', () => {
11+
const s = 'abc#d';
12+
const t = 'abc##d';
13+
expect(backspaceCompare(s, t)).toBe(false);
14+
});
15+
16+
it('handles empty string inputs', () => {
17+
const s = '';
18+
const t = '';
19+
expect(backspaceCompare(s, t)).toBe(true);
20+
});
21+
22+
it('handles inputs with only backspaces', () => {
23+
const s = '##';
24+
const t = '###';
25+
expect(backspaceCompare(s, t)).toBe(true);
26+
});
27+
28+
it('handles inputs with only characters', () => {
29+
const s = 'abcd';
30+
const t = 'abcd';
31+
expect(backspaceCompare(s, t)).toBe(true);
32+
});
33+
34+
it('handles inputs with mixed characters and backspaces', () => {
35+
const s = 'a##c';
36+
const t = '#a#c';
37+
expect(backspaceCompare(s, t)).toBe(true);
38+
});
39+
});

solutions/backspaceCompare/index.spec.js

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const climbStairs = require('./climbStairs');
2+
3+
describe('Climbing Stairs', () => {
4+
it('climbStairs returns the correct result', () => {
5+
expect(climbStairs(1)).toEqual(1);
6+
expect(climbStairs(2)).toEqual(2);
7+
expect(climbStairs(3)).toEqual(3);
8+
expect(climbStairs(4)).toEqual(5);
9+
expect(climbStairs(5)).toEqual(8);
10+
expect(climbStairs(6)).toEqual(13);
11+
});
12+
13+
it('returns the correct result for large values of n', () => {
14+
expect(climbStairs(45)).toBe(1836311903);
15+
});
16+
});

solutions/climbStairs/index.spec.js

Lines changed: 0 additions & 21 deletions
This file was deleted.

solutions/containsDuplicate/index.js renamed to solutions/containsDuplicate/containsDuplicate.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ function containsDuplicate(nums) {
66
const set = new Set();
77

88
for (let i = 0; i < nums.length; i++) {
9-
if (set.has(nums[i])) {
10-
return true;
11-
}
9+
if (set.has(nums[i])) return true;
1210
set.add(nums[i]);
1311
}
1412

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const containsDuplicate = require('./containsDuplicate');
2+
3+
describe('Contains Duplicate', () => {
4+
test('should return true if array contains duplicate numbers', () => {
5+
expect(containsDuplicate([1, 2, 3, 1])).toBe(true);
6+
expect(containsDuplicate([1, 2, 3, 4, 5, 6, 7, 8, 9, 9])).toBe(true);
7+
expect(containsDuplicate([1, 1])).toBe(true);
8+
});
9+
10+
test('should return false if array does not contain duplicate numbers', () => {
11+
expect(containsDuplicate([1, 2, 3, 4])).toBe(false);
12+
expect(containsDuplicate([1])).toBe(false);
13+
expect(containsDuplicate([])).toBe(false);
14+
});
15+
});

0 commit comments

Comments
 (0)