# Daily Challenge #312 - Describe the Function

You have to create a function calcType, which receives 3 arguments: 2 numbers, and the result of an unknown operation performed on them (also a number).

Based on those 3 values you have to return a string, that describes which operation was used to get the given result.

The possible return strings are: "addition", "subtraction", "multiplication", "division".

In case of division you should expect that the result of the operation is obtained by using / operator on the input values

### Example:

`calcT_type(1, 2, 3)` --> `1 ? 2 = 3` --> "addition"

### Tests

calc_type(1, 2, 3)
calc_type(10, 5, 5)
calc_type(10, 4, 40)
calc_type(9, 5, 1.8)

Good luck!

This challenge comes from ridokai on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions! # JavaScript

stupidly golfed:

``````calc_type=(a,b,c)=>[...'+-*/'].find(x=>eval(a+x+b+'=='+c))
``````

reasonable:

``````const ops = '+-*/'.split('').map(x => [x, new Function('a', 'b', 'c', `return (a \${x} b) === c`)])
const calc_type = (a, b, c) => ops.find(([x,f]) => f(a,b,c))?.
``````

optimized:

``````function calc_type(a, b, c) {
switch (c) {
case a + b: return '+'
case a - b: return '-'
case a * b: return '*'
case a / b: return '/'
}
}
``````

Guess what, flow control is expensive! Inlining saves the day, and `case` is number one.

Both of these are slower, despite not having to calculate the "expensive" division operation:

``````const ops = [
(a, b) => a + b,
(a, b) => a - b,
(a, b) => a * b,
(a, b) => a / b,
]
const calc_type = (a, b, c) => strs[ops.findIndex(f => f(a, b) === c)]
``````
``````const ops = [
(a, b, c) => (a + b) === c,
(a, b, c) => (a - b) === c,
(a, b, c) => (a * b) === c,
(a, b, c) => (a / b) === c,
]
const calc_type = (a, b, c) => strs[ops.findIndex(f => f(a, b, c))]
`````` bugb

``````2 + 2=4
``````

and :

``````2 * 2 = 4
``````

Or one of these number is 0 there's only

• `x + 0 == x - 0 (== x)`
• `x * 1 == x / 1 (== x)`

• `2 + 2 == 2 * 2`

• `4 - 2 == 4 / 2` (reverse)

Right? What did you mean with

one of these numbers is 0 bugb

You are correct, I mean:

``````±n / 0 == ±∞
0 * 0 == 0 + 0 == 0 - 0
`````` If we're talking IEEE754-like floats, there's also NaN :v

• `NaN + a == NaN - b == NaN * c == NaN / d == NaN % e == f + NaN == g - NaN == h * NaN == i / NaN == j % NaN (== NaN)`
• `inf + a == inf - b == inf * c == inf / d == f + inf == h * inf (== inf)`
• `-inf + a == -inf - b == -inf * c == -inf / d == f + -inf == h * -inf (== -inf)`
• `inf - inf == inf / inf (== NaN)` peter279k

Python:

``````def calc_type(a, b, res):
if a + b == res:
if a - b == res:
return "subtraction"
if a * b == res:
return "multiplication"
if a / b == res:
return "division"
`````` Python:

``````def calc_type(a, b, res):
if a + b == res:    return "addition"
elif a - b == res:  return "subtraction"
elif a / b == res:  return "division"
elif a * b == res:  return "multiplication"
else:   return "Error 404"
``````