Menu
Coddy logo textTech

Recap - Simple Math

Part of the Fundamentals section of Coddy's R journey — lesson 15 of 78.

challenge icon

Challenge

Easy

Calculate various statistics about a road trip using all the arithmetic operators you've learned.

You are provided with the following variables:

total_distance <- 847
fuel_used <- 42
hours_driven <- 13
rest_stops <- 4

Perform the following calculations:

  1. Create a variable called fuel_efficiency that stores the distance traveled per unit of fuel (total distance divided by fuel used)
  2. Create a variable called avg_speed that stores the average speed (total distance divided by hours driven)
  3. Create a variable called hours_per_stop that stores how many complete hours passed between each rest stop (use integer division of hours driven by rest stops)
  4. Create a variable called extra_minutes that stores the remainder when dividing hours driven by rest stops, then multiply it by 60 to convert to minutes
  5. Create a variable called distance_squared that stores the total distance raised to the power of 2

Use the print() function to display fuel_efficiency, avg_speed, hours_per_stop, extra_minutes, and distance_squared in that exact order.

Try it yourself

# Given variables for the road trip
total_distance <- 847
fuel_used <- 42
hours_driven <- 13
rest_stops <- 4

# TODO: Write your code below
# 1. Calculate fuel_efficiency (total_distance / fuel_used)

# 2. Calculate avg_speed (total_distance / hours_driven)

# 3. Calculate hours_per_stop (use integer division %/%)

# 4. Calculate extra_minutes (use modulo %% then multiply by 60)

# 5. Calculate distance_squared (use exponentiation ^)

# Print the results in order
print(fuel_efficiency)
print(avg_speed)
print(hours_per_stop)
print(extra_minutes)
print(distance_squared)

All lessons in Fundamentals