Menu
Coddy logo textTech

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);  // true

This 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 icon

Challenge

Easy

Create a program that manages a quality control system by verifying that all products in a batch meet the required standards. Your program should:

  1. Read a string input representing the factory name
  2. Read multiple integer inputs representing product quality scores (the input will end when you receive -1)
  3. Read an integer input representing the minimum quality standard threshold
  4. Use the every() method to check if all product scores are greater than or equal to the minimum threshold
  5. 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 shipment

If 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 required

If 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 shipment

Your 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);  // true

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.

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
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Logic & Flow