Heat Index Function for Java Script

Heat Index Function for Java Script
0.0 0

#1

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.


#2

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.


Weather app Heat Index
#3

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 HIADJ;
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));
  HIADJ = HI - x;
  console.log("Subtracted x=" + x + ". Newl heat index after adjustment is HIADJ= " + HIADJ);
} else if (hum > 85 && temp > 80 && temp < 87) {
  var y = ((hum - 85)/10)*((87-temp)/5);
  HIADJ = HI + y;
  console.log("Added y=" + y + ". Newl heat index after adjustment is HIADJ=" + HIADJ);
} else {
HIADJ = HI;
console.log("Heat index with no adjustment is " + HIADJ);
    }

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


#4

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.


#5

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;
if (HIADJ > 95) {
  weatherTip = "";
} else if (HIADJ > 90 && HIADJ < 94) {
    weatherTip = "";
} else if (HIADJ > 85 && HIADJ < 89) {
    weatherTip = "";
} else if (HIADJ > 80 && HIADJ < 84) {
     weatherTip = "";
  } else if (HIADJ > 75 && HIADJ < 79) {
    weatherTip = "";
} else if (HIADJ > 70 && HIADJ < 74) {
    weatherTip = "";
} else if (HIADJ > 65 && HIADJ < 69) {
    weatherTip = "";
} else if (HIADJ > 60 && HIADJ < 64) {
    weatherTip = "";
} else if (HIADJ > 55 && HIADJ < 59) {
    weatherTip = "";
} else if (HIADJ > 50 && HIADJ < 54) {
    weatherTip = "";
} else if (HIADJ > 45 && HIADJ < 49) {
    weatherTip = "";
} else if (HIADJ > 40 && HIADJ < 44) {
    weatherTip = "";
} else if (HIADJ > 35 && HIADJ < 39) {
    weatherTip = "";
} else if (HIADJ > 30 && HIADJ < 34) {
    weatherTip = "";
} else if (HIADJ > 25 && HIADJ < 29) {
    weatherTip = "";
  } else if (HIADJ > 20 && HIADJ < 24) {
    weatherTip = "";
} else if (HIADJ > 15 && HIADJ < 19) {
    weatherTip = "";
} else if (HIADJ > 10 && HIADJ < 14) {
    weatherTip = "";
} else if (HIADJ > 5 && HIADJ < 9) {
    weatherTip = "";
} else if (HIADJ > 0 && HIADJ < 4) {
    weatherTip = "";
} else {
    weatherTip = "";
}