DEV Community

Discussion on: Daily Challenge #57 - BMI Calculator

Collapse
 
dak425 profile image
Donald Feury

I did two functions, one that expects imperial units (Go America!) and one that expects metric units (the rest of the world!).

body.go

package body

const (
    underweight string = "Underweight"
    normal      string = "Normal"
    overweight  string = "Overweight"
    obese       string = "Obese"
)

// Attributes represents some properties of a person's physique, such as weight and height
type Attributes struct {
    Height float64
    Weight float64
}

// BMI gives a description of your body health based on your attributes using body mass index
func BMI(attr Attributes) string {
    return status((attr.Weight / (attr.Height * attr.Height)) * 703)
}

// BMIMetric serves the same purpose as BMI but expects the attributes to be in the metric scale (kilograms, meters)
func BMIMetric(attr Attributes) string {
    return status(attr.Weight / (attr.Height * attr.Height))
}

func status(bmi float64) string {
    switch {
    case bmi <= 18.5:
        return underweight
    case bmi <= 25.0:
        return normal
    case bmi <= 30.0:
        return overweight
    default:
        return obese
    }
}

body_test.go

package body

import "testing"

type testCase struct {
    description string
    input       Attributes
    expected    string
}

func TestBMI(t *testing.T) {
    testCases := []testCase{
        {
            "underweight boi",
            Attributes{71, 116.0},
            underweight,
        },
        {
            "normal boi",
            Attributes{71, 147.0},
            normal,
        },
        {
            "overweight boi",
            Attributes{67, 190},
            overweight,
        },
        {
            "obese boi",
            Attributes{65, 210},
            obese,
        },
    }

    for _, test := range testCases {
        if result := BMI(test.input); result != test.expected {
            t.Fatalf("FAIL: %s - BMI(%+v): %s - expected %s", test.description, test.input, result, test.expected)
        }
        t.Logf("PASS: %s", test.description)
    }
}

func TestBMIMetric(t *testing.T) {
    testCases := []testCase{
        {
            "underweight boi",
            Attributes{1.8034, 52.61671},
            underweight,
        },
        {
            "normal boi",
            Attributes{1.8034, 66.67808},
            normal,
        },
        {
            "overweight boi",
            Attributes{1.7018, 86.18255},
            overweight,
        },
        {
            "obese boi",
            Attributes{1.651, 95.2544},
            obese,
        },
    }

    for _, test := range testCases {
        if result := BMIMetric(test.input); result != test.expected {
            t.Fatalf("FAIL: %s - BMIMetric(%+v): %s - expected %s", test.description, test.input, result, test.expected)
        }
        t.Logf("PASS: %s", test.description)
    }
}