Recap - Product List
Part of the Fundamentals section of Coddy's Swift journey — lesson 66 of 86.
Challenge
EasyWrite a function manageProducts that takes products, newProduct, and insertIndex and returns the modified product list after performing a series of operations.
Logic:
- Append
newProductto the end of the array - Insert
"SALE"at the specifiedinsertIndex - Remove the first element from the array
- Sort the array alphabetically
Parameters:
products([String]): The initial array of product namesnewProduct(String): The product to appendinsertIndex(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:
- After append:
["Milk", "Bread", "Eggs", "Butter"] - After insert at index 1:
["Milk", "SALE", "Bread", "Eggs", "Butter"] - After removing first:
["SALE", "Bread", "Eggs", "Butter"] - 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
4Operators Part 1
Arithmetic OperatorsModulo OperatorCompound AssignmentRecap - Simple MathComparison Operators7Basic IO
Print FunctionString InterpolationReadLine InputType ConversionRecap - Till 120Recap - True or False10Functions
Declare A FunctionParameters And ArgumentsReturn ValuesArgument LabelsRecap - Sigma FunctionRecap - Validation FunctionDefault Values13Iterating Over Sequences
Iterating Over ElementsThe Enumerated MethodIterating Over Strings P1Iterating Over Strings P22Variables
Let vs VarType AnnotationsNumbersStringBooleanNaming ConventionsRecap - Initialize Variables5Operators Part 2
Logical Operators Part 1Logical Operators Part 2Recap - Simple LogicLogical Operators Part 3Ternary Operator8Bill Split Calculator
Welcome MessageGetting Input3Optionals
What Are OptionalsUnwrapping With If LetGuard LetNil Coalescing OperatorRecap - Safe Unwrapping9Loops
For-In LoopWhile LoopRepeat-While LoopBreakContinueRecap - FactorialRanges In LoopsNested LoopRecap - Dynamic Input12Arrays Basics
Declaring An ArrayAccessing ElementsModifying ArraysArray MethodsRecap - Product ListRecap - Reversed ArrayTuples