/
logic2.js
66 lines (55 loc) · 1.67 KB
/
logic2.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
let fieldWidth;
let pondRadius;
let pondCenterX;
let pondCenterY;
let shotsInsidePond = 0;
let totalShotsFired = 0;
let distanceX;
let distanceY;
let c;
let aSquared;
let bSquared;
let cSquared;
function functionChain() {
function isCoordinateWithinCircle(trueOrFalse) {
if (trueOrFalse === true) {
shotIsInsidePond = true;
shotsInsidePond = (shotsInsidePond + 1);
}
};
function distanceFromCenterOfPond(randomX, randomY) {
distanceX = (randomX - pondCenterX);
distanceY = (randomY - pondCenterY);
function pythagoreanTheorem() {
aSquared = Math.pow(distanceX, 2);
bSquared = Math.pow(distanceY, 2);
cSquared = (aSquared + bSquared);
c = Math.sqrt(cSquared);
}
pythagoreanTheorem();
isCoordinateWithinCircle((c < pondRadius));
};
function plotRandomCoordinate() {
randomX = Math.floor(Math.random() * (fieldWidth + 1));
randomY = Math.floor(Math.random() * (fieldWidth + 1));
distanceFromCenterOfPond(randomX, randomY);
};
plotRandomCoordinate();
};
let i = 0;
function runProgram(widthOfField, numberOfShots) {
fieldWidth = widthOfField;
pondRadius = (fieldWidth / 2);
pondCenterX = pondRadius;
pondCenterY = pondRadius;
while (i < numberOfShots) {
functionChain();
i += 1;
}
let approximationOfPi = ((shotsInsidePond / numberOfShots) * 4);
console.log("Number of shots fired: " + numberOfShots + "\n" +
"Shots inside pond: " + shotsInsidePond + "\n" +
"Approximation of Pi: " + approximationOfPi
);
}
runProgram(200, 4000000000);