-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
expressions-matter.js
114 lines (96 loc) · 2.97 KB
/
expressions-matter.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
function expressionMatter(a, b, c) {
const answer1 = a * (b + c);
const answer2 = a * b * c;
const answer3 = a + b * c;
const answer4 = (a + b) * c;
const answer5 = a * b + c;
const answer6 = a + b + c;
return Math.max(answer1, answer2, answer3, answer4, answer5, answer6);
}
function expressionMatter(a, b, c) {
const add = (x, y) => x + y;
const multiply = (x, y) => x * y;
const answer1 = add(a, add(b, c));
const answer2 = add(a, multiply(b, c));
const answer3 = multiply(a, add(b, c));
const answer4 = multiply(a, multiply(b, c));
const answer5 = multiply(add(a, b), c);
const answer6 = multiply(multiply(a, b), c);
const answer7 = add(add(a, b), c);
const answer8 = add(multiply(a, b), c);
return Math.max(answer1, answer2, answer3, answer4, answer5, answer6, answer7, answer8);
}
function expressionMatter(a, b, c) {
const add = (x, y) => x + y;
const multiply = (x, y) => x * y;
const answers = [];
const fns = [add, multiply];
fns.forEach(fn1 => {
fns.forEach(fn2 => {
answers.push(fn1(a, fn2(b, c)));
answers.push(fn1(fn2(a, b), c));
});
});
return Math.max(...answers);
}
function expressionMatter(a, b, c) {
const add = (x, y) => x + y;
const multiply = (x, y) => x * y;
let highest = 0;
const fns = [add, multiply];
fns.forEach(fn1 => {
fns.forEach(fn2 => {
const answer1 = fn1(a, fn2(b, c));
if (answer1 > highest) {
highest = answer1;
}
const answer2 = fn1(fn2(a, b), c);
if (answer2 > highest) {
highest = answer2;
}
});
});
return highest;
}
function expressionMatter(a, b, c) {
const add = (x, y) => x + y;
const multiply = (x, y) => x * y;
let highest = 0;
const fns = [add, multiply];
fns.forEach(fn1 => {
fns.forEach(fn2 => {
const answer1 = fn1(a, fn2(b, c));
const answer2 = fn1(fn2(a, b), c);
highest = Math.max(highest, answer1, answer2);
});
});
return highest;
}
describe("Small values", function() {
console.log(expressionMatter(2, 1, 2), 6);
console.log(expressionMatter(2, 1, 1), 4);
console.log(expressionMatter(1, 1, 1), 3);
console.log(expressionMatter(1, 2, 3), 9);
console.log(expressionMatter(1, 3, 1), 5);
console.log(expressionMatter(2, 2, 2), 8);
});
describe("Medium values", function() {
console.log(expressionMatter(5, 1, 3), 20);
console.log(expressionMatter(3, 5, 7), 105);
console.log(expressionMatter(5, 6, 1), 35);
console.log(expressionMatter(1, 6, 1), 8);
console.log(expressionMatter(2, 6, 1), 14);
console.log(expressionMatter(6, 7, 1), 48);
});
describe("Mixed values", function() {
console.log(expressionMatter(2, 10, 3), 60);
console.log(expressionMatter(1, 8, 3), 27);
console.log(expressionMatter(9, 7, 2), 126);
console.log(expressionMatter(1, 1, 10), 20);
console.log(expressionMatter(9, 1, 1), 18);
console.log(expressionMatter(10, 5, 6), 300);
console.log(expressionMatter(1, 10, 1), 12);
});
function describe(description, cb) {
cb();
}