-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
53 lines (41 loc) · 1.63 KB
/
script.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
// Get the widget element
const widget = document.getElementById('widget');
// Create the input fields and submit button
const xInput = document.createElement('input');
xInput.placeholder = 'Enter value for x...';
widget.appendChild(xInput);
const yInput = document.createElement('input');
yInput.placeholder = 'Enter value for y...';
widget.appendChild(yInput);
const proportionInput = document.createElement('input');
proportionInput.placeholder = 'Enter necessary proportion...';
widget.appendChild(proportionInput);
const submitButton = document.createElement('button');
submitButton.textContent = 'Submit';
widget.appendChild(submitButton);
// Create a container for displaying the result
const resultContainer = document.createElement('div');
widget.appendChild(resultContainer);
// Handle submit button click event
submitButton.addEventListener('click', () => {
const x = parseFloat(xInput.value);
const y = parseFloat(yInput.value);
const necessaryProportion = parseFloat(proportionInput.value);
if (isNaN(x) || isNaN(y) || isNaN(necessaryProportion) || necessaryProportion <= 0 || necessaryProportion >= 1) {
resultContainer.textContent = 'Please enter valid numbers between 0 and 1 for the necessary proportion.';
return;
}
const currentProportion = x / y;
if (currentProportion >= necessaryProportion) {
resultContainer.textContent = 'Already at or above the necessary proportion.';
return;
}
let z = 0;
while (((x + z) / (y + z)) < necessaryProportion) {
z++;
}
resultContainer.textContent = `The minimum value of z is: ${z}`;
// Clear the input fields
xInput.value = '';
yInput.value = '';
});