# Heat Index Function for Java Script

Does anyone know how to write a function for the heat index in a weather app? This gentleman implemented it it in C++:https://stackoverflow.com/questions/17328034/c-program-to-calculate-heat-index-not-providing-expected-results

I’m a total newbie to coding and given up trying to understand the guys code above. There is a heat index chart available here:http://www.petprofessionalguild.com/Resources/Documents/Heat%20Index%20Chart.pdf and making a function with many “cases” using the humidity and temperature is starting to look like my solution. I feel like there’s a better way to do this, just need a little assistance from y’all. Thanks in advance.

Look at the top of the function void calcheatIndex he is using a formula to convert temperature and humidity into the heat index. Then the answer is in Fahrenheit and he sees if the user wants it in celsius or not.

It’s the first formula here.
https://en.wikipedia.org/wiki/Heat_index#Formula

Using the formula you could have one function that takes input and outputs the heat index, instead of converting a chart into a bunch of switch cases.

Below I’ll try explaining some of the c++ specific parts of the function.

C++ is static typed, which means you have to tell the computer what type a variable is when declaring it, and with functions tell it what type it will return.

void FunctionName() means it will return nothing.
const means the variable cannot be changed after declaration, Note: JavaScript also has this.
double means the variable is a number.

cout<< is the same as console.log(),
and cin>> gets input through the console.

After going through the link you provided and following other resources, I came up with this: https://repl.it/Jwru/3. Any feedback is appreciated.

``````var temp = 85;
var hum = 10;
var HI = -42.379 + 2.04901523*temp + 10.14333127*hum - .22475541*temp*hum - .00683783*temp*temp - .05481717*hum*hum + .00122874*temp*temp*hum + .00085282*temp*hum*hum - .00000199*temp*temp*hum*hum;

console.log("Prelimenary Heat Index is " + HI);

if (hum < 13 && temp > 80 && temp < 112) {
var x = ((13 - hum)/4) * (Math.sqrt(17-Math.abs(temp-95)/17));
} else if (hum > 85 && temp > 80 && temp < 87) {
var y = ((hum - 85)/10)*((87-temp)/5);
} else {
}
``````

Used this site heavily for reference: http://www.wpc.ncep.noaa.gov/html/heatindex_equation.shtml

Changed the numbers some and it lines up with the heat index well. Just round the result and you’re good to go for the app.

Rounding should be fairly easy. Do you have any suggestions how to code up weather advice based on the temperature ranges? So far, I’ve used a very long if/else statement. I attempted a for-loop but just couldn’t do it. https://repl.it/Jwru/3

``````    var weatherTip;
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";
weatherTip = "";