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 1f61be6

Browse files
Merge pull request #6 from somekindofwallflower/feature/reverse_integer
Implement the solution for reverse integer problem
2 parents d72c70f + 7c06348 commit 1f61be6

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

‎03_integer_reverse/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// --- Directions
2+
// Given an integer, return an integer that is the reverse
3+
// ordering of numbers.
4+
// --- Examples
5+
// reverseInt(15) === 51
6+
// reverseInt(981) === 189
7+
// reverseInt(500) === 5
8+
// reverseInt(-15) === -51
9+
// reverseInt(-90) === -9
10+
11+
// Tips to solve the problem
12+
// - Convert number to string by using toString function
13+
// - Check if it is a sign number or not by using Math.sign() function.
14+
// - Convert string to number by using parseInt method
15+
16+
function reverseInt(n) {
17+
const reversed = n
18+
.toString()
19+
.split('')
20+
.reverse()
21+
.join('');
22+
return parseInt(reversed) * Math.sign(n);
23+
}
24+
25+
26+
module.exports = reverseInt;

‎03_integer_reverse/test.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const reverseInt = require('./index');
2+
3+
test('ReverseInt function exists', () => {
4+
expect(reverseInt).toBeDefined();
5+
});
6+
7+
test('ReverseInt handles 0 as an input', () => {
8+
expect(reverseInt(0)).toEqual(0);
9+
});
10+
11+
test('ReverseInt flips a positive number', () => {
12+
expect(reverseInt(5)).toEqual(5);
13+
expect(reverseInt(15)).toEqual(51);
14+
expect(reverseInt(90)).toEqual(9);
15+
expect(reverseInt(2359)).toEqual(9532);
16+
});
17+
18+
test('ReverseInt flips a negative number', () => {
19+
expect(reverseInt(-5)).toEqual(-5);
20+
expect(reverseInt(-15)).toEqual(-51);
21+
expect(reverseInt(-90)).toEqual(-9);
22+
expect(reverseInt(-2359)).toEqual(-9532);
23+
});

0 commit comments

Comments
(0)

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