difficult way to calculate edu in swift

One way to calculate the education (edu) of a person could be to use a formula that takes into account their highest level of education completed and the number of years they spent in education. Here's a possible implementation in Swift:

main.swift
func calculateEdu(level: String, years: Int) -> Int {
    var edu = 0
    
    switch level {
    case "High School":
        edu = years
    case "Bachelor's Degree":
        edu = years + 12
    case "Master's Degree":
        edu = years + 16
    case "PhD":
        edu = years + 20
    default:
        break
    }
    
    return edu
}
342 chars
19 lines

This function takes two parameters: the level of education completed (as a String) and the number of years spent in education (as an Int). It then calculates the edu value based on the level of education completed, using the following conversion rates:

  • High School: 1 year of education = 1 edu point
  • Bachelor's Degree: 1 year of education = 1 edu point, plus an additional 12 edu points
  • Master's Degree: 1 year of education = 1 edu point, plus an additional 16 edu points
  • PhD: 1 year of education = 1 edu point, plus an additional 20 edu points

For example, if someone completed a Bachelor's Degree and spent 4 years in education, their edu value would be 16 + 4 + 12 = 32.

gistlibby LogSnag