voidmain(){group("Eradicate trailing zeroes :",(){finalzeroesToHeroes={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",(){expect(eradicateTrailingZeroes(zero),equals(hero));});});});}numeradicateTrailingZeroes(numnumber){for(;(number%10==0)^(number==0);number/=10){}returnnumber;}
intremoveZeros(intnumber){// if number is 0 return 0if(number==0)return0;// if number is not 0 then divide the number by 10 until last digit of number is non-zerowhile(number%10==0){number/=10;}returnnumber;}
staticvoidMain(string[]args){System.Console.WriteLine("No Zero Challenge");Console.WriteLine(Eradicate(1450));//145Console.WriteLine(Eradicate(960000));//96Console.WriteLine(Eradicate(1050));//105Console.WriteLine(Eradicate(-1050));//-105Console.WriteLine(Eradicate(9070));//907Console.WriteLine(Eradicate(210000));//21Console.WriteLine(Eradicate(10210));//1021Console.WriteLine(Eradicate(0));//0Console.WriteLine(Eradicate(1234));//1234}publicstaticintEradicate(intnumber){if(number==0)returnnumber;while(number.ToString()[^1]=='0')number/=10;returnnumber;}
Recursive Haskell solution
Does this work when passing
0as argument?Good catch, guess I missed that case. Updated it.
Haskell
And the output:
Elixir
Elixir with recursion:
Elixir without recursion, inspired by @avalander 's solution:
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).
Javascript
I just realized the function returns undefined when it's not 0 so let me try to fix that real quick lol
codepen
Can someone tell my why the recursion I tried to do didn't work?
I guess you just need to add a 'return' in front of
The code will be--
Yep, that does it. Thanks!
A Dart solution using TDD :
Elm
C++
Python one liner
C#
and Dart
Felt like a bit of recursion is always fun to do.
And I couldn't remember when I used extensions in C# last...
Project on GitHub
Ruby
Regex
Recursion
TESTS
Nice solution :)
Take a loot at
stripmethod fromstrobjects;dev.to/rafaacioly/comment/m1ib
TypeScript
Python
Java with recursion
Javascript
Python solution 🐍