I am trying to do my first JS app and unfortunately JS returns NaN value instead of desired amount. I bet solution is quite simple, although I kept trying to resolve problem on my own for past 2 hours and nothing works...
function calculateTip () {
var cost = document.querySelector('.amount').value;
var service = document.querySelector('.service').value;
var people = document.querySelector('.numOfPeo').value;
var tip = (cost * service) / people;
if (cost === "" || service === 0) {
alert("Please enter values");
}
if (people === "" || people <= 1) {
people = 1;
}
document.getElementById('totalTip').style.display = "block";
document.getElementById('tip').innerHTML = tip;
}
btn.addEventListener('click', calculateTip);
<div class="container">
<h1>Tip Calculator</h1>
<div class="descr-1"><p>How Much was your bill?</p></div>
<div class="input-1 amount"><input type="text" placeholder="Bill Amount in £"></div>
<div class=descr-2><p>How was your service?</p></div>
<div class="select"> <select class="service">
<option disabled selected value="0">Choose an Option</option>
<option value="30">Extraordinary! - 30%</option>
<option value="20">Amazing! - 20%</option>
<option value="15">Good - 15%</option>
<option value="10">Was Ok - 10%</option>
<option value="5">Awful! - 5%</option>
</select></div>
<div class="descr-3">How many people sharing a bill?</div>
<div class="people numOfPeo"> <input type="text" placeholder="Number of People"></div>
<button id="btn">Calculate!</button>
<div class="total">
<div id="totalTip">
<sup>$</sup><span id="tip">0.00</span>
<small id="each">each</small>
</div>
</div>