# Learning Algorithms with JS, Python and Java 3: Integer Reversal

### tommy-3 ・1 min read

I joined dev.to this week and posted two little articles. Thank you for the hearts and unicorns (though I'm still not really sure what unicorns mean)!

The third question in the Stephen Grider's course is to reverse an integer. I'll just quote the directions here.

--- Directions

Given an integer, return an integer that is the reverse ordering of numbers.

--- Examples

reverseInt(15) === 51

reverseInt(981) === 189

reverseInt(500) === 5

reverseInt(-15) === -51

reverseInt(-90) === -9

Stephen's JS solution is:

```
function reverseInt(n) {
const reversed = n
.toString()
.split('')
.reverse()
.join('');
return parseInt(reversed) * Math.sign(n);
}
```

This works because `parseInt('12-')`

returns `12`

.

A neater solution might be:

```
function reverseInt(n) {
const absReversed = Math.abs(n)
.toString()
.split('')
.reverse()
.join('');
return Number(absReversed) * Math.sign(n);
}
```

My attempt in Python:

```
def reverse_int(n):
abs_reversed = str(abs(n))[::-1]
if n > 0:
return int(abs_reversed)
else:
return -int(abs_reversed)
```

int('12-') throws a ValueError, so `abs()`

cannot be omitted.

I wonder if there's a cooler way to do it.

And Java:

```
public static int reverseInt(int n) {
String abs = Integer.toString(Math.abs(n));
String absReversed = new StringBuilder(abs).reverse().toString();
return Integer.parseInt(absReversed) * Integer.signum(n);
}
```

`parseInt('12-')`

throws a NumberFormatException in Java, so `Math.abs()`

cannot be omitted.

May I suggest for Python:

A little bit more concise:

`-`

), then I return it as a negative integer (which is exactly like your method) The advantage is that you can "convert" both positive and negative numbers !examples:

Also, if you want to go deeper and converting both integer and float, you could use something like this !

Which allows: