Write a function that calculates body mass index
Note: bmi = weight / height ^ 2
if bmi <= 18.5 return "Underweight"
if bmi <= 25.0 return "Normal"
if bmi <= 30.0 return "Overweight"
if bmi > 30 return "Obese"
This challenge comes from wichu on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!
Latest comments (20)
[gist.github.com/devparkk/9117b0780...]
Scala:
Might propose a change? Instead of underweight, overweight, obsese, etc, how about we replace the return values with "no idea, consult a health professional and have your body composition correctly measured"? It will be significantly more accurate! :D
I concur with this response.
Something like:
Let's say you need to run this a few billion times times, then a more complex look up table approach might be worth it.
(in JS because I like it, and I find it can be nice to mock up performance code in a slowish language first up.)
Have you tested if your code is faster than a simpler conditional version (as most other solutions are written)?
Fair point Craig, in this case it turns out to be a bunch slower due to the complexity in
indexFromBmi.I just figured it was worth posting as an alternative approach as there are problems where luts are gold. In this case apparently not.
I'd say that here it's best to just use
and be done with it.
LabVIEW: ;)
Awesome, haven't seen LabVIEW since I was at uni.
I couldn't resist posting a quick JavaScript solution... 😀
This seems to do the job
C++:
I did two functions, one that expects imperial units (Go America!) and one that expects metric units (the rest of the world!).
body.go
body_test.go
Haskell
Rust: