• Create BookmarkCreate Bookmark
• Create Note or TagCreate Note or Tag
• PrintPrint

### Exercise—Temperature Conversion JavaScript

1. Consider the following example. This example is closely based on the example you looked at previously:

```<html>
<title>A Simple Page</title>
<script language="JavaScript">
<!-- Cloaking device on!
function inputCels()
{
var cels = prompt("Enter a temperature in Degrees Celsius","Enter a temperature");
ansFah = doFahCalc(cels);
alert(cels + " Degrees Celsius is " + ansFah + " Degrees Fahrenheit");
}

function inputFah()
{
var fah = prompt("Enter a temperature in Degrees Fahrenheit","Enter a temperature");
ansCel = doCelCalc(fah);
alert(fah + " Degrees Fahrenheit is " + ansCel + " Degrees Celsius");
}

function doCelCalc(fah)
{
var ans = ((Number(fah) - 32) / 1.8);
return (ans);
}

function doFahCalc(cels)
{
var ans = ((1.8 * Number(cels)) + 32);
return (ans);
}
// Cloaking device off -->
</script>
<body>
<input type="button" value="Convert Celsius to Fahrenheit" onClick="inputCels();">
<br>
<br>
<input type="button" value="Convert Fahrenheit to Celsius" onClick="inputFah();">
</body>
</html>

```
2. Take a look at this code (type it in and run it or download the examples and try it out). Try to work out what each statement does and how they interact through parameters and the return statement.

4. Try modifying the example to use different mathematical equations (sales tax, gas mileage, and so on).

5. Most important of all, have fun! Take it easy; look at the previous examples and use them as a guide to help you to understand the interactions in this JavaScript example. It will take a little time, and it isn't necessarily easy, but it's definitely worth it! By going through this example for yourself, and perhaps even creating an example of your own, you are consolidating your JavaScript knowledge, which will help you on your way to JavaScript success!

EXERCISE

Try your hand at creating functions that pass data back and forth using parameters and the return statement. Follow the previous example if you find it difficult to begin, and simply change the variable names. After a while, you'll find that it comes easily to you.

PREVIEW

Not a subscriber?

Start A Free Trial

• Create BookmarkCreate Bookmark
• Create Note or TagCreate Note or Tag
• PrintPrint