Menu
Coddy logo textTech

Transforming with 'map'

Part of the Logic & Flow section of Coddy's Dart journey — lesson 10 of 65.

When you need to transform every element in a list into something new, Dart provides the powerful map() method. This method applies a transformation function to each element in the list and creates a new list containing all the transformed results.

The map() method takes a function as its parameter that defines how each element should be transformed. This function receives each element from the original list and returns the transformed version. The map() method then collects all these transformed values into a new list.

List<int> numbers = [1, 2, 3, 4, 5];

// Transform each number by doubling it
var doubled = numbers.map((number) => number * 2);
print(doubled);  // (2, 4, 6, 8, 10)

This approach is much more elegant than manually creating a new list and using a loop to transform each element. The map() method represents a fundamental concept in functional programming, allowing you to express data transformations in a clear and concise way.

challenge icon

Challenge

Easy

Create a program that manages a product pricing system by applying percentage-based price adjustments to all items in a store inventory. Your program should:

  1. Read a string input representing the store name
  2. Read multiple decimal inputs representing original product prices (the input will end when you receive -1)
  3. Read a decimal input representing the percentage adjustment (positive for price increase, negative for discount)
  4. Use the map() method to transform each price by applying the percentage adjustment
  5. Print the pricing adjustment results in the exact format shown below

For example, if the store name is "Tech Store", the prices are 99.99, 149.50, 79.99, 199.99, and the adjustment is 15.0 (15% increase), your program should output:

Store: Tech Store
Original prices: [99.99, 149.5, 79.99, 199.99]
Applying 15.0% adjustment
Adjusted prices: (114.9885, 171.925, 91.9885, 229.9885)
Total items processed: 4
Status: Price adjustment completed

If the store name is "Fashion Outlet", the prices are 29.99, 45.00, and the adjustment is -20.0 (20% discount), your program should output:

Store: Fashion Outlet
Original prices: [29.99, 45.0]
Applying -20.0% adjustment
Adjusted prices: (23.992, 36.0)
Total items processed: 2
Status: Price adjustment completed

If the store name is "Book Corner" and only one price 12.50 is provided with adjustment 0.0, your program should output:

Store: Book Corner
Original prices: [12.5]
Applying 0.0% adjustment
Adjusted prices: (12.5)
Total items processed: 1
Status: Price adjustment completed

Your program must use the map() method with a transformation function that calculates the new price using the formula: originalPrice * (1 + percentage / 100). The map() method should return an iterable containing all the adjusted prices, which will be displayed in parentheses format.

Cheat sheet

The map() method transforms every element in a list by applying a function to each element and creating a new list with the results.

The map() method takes a function as parameter that defines the transformation. This function receives each element and returns the transformed version:

List<int> numbers = [1, 2, 3, 4, 5];

// Transform each number by doubling it
var doubled = numbers.map((number) => number * 2);
print(doubled);  // (2, 4, 6, 8, 10)

This is more elegant than manually creating a new list and using loops for transformation.

Try it yourself

import 'dart:io';

void main() {
  // Read store name
  String? storeName = stdin.readLineSync();
  
  // Read prices until -1 is encountered
  List<double> prices = [];
  while (true) {
    String? input = stdin.readLineSync();
    double price = double.parse(input!);
    if (price == -1) {
      break;
    }
    prices.add(price);
  }
  
  // Read percentage adjustment
  String? adjustmentInput = stdin.readLineSync();
  double adjustment = double.parse(adjustmentInput!);
  
  // TODO: Write your code below
  // Use the map() method to apply percentage adjustment to all prices
  // Formula: originalPrice * (1 + percentage / 100)
  
  // Output the results
  print('Store: $storeName');
  print('Original prices: $prices');
  print('Applying $adjustment% adjustment');
  // Print adjusted prices in parentheses format
  // Print total items processed
  // Print status message
}
quiz iconTest yourself

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

All lessons in Logic & Flow