Skip to content

Instantly share code, notes, and snippets.

@dillonchr
Created December 1, 2021 05:32
Show Gist options
  • Save dillonchr/0d2f9cd14985e45757dd1bdbdcfd3b5c to your computer and use it in GitHub Desktop.
Save dillonchr/0d2f9cd14985e45757dd1bdbdcfd3b5c to your computer and use it in GitHub Desktop.

Revisions

  1. dillonchr created this gist Dec 1, 2021.
    27 changes: 27 additions & 0 deletions calculate-weight.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    (function(weight) {
    // how much water weighs per gallon
    const WATER_WEIGHT_IN_POUNDS_PER_GALLON = 8.34;
    // how much of the body is made of water
    const WATER_PERCENTAGE_IN_BODY = 0.6;

    function getGallonsOfWater(weight) {
    return weight * WATER_PERCENTAGE_IN_BODY / WATER_WEIGHT_IN_POUNDS_PER_GALLON;
    }

    function getPoundsFromGallons(gallons) {
    // since water is 60%, we need to figure out the other 40%
    // so we just find one third of 60% (20%) and then double that
    // to find 40% and add the two together to uncover what the original
    // weight was
    const oneThird = gallons / 3;
    return (gallons + (oneThird * 2)) * WATER_WEIGHT_IN_POUNDS_PER_GALLON;
    }

    console.clear();
    console.log({
    weight,
    gallons: getGallonsOfWater(weight),
    andBack: getPoundsFromGallons(getGallonsOfWater(weight))
    });
    }
    )(200);