From 039b195e7abc4b8ea22ab4e18b8385f1f4cae18c Mon Sep 17 00:00:00 2001 From: Roland Geider Date: Tue, 15 Jun 2021 17:33:39 +0200 Subject: [PATCH] Fix error in plate calculator The smallest plate was never being taken into consideration, as the loop never reached it --- lib/helpers/gym_mode.dart | 8 ++++---- test/plate_calculator_test.dart | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/helpers/gym_mode.dart b/lib/helpers/gym_mode.dart index 371a936a..89deee51 100644 --- a/lib/helpers/gym_mode.dart +++ b/lib/helpers/gym_mode.dart @@ -26,16 +26,16 @@ List plateCalculator(num totalWeight, num barWeight, List plates) { return []; } + // Remove the bar and divide by two to get weight on each side + totalWeight = (totalWeight - barWeight) / 2; + // Weight can't be divided with the smallest plate if (totalWeight % plates.first > 0) { return []; } - // Remove the bar and divide by two to get weight on each side - totalWeight = (totalWeight - barWeight) / 2; - // Find the plates - for (int i = (platesCount - 1); i > 0; i--) { + for (int i = (platesCount - 1); i >= 0; i--) { var plate = plates[i]; while (totalWeight >= plate) { diff --git a/test/plate_calculator_test.dart b/test/plate_calculator_test.dart index 34b4a614..6a946a51 100644 --- a/test/plate_calculator_test.dart +++ b/test/plate_calculator_test.dart @@ -25,6 +25,7 @@ void main() { test('Regular weights', () async { expect(plateCalculator(40, BAR_WEIGHT, AVAILABLE_PLATES), [10]); expect(plateCalculator(100, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 10]); + expect(plateCalculator(102.5, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 10, 1.25]); expect(plateCalculator(140, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 15, 15]); expect(plateCalculator(45, BAR_WEIGHT, AVAILABLE_PLATES), [10, 2.5]); expect(plateCalculator(85, BAR_WEIGHT, AVAILABLE_PLATES), [15, 15, 2.5]);