Arithmetic Expression
Description
Consider an arithmetic expression of the form a#b=c
. Check whether it is possible to replace #
with one of the four signs: +
, -
, *
or /
to obtain a correct expression.
Example
- For
a = 2
,b = 3
, andc = 5
, the output should bearithmeticExpression(a, b, c) = true
.
We can replace #
with a +
to obtain 2 + 3 = 5
, so the answer is true
.
- For
a = 8
,b = 2
, andc = 4
, the output should bearithmeticExpression(a, b, c) = true
.
We can replace #
with a /
to obtain 8 / 2 = 4
, so the answer is true
.
- For
a = 8
,b = 3
, andc = 2
, the output should bearithmeticExpression(a, b, c) = false
.8 + 3 = 11 ≠ 2
;8 - 3 = 5 ≠ 2
;8 * 3 = 24 ≠ 2
;8 / 3 = 2.(6) ≠ 2
.
So the answer is
false
.
Input/Output
-
[execution time limit] 4 seconds (js)
-
[input] integer a
Guaranteed constraints:
1 \leq a \leq 20
. -
[input] integer b
Guaranteed constraints:
1 \leq b \leq 20
. -
[input] integer c
Guaranteed constraints:
0 \leq c \leq 20
. -
[output] boolean
true
if the desired replacement is possible,false
otherwise.
[JavaScript (ES6)] Syntax Tips
1
2
3
4
5
6
// Prints help message to the console
// Returns a string
function helloWorld(name) {
console.log("This prints to the console when you Run Tests");
return "Hello, " + name;
}
Solution
1
2
3
4
5
6
7
8
9
function arithmeticExpression(a, b, c) {
var ops = [
(d, e) => d + e,
(d, e) => d - e,
(d, e) => d * e,
(d, e) => d / e
];
return ops.some(f => f(a, b) === c);
}