I am having an issue with the following question: For every hour a cyclist rides his/her bike, they will need to drink 0.5 liters of water. So if a cyclist rides for a total of 1 hour, he/she will need to drink 0.5 liters of water. If they cycle for 3 hours, it will be a total of 1.5 liters and etc.
My first thought was to use an if/else statement like this:
export function liters(time: number): number {
if (time === 1){
return 0.5 liters
} else if (time === 1.5) {
return 1 liters
}
}
const answer = liters(1)
console.log(answer)
But then I realized I can't keep writing if/else statements forever, especially if someone logs 100 hours of cycling. I am sure there is a better way of approaching this problem and making it dynamic. Does anyone have any recommendations? I am currently learning TypeScript, so please don't tell me the answer, I would prefer some leads or be pointed in the right direction, that way I can solve by myself. Thanks!
Aucun commentaire:
Enregistrer un commentaire