oneby7

Foundations Conditions

587. Simple Calculator

Easy

You are given two integers a and b and an operator op, which is one of "+", "-", "*" and "/". Return the result of a op b: the sum, the difference, the product or the quotient.

Division keeps only the whole part of the quotient and rounds toward zero. So 7 / 2 is 3, and −7 / 2 is −3, not −4.

The answer can be larger than 253, so the JavaScript starter returns a BigInt.

Example 1

Input: a = -17, b = 5, op = "/"

Output: -3

Explanation: −17 / 5 is −3.4. Keeping the whole part gives −3. Rounding down would give −4, which is not the answer.

Example 2

Input: a = 15, b = -4, op = "-"

Output: 19

Explanation: Subtracting −4 is the same as adding 4, so 15 − (−4) = 19.

Example 3

Input: a = 1000000000, b = -999999999, op = "*"

Output: -999999999000000000

Check your understanding

Work out the answer for this input before you write any code.

a = -9, b = 4, op = "/"

Constraints

  • -109 ≤ a, b ≤ 109
  • op is one of "+", "-", "*" and "/".
  • b is not 0 when op is "/".

Clarifying questions

What you would ask an interviewer before writing any code.

Hints

Interview follow-ups

Runs in your browser
Code
Runs in your browser for now · some things may not work as expectedLn 1, Col 1

Article

Send feedback

Thank you for helping. We read every report and fix what is wrong.

How do you feel about oneby7?

Sign in to oneby7

Keep your progress, your streak and your code, on any device.

New here? This creates your account too.