JSFiddle - React, Tailwind, and code Playground
JavaScript
function Balloon() {
function density(altitude, theGas) { /* KG/CU M */
var gas = { /* GAS DEFINATIONS - wolframalpha.com */
"hydrogen": .00100794,
"helium": .004002602,
"nitrogen": .0140067,
"methane": .0160425,
"ammonia": .0170305,
"neon": .0201791,
"dry air": .0289644
}
var alt = { /* CONSTANTS - http://en.wikipedia.org/wiki/Density_of_air#Altitude */
"p0": 101325,
// Sea level standard atmospheric pressure (Pa)
"T0": 288.15,
// Sea level standard temperature (K)
"g": 9.80665,
// Earth-surface gravitational acceleration (m/s^2)
"L": 0.0065,
// Temperature lapse rate (K/m)
"R": 8.31447 // Universal gas constant (mol * K)
}
var temperature = alt["T0"] - alt["L"] * altitude;
var pressure = alt["p0"] * (1 - ((alt["L"] * altitude) / alt["T0"])) ^ ((alt["g"] * gas[theGas]) / (alt["R"] * alt["L"]));
var density = (pressure * gas[theGas]) / (alt["R"] * temperature);
return density;
}
function lift(altitude, gas) { /* KG/CU M */
return density(altitude, "dry air") - density(altitude, gas);
}
this.requiredGas = function(altitude, gas, ratio, weight) {
return ((weight / 1000) * ratio) / lift(altitude, gas);
}
}
balloon = new Balloon();
var required = balloon.requiredGas(10, "helium", 1.5, 4530);
alert(required);