Challenge: Sum of numbers
Part of the Fundamentals section of Coddy's Dart journey — lesson 93 of 94.
Challenge
EasyIn this challenge, you'll create a function that calculates the sum of numbers in a list based on certain conditions.
You have a list of integers, and you need to complete the sumWithCondition function that adds up only the numbers that meet specific criteria.
The function should:
- Calculate the sum of all numbers in the list that are greater than the
thresholdvalue - Stop adding numbers immediately after the sum exceeds the
maxSumvalue (the final sum may be greater thanmaxSum) - Return the final sum
Important: The function stops adding numbers after adding a number that causes the sum to exceed maxSum. This means the final result can be greater than maxSum.
Example: If numbers greater than threshold are [15, 20, 25, 30] and maxSum is 50:
- Add 15: sum = 15 (continue)
- Add 20: sum = 35 (continue)
- Add 25: sum = 60 (exceeds 50, so stop here)
- Result: 60 (not 50)
Try it yourself
void main() {
// Test cases
List<int> numbers = [5, 10, 15, 20, 25, 30];
// Sum numbers greater than 12, with max sum of 50
int result = sumWithCondition(numbers, 12, 50);
print('Sum of numbers > 12 (max 50): $result');
// Sum numbers greater than 8, with max sum of 100
result = sumWithCondition(numbers, 8, 100);
print('Sum of numbers > 8 (max 100): $result');
}
// TODO: Complete this function to sum numbers based on conditions
int sumWithCondition(List<int> numbers, int threshold, int maxSum) {
int sum = 0;
// TODO: Loop through the numbers list
// TODO: Add only numbers greater than threshold to sum
// TODO: Stop adding if sum exceeds maxSum
return sum;
}All lessons in Fundamentals
4Operators Part 2
Comparison OperatorsLogical ANDLogical ORLogical NOTType Test OperatorsRecap - Making Comparisons7Working with Strings
String ConcatenationString InterpolationMulti-line StringsString PropertiesBasic String Methods10Collections - Maps Basics
What are Maps?Creating a MapAccessing Values by KeyKey-Value PairsGetting Map SizeGetting KeysGetting ValuesChecking if a Key Exists13Null Safety In Depth
Understanding NullNullable TypesNon-Nullable TypesNull Assertion OperatorLate InitializationRecap - Handling Null Safely16Fundamentals Challenges
Challenge: List of calculationChallenge: Sum of numbersChallenge: Find product2Variables and Basic Data Types
What are Variables?StringsIntegers (int)Doubles (double)Booleans (bool)Type Inference with 'var'Final VariablesConstant VariablesNaming ConventionsBasic Null SafetyRecap - Declaring Variables8Control Flow - Loops
The 'for' LoopThe 'while' LoopThe 'do-while' LoopUsing 'break' in LoopsUsing 'continue' in LoopsRecap - Repeating Code3Operators Part 1
Arithmetic OperatorsInteger DivisionModulo OperatorIncrement and DecrementAssignment ShortcutsRecap - Simple Calculations6Control Flow - Decision Making
The 'if' StatementThe 'else' StatementThe 'else if' StatementRecap - Simple DecisionsNested 'if' StatementsThe 'switch' Statement9Collections - Lists Basics
What are Lists?Creating a ListAccessing by IndexGetting List LengthAdding ElementsRemoving ElementsChecking if a List is EmptyIterating Over a List12Functions Advanced
Optional Positional ParametersNamed ParametersRequired Named ParametersDefault Parameter ValuesRecap - Function Parameters15Project: Simple Calculator
Setting UpDeclaring Number