TutorialEdge

Challenge 07 - Minimums, Maximums and Errors

Welcome, everyone to the discussion thread for the 7th Go challenge posted to the site :tada:

Feel free to show us how you solved the challenge by posting your solutions below!

Solution:

package main

import (
	"errors"
	"fmt"
)

type Flight struct {
	Origin      string
	Destination string
	Price       int
}

func GetMinMax(flights []Flight) (int, int, error) {

	if len(flights) == 0 {
		return 0, 0, errors.New("No Flights Passed in")
	}

	min, max := flights[0].Price, flights[0].Price

	for _, flight := range flights {
		if flight.Price < min {
			min = flight.Price
		}

		if flight.Price > max {
			max = flight.Price
		}
	}

	return min, max, nil
}

func main() {
	fmt.Println("Getting the Minimum and Maximum Flight Prices")
}