-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormula.js
More file actions
75 lines (55 loc) · 2.31 KB
/
Formula.js
File metadata and controls
75 lines (55 loc) · 2.31 KB
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
/* ----------------------------------------------------------------------------------------
Now we will confect a reagent. There are eight materials to choose from, numbered
1,2,..., 8 respectively.
We know the rules of confect:
material1 and material2 cannot be selected at the same time
material3 and material4 cannot be selected at the same time
material5 and material6 must be selected at the same time
material7 or material8 must be selected(at least one, or both)
Task
You are given a integer array formula. Array contains only digits 1-8 that
represents material 1-8. Your task is to determine if the formula is valid.
Returns true if it's valid, false otherwise.
Example
For formula = [1,3,7], The output should be true.
For formula = [7,1,2,3], The output should be false.
For formula = [1,3,5,7], The output should be false.
For formula = [1,5,6,7,3], The output should be true.
For formula = [6,7,8], The output should be false.
For formula = [7,8], The output should be true.
Note
All inputs are valid. Array contains at least 1 digit. Each digit appears at most
once.
---------------------------------------------------------------------------------------- */
/*
Array.prototype.includes(): determines whether an array includes a certain value
among its entries, returning true or false as appropriate
*/
function isValid(formula) {
if (formula.includes(1) && formula.includes(2)) {
return false;
} else if (formula.includes(3) && formula.includes(4)) {
return false;
} else if (formula.includes(5) && !formula.includes(6)) {
return false;
} else if (!formula.includes(5) && formula.includes(6)) {
return false;
} else if (!formula.includes(7) && !formula.includes(8)) {
return false;
} else {
return true;
}
}
/*
Alternative solution:
Array.prototype.includes(): determines whether an array includes a certain value
among its entries, returning true or false as appropriate
*/
function isValid(formula) {
return (
!(formula.includes(1) && formula.includes(2)) &&
!(formula.includes(3) && formula.includes(4)) &&
formula.includes(5) === formula.includes(6) &&
(formula.includes(7) || formula.includes(8))
);
}