DEV Community

Cover image for Public Solving: Calculating the wind chill
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Public Solving: Calculating the wind chill

Santa's sled is pretty modern. Hey, we even upgraded it to have an autopilot.
But now, the elves want to surprise Santa by adding a wind chill gauge.

The wind chill is the "feel" temperature, you know when it's like 30 degrees, but it feels like 35?

You can find the complete puzzle here.

To do this, we can use an already provided mathematical calculation which can be found here.

The wind chill can be calculated for English and Metric values.

Thinking about the solution

The main thing we have to achieve today is to actually make the formula in JavaScript.
This should be a pretty straightforward process.

The formula for English units looks like this:

35.74 + 0.6215T – 35.75 (V^0.16) + 0.4275T (V^0.16)
Enter fullscreen mode Exit fullscreen mode

Where T = Temperature in degrees Fahrenheit and V = wind speed in miles per hour.

In JavaScript this should look similar to this:

35.74 + 0.6215 * temperature - 35.75 * windSpeed ** 0.16 + 0.4275 * temperature * windSpeed ** 0.16
Enter fullscreen mode Exit fullscreen mode

Did you note the (V^0.16) exponent? We can use Math.pow or the shortcut ** for that.

Then we can simply wrap this in a Math.round to get the rounded number.

return Math.round(
  35.74 +
    0.6215 * temperature -
    35.75 * windSpeed ** 0.16 +
    0.4275 * temperature * windSpeed ** 0.16
);
Enter fullscreen mode Exit fullscreen mode

However, we also need a way to calculate the metric version.

I decided to just catch and return the English units beforehand.

And if that didn't hit, surely it must be the metric version.

if (units === 'US') {
    return Math.round(
      35.74 +
        0.6215 * temperature -
        35.75 * windSpeed ** 0.16 +
        0.4275 * temperature * windSpeed ** 0.16
    );
}

return Math.round(
    13.12 +
      0.6215 * temperature -
      11.37 * windSpeed ** 0.16 +
      0.3965 * temperature * windSpeed ** 0.16
);
Enter fullscreen mode Exit fullscreen mode

And that's it! We solved the issue.

Let's try it out and see if our test turns green.

Wind chill test in JavaScript turning green

Would love to hear what you would do differently to solve this problem.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (4)

Collapse
 
lexlohr profile image
Alex Lohr

Really nothing for me to add here. I would probably use a ternary statement for the sake of brevity, but other than that, my code would be the same.

Maybe you expected me to come up with some clever way to change the variables based on units, but I'm actually striving to build code that is as stupid and obvious as possible, simply because it's more reliable. The fewer logic your code has to implement, the less it can fail. That's why I prefer plain objects to functions.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Yeah actually thought about using ternary here, but it would take away from the ease of reading.

An object lookup would probably be the best, but you would kind of want a "default" return case.

While talking about this in general that would be something that would be really cool to have in JS.

const values = {
'us': myFunc(),
default: myDefaultFunc()
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lexlohr profile image
Alex Lohr

Also, a default overwrite would a) have to use Symbol.default or not be exposed not to clash with a string value of the same name and b) totally be possible using a Proxy:

const setObjectDefault = (obj, default) =>
  new Proxy(
    obj,
    {
      get: (target, prop) => prop in target
        ? target[prop]
        : default
    }
  )
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
lexlohr profile image
Alex Lohr

I don't see why

if (unit === 'US') {
  return ...
}
return ...
Enter fullscreen mode Exit fullscreen mode

should be easier to read than

return unit === 'US'
  ? ...
  : ...
Enter fullscreen mode Exit fullscreen mode

but I guess it comes down to reading habits. I prefer concise notation if not too convoluted.