-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscreteRandom.js
51 lines (39 loc) · 1.37 KB
/
DiscreteRandom.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
document.getElementById('randomVariableForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent form submission
// Get input values
var outcomesInput = document.getElementById('outcomes');
var probabilitiesInput = document.getElementById('probabilities');
var valueInput = document.getElementById('value');
var outcomes = outcomesInput.value.split(',').map(function(item) {
return item.trim();
});
var probabilities = probabilitiesInput.value.split(',').map(function(item) {
return parseFloat(item.trim());
});
var value = parseFloat(valueInput.value);
// Validate input lengths
if (outcomes.length !== probabilities.length) {
alert('Number of outcomes must match number of probabilities.');
return;
}
// Calculate probability
var probability = 0;
for (var i = 0; i < outcomes.length; i++) {
if (outcomes[i] === value.toString()) {
probability = probabilities[i];
break;
}
}
// Display result
var resultDiv = document.getElementById('result');
resultDiv.innerHTML = 'Probability: ' + probability.toFixed(2);
});
// FORMULA
function showPopup() {
var popup = document.getElementById('popup');
popup.style.display = 'block';
}
function closePopup() {
var popup = document.getElementById('popup');
popup.style.display = 'none';
}