-
Notifications
You must be signed in to change notification settings - Fork 0
/
equality.js
30 lines (28 loc) · 874 Bytes
/
equality.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*You are given an equality of the form a operator b = c. Solve the problem by replacing the '?' by the correct operator out of +, -, *, or /.
Entrée
Line 1 : A string representing an equality where an operator symbol has been replaced with ?
Sortie
Line 1: Missing operator
Contraintes
a, b and c are all positive integers (represented as string)
Exemple
Entrée
2 ? 9 = 11
Sortie
+*/
function equality(string) {
const arthmetique = ["+", "-", "*", "/"];
const expression = string.split(" ");
for (let i = 0; i < arthmetique.length; i++) {
if (
eval(`${expression[0]} ${arthmetique[i]} ${expression[2]}`) ===
parseInt(expression[4])
) {
return arthmetique[i];
}
}
}
console.log(equality("2 ? 9 = 11")); // +
console.log(equality("7 ? 2 = 14")); // *
console.log(equality("9 ? 3 = 3")); // /
console.log(equality("8 ? 1 = 7")); // -