# Performing Calculations - Exercise

Performing Calculations - Exercise

# Performing Calculations

Duration: 20 to 30 minutes.

In this exercise, you will modify the calculator processing page to calculate the time it takes to finish a marathon at certain speeds.

1. Open Basics/Exercises/ProcessCalculator-2.cfm in your editor.
2. Beneath the "Calculate Speeds" heading, set a variable called Marathon with the value of 26.2.
3. Set two more variables, yourmarathontime and friendmarathontime and assign them values as calculated from the form input and the Marathon variable.
4. For now we will assume the units are in MPH.
5. Return text to the browser that reads:
```							              John, at 10
mph, you would run a marathon in 2.62 hours.
At 5
mph, Marylin would run a marathon in 5.24 hours```
. *Replace the highlighted words with variable values.
6. At the bottom of the page, add a paragraph that reads:
`Form processed at 11:30AM on October 23, 2006.`
*Replace the bold words with the current time and date from the server.

## Code Sample:

Basics/Exercises/ProcessCalculator-2.cfm
```<html>
<title>Calculate Speeds</title>
<body>
<h1>Calculate Speeds</h1>
<!---
Set a variable called Marathon with
the value of 26.2
--->

<!---
Calculate the values for
yourmarathontime and friendmarathontime
--->

<!---
Return text to the browser that reads
"John, at 10 mph, you would run a marathon in 2.62 hours.
At 5 mph, Marylin would run a marathon in 5.24 hours."
--->

<!---
Form processed at 11:30AM on February 23, 2007.
--->

</body>
</html>
```

Return text to the browser that reads:

John, at 10 mph, you would run a marathon in 2 hours and 37 minutes. At 5 mph, Marylin would run a marathon in 5 hours and 14 minutes.

Hint: you may find it useful to use the Int() function to reformat the marathon times output to show a whole number (e.g, no decimal point). You may also find it useful to use modulus (MOD).

## Solution:

Basics/Solutions/ProcessCalculator-2.cfm
```<html>
<title>Calculate Speeds</title>
<body>
<h1>Calculate Speeds</h1>
<cfset Marathon = 26.2>
<cfset yourmarathontime = Marathon/FORM.yourspeed>
<cfset friendmarathontime = Marathon/FORM.friendspeed>

<cfoutput>
#FORM.yourname#, at #FORM.yourspeed# #FORM.units#,
you would run a marathon in #yourmarathontime#
hours.<br>

At #FORM.friendspeed# #FORM.units#, #FORM.friendname# would
run a marathon in #friendmarathontime# hours.<br>

<p>Form processed at #TimeFormat(Now(),"h:mmtt")# on
#DateFormat(Now(),"mmmm d, yyyy")#.</p>
</cfoutput>

</body>
</html>
```

## Challenge Solution:

Basics/Solutions/ProcessCalculator-2-challenge.cfm
```<html>
<title>Calculate Speeds</title>
<body>
<h1>Calculate Speeds</h1>
<cfset Marathon = 26.2>
<cfset yourmarathontime = Marathon/FORM.yourspeed>
<cfset friendmarathontime = Marathon/FORM.friendspeed>

<cfset yourhours = Int(yourmarathontime)>
<cfset yourminutes = (yourmarathontime * 60) MOD 60>
<cfset friendhours = Int(friendmarathontime)>
<cfset friendminutes = (friendmarathontime * 60) MOD 60>

<cfoutput>
#FORM.yourname#, at #FORM.yourspeed# #FORM.units#,
you would run a marathon in #yourhours#
hours and #yourminutes# minutes.<br>

At #FORM.friendspeed# #FORM.units#, #FORM.friendname# would
run a marathon in #friendhours#hours
and #friendminutes# minutes .<br>

<p>Form processed at #TimeFormat(Now(),"h:mmtt")# on
#DateFormat(Now(),"mmmm d, yyyy")#.</p>
</cfoutput>

</body>
</html>
```