sf-83a / app.js
Hamson's picture
Add 3 files
428a378 verified
Raw History Blame Contribute Delete
1.01 kB
function calculatePrice() {
cost = (days * nights * persons);
price = cost * 0.95; // 5% markdown
}
function roundToTwo(num) {
return +(Math.round(num + "e+2") + "e-2");
}
function parseNumber(str) {
if (typeof str == "number") return str;
str = str.replace(/[^\d.-]/g, "");
return parseFloat(str);
}
function isFloat(value) {
let x = value;
x = x.replace(/\./, "");
return parseInt("1" + x);
}
function validate(value) {
const float = parseFloat(value);
return isFloat(float) && float >= 0 ? float : parseNumber(value);
}
function format(value) {
let x = parseNumber(value);
x = roundToTwo(x);
x = isFloat(x) ? x.toFixed(2) : x;
return x;
}
window.onload = function() {
calculatePrice();
};
document
.querySelector('button[type="submit"]')
.addEventListener("click", calculatePrice);
document
.querySelector('input[type="text"]')
.addEventListener("input", (event) => {
calculatePrice(event.target.value);
});