← Back to challenges

Operations

PythonHardconditionslanguage_fundamentalsmath

Instructions

Write a function that does the following operations: adding, subtracting, dividing, or multiplying values. It is simply referred to as variable operation variable. Of course, the variables have to be defined, but in this challenge the variables will be defined for you. All you have to do is look at the variables, do some string to integer conversions, use some if conditionals, and combine them based on the operation.

The numbers and operation will be given as strings, and you should return the value as a string as well.

Examples

operation("1", "2", "add" ) ➞ "3"

operation("4", "5", "subtract") ➞ "-1"

operation("6", "3", "divide") ➞ "2"

Notes

  • The numbers and operation will be given as strings, and you should also return the value as a string.
  • If the answer is "undefined", return "undefined" (e.g. dividing by zero).
  • For divide, go ahead and round down to an integer.
python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.
Next: Match the Last Item