-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisotopes.js
50 lines (44 loc) · 1.24 KB
/
isotopes.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
/**
* @param {number} protonsStart The initial number of protons
* @param {number} neutronsStart The initial number of neutrons
* @param {number} protonsTarget The desired number of protons
* @param {number} neutronsTarget The desired number of neutrons
* @return {string[]}
*/
function solve(protonsStart, neutronsStart, protonsTarget, neutronsTarget) {
let recipe = [];
const proton = () => {
currentProton += 1;
let tempString = "PROTON";
recipe.push(tempString);
};
const neutron = () => {
currentNeutron += 1;
let tempString = "NEUTRON";
recipe.push(tempString);
};
const alpha = () => {
currentProton -= 2;
currentNeutron -= 2;
let tempString = "ALPHA";
recipe.push(tempString);
};
let currentProton = protonsStart;
let currentNeutron = neutronsStart;
while (
!(currentProton == protonsTarget && currentNeutron == neutronsTarget)
) {
if (currentProton > protonsTarget) {
alpha();
} else if (currentNeutron > neutronsTarget) {
alpha();
} else if (currentProton != protonsTarget) {
proton();
} else if (currentNeutron != neutronsTarget) {
neutron();
}
}
return recipe;
}
const result = solve(2, 2, 3, 3);
console.log("Result: ", result);