Recap - Simple Calculator
Part of the Fundamentals section of Coddy's Swift journey — lesson 28 of 86.
Challenge
EasyWrite a function calculate that takes a, b, and operation and returns the result of the mathematical operation.
Based on the operator symbol, perform the corresponding calculation on the two numbers.
Supported Operations:
"+"- return the sum of a and b"-"- return the difference (a minus b)"*"- return the product of a and b"/"- return the quotient (a divided by b, integer division)- Any other operation - return
0
Parameters:
a(Int): The first numberb(Int): The second numberoperation(String): The operator symbol
Returns: The result of the calculation, or 0 for unknown operations (Int)
Try it yourself
func calculate(a: Int, b: Int, operation: String) -> Int {
// 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 Input