Conditions with 'every'
Part of the Logic & Flow section of Coddy's Dart journey — lesson 14 of 65.
While any() checks if at least one element meets a condition, sometimes you need to verify that all elements in a list satisfy a specific requirement. The every() method provides this functionality by testing every single element against your condition and returning true only if they all pass.
The every() method takes a test function that returns true or false for each element. If any element fails the test, every() immediately returns false. Only when every element passes the test does it return true.
List<int> examScores = [75, 82, 68, 91, 77];
// Check if all students passed (score > 60)
bool allPassed = examScores.every((score) => score > 60);
print(allPassed); // trueThis method is particularly useful for validation scenarios where you need to ensure that all items meet certain criteria before proceeding. The every() method stops checking as soon as it finds an element that fails the test, making it efficient for large lists where you need universal compliance.
Challenge
EasyCreate a program that manages a quality control system by verifying that all products in a batch meet the required standards. Your program should:
- Read a string input representing the factory name
- Read multiple integer inputs representing product quality scores (the input will end when you receive
-1) - Read an integer input representing the minimum quality standard threshold
- Use the
every()method to check if all product scores are greater than or equal to the minimum threshold - Print the quality control results in the exact format shown below
For example, if the factory name is "Electronics Factory", the quality scores are 85, 92, 78, 88, 95, and the minimum threshold is 75, your program should output:
Factory: Electronics Factory
Quality scores: [85, 92, 78, 88, 95]
Minimum standard: 75
Checking if all scores >= 75
All products passed: true
Status: Batch approved for shipmentIf the factory name is "Textile Mill", the quality scores are 68, 82, 74, 91, and the minimum threshold is 70, your program should output:
Factory: Textile Mill
Quality scores: [68, 82, 74, 91]
Minimum standard: 70
Checking if all scores >= 70
All products passed: false
Status: Batch rejected - quality improvement requiredIf the factory name is "Food Processing", the quality scores are 95, 98, 92, and the minimum threshold is 90, your program should output:
Factory: Food Processing
Quality scores: [95, 98, 92]
Minimum standard: 90
Checking if all scores >= 90
All products passed: true
Status: Batch approved for shipmentYour program must use the every() method to verify that all quality scores are greater than or equal to the minimum threshold. Display "Batch approved for shipment" if all products meet the standard, or "Batch rejected - quality improvement required" if any product fails to meet the minimum requirement.
Cheat sheet
The every() method checks if all elements in a list satisfy a specific condition. It returns true only if every element passes the test, and false if any element fails.
List<int> examScores = [75, 82, 68, 91, 77];
// Check if all students passed (score > 60)
bool allPassed = examScores.every((score) => score > 60);
print(allPassed); // trueThe every() method stops checking as soon as it finds an element that fails the test, making it efficient for large lists where you need universal compliance.
Try it yourself
import 'dart:io';
void main() {
// Read factory name
String? factoryName = stdin.readLineSync();
// Read quality scores until -1 is encountered
List<int> qualityScores = [];
while (true) {
int score = int.parse(stdin.readLineSync()!);
if (score == -1) {
break;
}
qualityScores.add(score);
}
// Read minimum threshold
int minThreshold = int.parse(stdin.readLineSync()!);
// TODO: Write your code below
// Use the every() method to check if all scores meet the minimum standard
// Print the results in the required format
print('Factory: $factoryName');
print('Quality scores: $qualityScores');
print('Minimum standard: $minThreshold');
print('Checking if all scores >= $minThreshold');
// Print whether all products passed and the final status
}This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Logic & Flow
1Advanced List Manipulation
List Properties: first & lastList State: isEmpty & isNotEmpReversing a ListAdding to a List: insertList Removal: removeWhereFinding in a List: indexOfSorting a ListShuffling a ListRecap - List Organizer4Advanced Map Manipulation
Iterating Over a MapChecking for Keys and ValuesMap Properties: keys & valuesConditional Add: putIfAbsentRemoving Entries from a MapNested MapsRecap - Inventory Update2Functional List Operations
Transforming with 'map'Filtering with 'where'Using '.toList()'Checking Conditions with 'any'Conditions with 'every'Finding with 'firstWhere'Recap - Data Filtering5Project: Shopping Cart Calc
Project SetupAdding Items to the Cart3Sets
What is a Set?Creating a SetAdding and Removing from SetsChecking for Elements in a SetConverting a List to a SetSet UnionSet IntersectionSet DifferenceRecap - Unique Guest List