Zizix94 - 1 year ago 72

HTML Question

A user inputs an amount of water they need, I need to calculate which bottles to use, either 10l bottles or 20l bottles. So if they ask for 67l, the script pops up with 3x20l and 1x10l

I have tried the following 2 solutions

`var resinAm = rsize*1.1*2.5`

var resTin = '';

if(resinAm<10){

resTin = 1;

} else {

resTin = resinAm/20;

resTin = Math.ceil(resTin);

}

and this

`var resAm = resinAm;`

var resAm10 = (resAm/10)+0.001;

var resAm20 = (resAm/20)*2;

if(resAm10>resAm20){

resTin10 = 1;

resTin20 = Math.floor(resAm/20);

} else {

resTin20 = Math.ceil(resAm/20);

}

I have also tried using modulo, but it always comes out with an extra 20l bottle, 70.5l came out as 5x20l and 1x10l

How do I fix this

Answer Source

Check the code at: https://jsfiddle.net/a4k2cyoy/2/

```
var resAm = 70.5;
var resAm20 = parseInt(resAm / 20);
var resAm10 = 0;
if(resAm % 20 > 10) {
resAm20++;
}
if(resAm % 20 <= 10 && resAm > 0) {
resAm10++;
}
alert(resAm + " will go into " + resAm20 + " x 20 bottles and " + resAm10 + " x 10 bottles");
```