-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·38 lines (35 loc) · 1.41 KB
/
app.js
File metadata and controls
executable file
·38 lines (35 loc) · 1.41 KB
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
document.getElementById("calculate-btn").addEventListener("click", function() {
// Get user input
const baseInput = document.getElementById("base-input").value;
const targetInput = document.getElementById("target-input").value;
const amountInput = document.getElementById("amount-input").value;
const outputArea = document.getElementById("result");
// Run calculateNicToAdd and display return value.
outputArea.innerHTML = calculateNicToAdd(baseInput, targetInput, amountInput);
});
// CALCULATE
function calculateNicToAdd(base, target, amount) {
// Check for invalid inputs
if (!base || !target || !amount) {
// values are not truthy
return `<p class="error">Error, one or more inputs are invalid. Please input value</p>`;
}
if (base <= 0 || target <= 0 || amount <= 0) {
// values cannot be 0
return `<p class="error">All inputs must be grater than zero.</p>`;
}
// calculate result
let result = (target / base) * amount;
// convert to grams
weight = result * 1.038;
// round to 2 decimal places
result = result.toFixed(2);
weight = weight.toFixed(2);
// return result
return `<p>
<strong>${result}mL</strong> amount of nicotine is required to mix your ${amount}mL bottle at the nicotine strength of ${target}mg.
<br/><br/>
<em>${result}mL of nicotine liquid weighs approximately <strong>${weight} grams</strong>.</em>
</p>
`;
}