-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
109 lines (89 loc) · 3.25 KB
/
app.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
//Determines probability distribution for a Milestone of a given size.
//Displays best and worst project completion times by week and the odds of hitting
//said deadline
var numberOfSampleIterations = 10000;
var result = new Array();
var milestoneSize;
var velocities;
var outputLabel = document.getElementById("output");
var lengthOfSprint;
$('#runApp').click(run);
function run() {
resetState();
getUserData();
createProjection();
printResult();
}
function resetState() {
outputLabel.innerHTML = "";
result = new Array();
}
function getUserData() {
lengthOfSprint = Number(document.getElementById("lengthOfSprint").value);
milestoneSize = Number(document.getElementById("milestoneSize").value);
velocities = document.getElementById("velocities").value.replace(/\s/g, '').split(",");
//convert string to number
velocities = velocities.map(Number);
//adjust for length of sprint
velocities = velocities.map(adjustForLengthOfSprint);
//console.log("milestonesize: " + milestoneSize + " velocities: " + velocities);
}
function adjustForLengthOfSprint(input) {
var result = input / lengthOfSprint;
return result;
}
function createProjection() {
var sample = 0;
var weeksThisIteration = 0;
for (var i = 0; i < numberOfSampleIterations; i++) {
sample = 0;
weeksThisIteration = 0;
while (sample < milestoneSize) {
var randomNumber = getRandomInt(0,velocities.length - 1);
sample += velocities[randomNumber];
//console.log('sample size ' + velocities[randomNumber]);
weeksThisIteration++;
}
if(!result[weeksThisIteration]) {
result[weeksThisIteration] = 0;//initialize the element
}
result[weeksThisIteration]++;
}
}
function printResult() {
var cumulativeResult = 0;
var firstRun = true;
for (var key in result) {
var resultAsNumber = result[key];
//var output = "It took " + key + " Weeks " + resultAsNumber + " times ";
//console.log(output);
var percentCalculation = resultAsNumber / numberOfSampleIterations * 100;
cumulativeResult += percentCalculation;
var percentageResult = "Odds it will be done by " + getDate(key) + " is " + Math.floor(cumulativeResult) + "% (" + key + " weeks)";
if(firstRun) {
var output = "The earliest the milestone will be complete is " + key + " weeks from now or " + getDate(key) + ". The odds of finishing this early is " + Math.floor(cumulativeResult) + "%.<br />";
//console.log(output);
outputLabel.innerHTML += output + "<br />";
firstRun = false;
}else{
//console.log(percentageResult);
outputLabel.innerHTML += percentageResult + ". <br />";
}
}
}
function getDate(weeksFromNow) {
var daysFromNow = weeksFromNow * 7;
var now = new Date();
now.setDate(now.getDate() + daysFromNow);
var monthFix = now.getMonth() + 1;
//var result = monthFix + "/" + now.getDay() + "/" + now.getFullYear();
//var result = now.toJSON().slice(0,10);
var result = now.toString().split(' ').splice(1,3).join(' ')
return result;
}
function getRandomInt(min, max) {
//var randomResult = Math.random() * (max - min) + min;
var randomResult = Math.floor(Math.random() * (max - min + 1)) + min;//not normal distribution
//console.log(randomResult)
return randomResult;
}