Menu
Coddy logo textTech

Recap - Simple Calculator

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

challenge icon

Challenge

Easy
Write 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 number
  • b (Int): The second number
  • operation (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