Golang basics series #4

2 min read
golang-basics-series-4

You will learn 2 concepts by completing this exercise. Conditionals If and Comparison #problemsolving

We learned some basic and most common concepts of Go so far, now let's try to solve the problem down below

the question is from exercism go track

In this exercise, you are going to write some code to help you prepare to buy a vehicle.

You have three tasks, one to determine if you need a license, one to help you choose between two vehicles, and one to estimate the acceptable price for a used vehicle.

Determine if you will need a driver's license

Some vehicle kinds require a driver's license to operate them. Assume only the kinds "car" and "truck" require a license, everything else can be operated without a license.

Implement the NeedsLicense(kind) function that takes the kind of vehicle and returns a boolean indicating whether you need a license for that kind of vehicle.

Choose between two potential vehicles to buy

You evaluate your options of available vehicles. You manage to narrow it down to two options but you need help making the final decision. For that, implement the function ChooseVehicle(option1, option2) that takes two vehicles as arguments and returns a decision that includes the option that comes first in dictionary order.

Calculate an estimation for the price of a used vehicle

Now that you made a decision, you want to make sure you get a fair price at the dealership. Since you are interested in buying a used vehicle, the price depends on how old the vehicle is. For a rough estimate, assume if the vehicle is less than 3 years old, it costs 80% of the original price it had when it was brand new. If it is at least 10 years old, it costs 50%. If the vehicle is at least 3 years old but less than 10 years, it costs 70% of the original price.

CalculateResellPrice(originalPrice, age) function that applies this logic using if, else if, and else (there are other ways if you want to practice). It takes the original price and the age of the vehicle as arguments and returns the estimated price in the dealership.

the task is straight forward and after doing this exercise we are expected to be more familiar with Go Conditions and down the road, I will try to put comments on the code so it will be easier for you to know what is going on

let go

// define our working package package purchase // Package fmt implements formatted I/O with functions analogous to C's printf and scanf. The format 'verbs' are derived from C's but are simpler. import "fmt" // NeedsLicense determines whether a license is needed to drive a type of vehicle. Only "car" and "truck" require a license. func NeedsLicense(kind string) bool { if kind == "car" || kind == "truck" {         return true     } return false } // ChooseVehicle recommends a vehicle for selection. It always recommends the vehicle that comes first in dictionary order. func ChooseVehicle(option1, option2 string) string { if option1 < option2{         return fmt.Sprintf("%s is clearly the better choice.",option1)     }     return fmt.Sprintf("%s is clearly the better choice.",option2) } // CalculateResellPrice calculates how much a vehicle can resell for at a certain age. func CalculateResellPrice(originalPrice, age float64) float64 { if age < 3 {          price :=originalPrice * 0.8         return price     }     if age < 10 && age > 3 {          price :=( originalPrice *  0.7)         return price     }           price := (originalPrice * 0.5)      return price      }
Comments (0)
No comments yet