Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 19f2963

Browse files
author
Luciano Medeiros Marcelino
committed
Add tests fot next-greater-element
1 parent c47c084 commit 19f2963

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

‎src/_Problems_/next-greater-element/index.js‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,4 @@ function nextGreaterElement(arr) {
4242
return nextGreater;
4343
}
4444

45-
// eslint-disable-next-line no-console
46-
console.log(nextGreaterElement([4, 6, 3, 2, 8, 1]));
45+
module.exports = { nextGreaterElement };
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { nextGreaterElement } = require('.');
2+
3+
describe('Next greater element', () => {
4+
it('returns next greater elements collection', () => {
5+
const input = [4, 6, 3, 2, 8, 1]
6+
const greaterElements = [6, 8, 8, 8, -1, -1]
7+
8+
expect(nextGreaterElement(input)).toEqual(greaterElements);
9+
});
10+
11+
it('returns and empty collection for an empty array', () => {
12+
expect(nextGreaterElement([])).toEqual([]);
13+
});
14+
15+
it('returns an array with -1 if the input has only one element', () => {
16+
expect(nextGreaterElement([0])).toEqual([-1]);
17+
});
18+
19+
it('returns a collection of -1 if there is no greater element', () => {
20+
const input = [90, 40, 15, 7, -1, -10]
21+
const greaterElements = [-1, -1, -1, -1, -1, -1]
22+
23+
expect(nextGreaterElement(input)).toEqual(greaterElements);
24+
});
25+
26+
it('uses -1 if the numbers are the same', () => {
27+
const input = [90, 90]
28+
const greaterElements = [-1, -1]
29+
30+
expect(nextGreaterElement(input)).toEqual(greaterElements);
31+
});
32+
33+
it('throws an error if the input is not an array', () => {
34+
expect(() => {
35+
nextGreaterElement('xunda')
36+
}).toThrowError('Invalid Argument');
37+
});
38+
});

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /