Menu
Coddy logo textTech

Recap - Product List

Part of the Fundamentals section of Coddy's Swift journey — lesson 66 of 86.

challenge icon

Challenge

Easy
Write a function manageProducts that takes products, newProduct, and insertIndex and returns the modified product list after performing a series of operations.

Logic:

  1. Append newProduct to the end of the array
  2. Insert "SALE" at the specified insertIndex
  3. Remove the first element from the array
  4. Sort the array alphabetically

Parameters:

  • products ([String]): The initial array of product names
  • newProduct (String): The product to append
  • insertIndex (Int): The index where "SALE" should be inserted

Returns: The final sorted array after all operations ([String])

Example: If products is ["Milk", "Bread", "Eggs"], newProduct is "Butter", and insertIndex is 1:

  1. After append: ["Milk", "Bread", "Eggs", "Butter"]
  2. After insert at index 1: ["Milk", "SALE", "Bread", "Eggs", "Butter"]
  3. After removing first: ["SALE", "Bread", "Eggs", "Butter"]
  4. After sorting: ["Bread", "Butter", "Eggs", "SALE"]

Try it yourself

func manageProducts(products: [String], newProduct: String, insertIndex: Int) -> [String] {
    // Write code here
}

All lessons in Fundamentals