I'm trying to write code that would solve the following problem:
Nathan loves cycling.
Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling.
You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value.
For example:
time = 3 ----> litres = 1
time = 6.7---> litres = 3
time = 11.8--> litres = 5
Here's the code I have:
function litres(time) {
var litrez = Math.round(time * 0.5);
if (litrez === 1) {
return "1 litre";
}
else {
return litrez + " litres";
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="in"/>
<button onclick="$('#out').append(litres(Number($('#in').val())))"> Calculate</button>
<div id="out"></div>
Thanks in advance! :)