JavaScript Algorithm: Hydration Count

We are going to write a function that will return how many liters of water a young man named Archie should drink to stay hydrated

Erica N
Erica N
Mar 31 · 2 min read

We are going to write a function called liters that will take a number (time) as an argument.

We have a young man named Archie. Archie loves to go jogging but he also loves water and staying hydrated. He loves water so much he sleeps in his swimming pool. Nevermind his skin swelling up, he plans on turning into a fish anyways.

When he goes out jogging, he drinks about 0.5 liters of water per hour. Given the time in hours return the number of liters of water Archie will need to drink to stay hydrated. Return the number rounded down to the smallest value. Here is an example:

let time = 6.7;

If Archie goes jogging for 6.7 hours and he drinks 0.5 liters of water per hour, we multiply 6.7 * 0.5 and get 3.35 liters. We round that number down and the function will return 3.

Since all we are doing is multiplying time by 0.5, our equation is:

time * 0.5

We want our number to be a whole number so in the case we encounter a decimal number, we’ll round down using Math.floor().

return Math.floor(time * 0.5);

Multiplying time by 0.5 is also equivalent to dividing time by 2.

return Math.floor(time / 2);

Here is the rest of our function:

function liters(time) {
return Math.floor(time * 0.5);

If you found this algorithm helpful, check out my other JavaScript algorithm solutions:

A note from JavaScript In Plain English: We are always interested in helping to promote quality content. If you have an article that you would like to submit to JavaScript In Plain English, send us an email at with your Medium username and we will get you added as a writer.

JavaScript In Plain English

New articles every day.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store