DEV Community

Discussion on: Daily Challenge #195 - No Zeroes for Heroes

Collapse
corentinleffy profile image
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", () {
        expect(eradicateTrailingZeroes(zero), equals(hero));
      });
    });
  });
}

num eradicateTrailingZeroes(num number) {
  for (; (number % 10 == 0) ^ (number == 0); number /= 10) {}
  return number;
}