Recap - Creating Reusable Code
Part of the Fundamentals section of Coddy's GO journey — lesson 59 of 109.
Challenge
EasyYou're building a simple calorie calculator for different activities.
Create a single function called calculateCalories that calculates how many calories are burned during an activity.
The function should:
- Take an activity type (string), duration in minutes (int), and intensity level (float64)
- Calculate and return the calories burned based on the activity type, duration, and intensity
Use these calorie rates per minute at normal intensity (1.0):
- Running: 10 calories per minute
- Swimming: 8 calories per minute
- Cycling: 7 calories per minute
- Any other activity: 5 calories per minute
The final calories should be calculated as: base calories × duration × intensity
Try it yourself
package main
import "fmt"
// calculateCalories determines how many calories are burned during an activity
// Parameters:
// - activity: the type of exercise ("running", "swimming", "cycling", or any other activity)
// - duration: how long the activity was performed (in minutes)
// - intensity: a multiplier representing how intense the workout was (1.0 is normal)
// Returns:
// - the total calories burned
func calculateCalories(activity string, duration int, intensity float64) float64 {
// TODO: Implement this function to calculate calories burned based on:
// 1. Determine the base calorie rate per minute based on activity type:
// - "running" burns 10 calories per minute at intensity 1.0
// - "swimming" burns 8 calories per minute at intensity 1.0
// - "cycling" burns 7 calories per minute at intensity 1.0
// - Any other activity burns 5 calories per minute at intensity 1.0
// 2. Calculate total calories as: base calories × duration × intensity
// 3. Return the calculated calories
return 0 // Replace with your implementation
}
func main() {
// Test the function with different activities
fmt.Println("Running for 30 minutes at intensity 1.2:", calculateCalories("running", 30, 1.2))
fmt.Println("Swimming for 45 minutes at intensity 1.0:", calculateCalories("swimming", 45, 1.0))
fmt.Println("Cycling for 60 minutes at intensity 1.5:", calculateCalories("cycling", 60, 1.5))
fmt.Println("Yoga for 60 minutes at intensity 0.8:", calculateCalories("yoga", 60, 0.8))
}
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison Operators - Part 1Comparison Operators - Part 2Logical AND OperatorLogical OR OperatorLogical NOT OperatorOperator Precedence BasicsRecap - Making Comparisons7Control Flow: Loops
What The `for` Loop ExplainedFor Loop - BasicFor Loop - Condition OnlyThe `break` KeywordThe `continue` KeywordNested LoopsRecap - Repeating Actions2Variables and Basic Data Types
What is a variableType Inference with `:=`Integers (int)Floating-Point NumbersBooleansStringsZero ValuesConstantsNaming ConventionsRecap - Variables and Types5Basic Input/Output
Formatted OutputFormat VerbsPrinting TypesGetting Basic User InputRecap - Input and Output8Functions
Understanding FunctionsDeclaring a FunctionCalling FunctionsFunction ParametersReturning a Single ValueReturning Multiple ValuesNamed Return ValuesFunction Scope BasicsRecap - Creating Reusable Code3Basic Operators
Arithmetic OperatorsDivision OperatorThe Modulo OperatorAssignment OperatorAugmented Assignment OperatorsIncrement and DecrementRecap - Calculations6Control Flow: Conditionals
The `if` StatementThe `else` KeywordThe `else if` KeywordVariable Shadowing in `if`Initializing VariablesThe `switch` StatementSwitch with ExpressionsSwitch without ExpressionThe `fallthrough` KeywordRecap - Making Decisions