diff --git a/app.js b/app.js new file mode 100644 index 0000000..dcabc53 --- /dev/null +++ b/app.js @@ -0,0 +1,72 @@ +const tipAmount = document.querySelector(".tip-amount"); +const totalAmount = document.querySelector(".total-amount"); +const inputBill = document.querySelector(".bill"); +const selectBtn = document.querySelectorAll("button"); +const peopleNum = document.querySelector(".number"); +const resetBtn = document.querySelector(".reset-btn"); +const ifZero = document.querySelector(".if-zero"); +const customTip = document.querySelector(".custom"); +let selectTip; + +inputBill.addEventListener("click", () => { + inputBill.style.outlineColor = "hsl(185, 41%, 84%)"; +}); + +peopleNum.addEventListener("click", () => { + peopleNum.style.outlineColor = "hsl(185, 41%, 84%)"; +}); + +function resetBtnColor() { + for (let i = 0; i < Object.keys(selectBtn).length; i++) { + selectBtn[i].classList.remove("clicked"); + } +} + +function inputCustom() { + if (customTip.value) { + selectTip = parseInt(customTip.value) / 100; + } +} + +function clickedBtn(event) { + resetBtnColor(); + event.target.classList.add("clicked"); + selectTip = event.target.innerText; + selectTip = parseInt(selectTip) / 100; +} + +function decideZero() { + if (peopleNum.value === "0") { + ifZero.innerText = "Can't be zero"; + ifZero.classList.add("not-zero"); + peopleNum.style.outlineColor = "red"; + } else { + ifZero.innerText = ""; + ifZero.classList.remove("not-zero"); + peopleNum.style.outlineColor = "hsl(185, 41%, 84%)"; + } +} + +function showResult() { + decideZero(); + tipCalc = (Number(inputBill.value) * selectTip) / peopleNum.value; + totalCalc = Number(inputBill.value) / peopleNum.value + tipCalc; + tipAmount.innerText = `$${tipCalc.toFixed(2)}`; + totalAmount.innerText = `$${totalCalc.toFixed(2)}`; +} + +function reset() { + inputBill.value = null; + peopleNum.value = null; + tipAmount.innerText = "$0.00"; + totalAmount.innerText = "$0.00"; +} + +for (let i = 0; i < selectBtn.length; i++) { + selectBtn[i].addEventListener("click", (e) => clickedBtn(e)); +} + +customTip.addEventListener("keyup", inputCustom); +peopleNum.addEventListener("keyup", showResult); +window.addEventListener("load", reset); +resetBtn.addEventListener("click", reset); diff --git a/index.html b/index.html index 0a2739a..9058f67 100644 --- a/index.html +++ b/index.html @@ -1,44 +1,58 @@ -
- - - - - -