# Daily Challenge #195 - No Zeroes for Heroes

### Setup

It has officially been decided that numbers that end with zeroes are boring. They might be fun in your world, but definitely not here. Implement a function to eradicate any trailing zeroes. If the given number is 0, just leave him alone. Poor guy anyway.

1450 -> 145
960000 -> 96
1050 -> 105
-1050 -> -105

### Tests

9070
210000
10210
0

This challenge comes from thecodix on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

## Discussion  Avalander

``````import Data.List (dropWhileEnd)

noZeroes :: Int -> Int
noZeroes 0 = 0
noZeroes n = (read . dropWhileEnd (== '0') . show) n
``````

And the output:

``````main = do
print (noZeroes 0)       -- 0
print (noZeroes 9070)    -- 907
print (noZeroes 210000)  -- 21
print (noZeroes 10210)   -- 1021
print (noZeroes 1450)    -- 145
print (noZeroes 960000)  -- 96
print (noZeroes 1050)    -- 105
print (noZeroes (-1050)) -- -105
`````` SavagePixie

Elixir

Elixir with recursion:

``````def dezerofy(0), do: 0
def dezerofy(n), do: _dezerofy(n, rem(n, 10))
defp _dezerofy(n, 0), do: _dezerofy(div(n, 10), rem(n, 100))
defp _dezerofy(n, _), do: n
``````

Elixir without recursion, inspired by @avalander 's solution:

``````def dezerofy(0), do: 0
def dezerofy(n), do: n
|> Integer.digits
|> Enum.reverse
|> Enum.drop_while(&(&1 == 0))
|> Enum.reverse
|> Integer.undigits
`````` Cent

Javascript

``````
const removeTrailingZeros = (number) => {
if (number === 0) return number;
if (number%10 === 0 ) removeTrailingZeros(number/10);
else return number;
}

``````

I just realized the function returns undefined when it's not 0 so let me try to fix that real quick lol

``````
const removeTrailingZeros = (number) => {
while(number % 10 === 0 && number !== 0) {
number= number / 10;
}
return number;
}

``````

codepen

Can someone tell my why the recursion I tried to do didn't work? zenmumbler

I find it interesting that every solution here uses strings, here's one acting on the numbers itself. (tested against the above and other values).

``````function deathToZeroes(n) {
while ((Math.abs(n) > 9) && (n % 10 === 0)) {
n /= 10;
}
return n;
}
`````` Nijeesh Joshy

## Ruby

### Regex

``````# using strings
def remove_trailing_zeros(number)
number
.to_s
.gsub(/0{1,}\$/,"")
.to_i || 0
end
``````

### Recursion

``````def remove_trailing_zeros(number)
return 0 if number == 0
return remove_trailing_zeros(number / 10) if number % 10 == 0
return number
end

``````

TESTS

``````require "test/unit"

class RemoveTrailingZerosTest < Test::Unit::TestCase
def test_remove_trailing_zeros
assert_equal 907, remove_trailing_zeros(9070)
assert_equal 21, remove_trailing_zeros(210000)
assert_equal 1021, remove_trailing_zeros(10210)
assert_equal 0, remove_trailing_zeros(0)
end
end

`````` Amin

Elm

``````eradicateZeros : Int -> Int
if number == 0 then
number

else if remainderBy 10 number == 0 then

else
number
`````` Vidit Sarkar

C++

``````int removeZeros(int number){
// if number is 0 return 0
if(number == 0)
return 0;

// if number is not 0 then divide the number by 10 until last digit of number is non-zero
while(number%10 == 0){
number /= 10;
}
return number;
}
`````` Vidit Sarkar

Python one liner

``````eradicate = lambda number : 0 if number==0 else int(str(number).strip('0'))

`````` Lucas Frigo de Souza

C#

``````        static void Main(string[] args)
{
System.Console.WriteLine("No Zero Challenge");
}

{
if (number == 0) return number;
while(number.ToString()[^1] == '0')
number /= 10;
return number;
}

`````` Lucas Frigo de Souza

and Dart

``````void main() {
}

if(number == 0) return number;

while(number.remainder(10) == 0)
number /= 10;

return number;
}

`````` Corentin Leffy

A Dart solution using TDD :

``````
void main() {
group("Eradicate trailing zeroes :", () {
final zeroesToHeroes = {
1450: 145,
960000: 96,
1050: 105,
-1050: -105,
9070: 907,
210000: 21,
10210: 1021,
0: 0
};
zeroesToHeroes.forEach((zero, hero) {
test("\$zero should become \$hero", () {