JavaScript Editor JavaScript Validator     JavaScript Editor 



Team LiB
Previous Section Next Section

Exercise Questions

Suggested solutions to these questions can be found in Appendix A.

Question 1

Q: 

 

Write a JavaScript program to convert degrees centigrade into degrees Fahrenheit, and write the result to the page in a descriptive sentence. The JavaScript equation for Fahrenheit to centigrade is

degFahren = 9 / 5 * degCent + 32

A: 
<html>
<body>
    
<script language=JavaScript>
    
var degCent = prompt("Enter the degrees in centigrade",0);
var degFahren = 9 / 5 * degCent + 32;
    
document.write(degCent + " degrees centigrade is " + degFahren +
   " degrees Fahrenheit");
    
</script>
    
</body>
</html>

Save this as ch02_q1.htm.

We get the degrees centigrade the user wants to convert by using the prompt() function, and store it inside the degCent variable.

We then do our calculation, which uses the data stored in degCent and converts it to Fahrenheit. The result is assigned to the degFahren variable.

Finally, we write the results to the web page, building it up in a sentence using the concatenation operator +. Note how JavaScript knows in the calculation that degCent is to be treated as a number, but in the document.write() it knows that it should be treated as text for concatenation. So how does it know? Simple, it looks at the context. In the calculation, degCent is surrounded by numbers and numerical only operators, such as * and /. In the document.write(), degCent is surrounded by strings, hence JavaScript assumes the + means concatenate.

Question 2

Q: 

 

The following code uses the prompt() function to get two numbers from the user. It then adds those two numbers together and writes the result to the page.

<html>
<body>
<script language="JavaScript" type="text/javascript">
    
var firstNumber = prompt("Enter the first number","");
var secondNumber = prompt("Enter the second number","");
var theTotal = firstNumber + secondNumber;
document.write(firstNumber + " added to " + secondNumber + " equals " +
   theTotal);
    
</script>
</body>
</html>

However, if you try the code out, you'll spot that it doesn't work. Why not?

Change the code so that it does work.

A: The data that the prompt() actually obtains is a string. So both firstNumber and secondNumber contain text that happens to be number characters. When we use the + symbol to add the two variables together, JavaScript assumes that since it's string data, we must want to concatenate the two together and not sum them.

To make it explicit to JavaScript that we want to add the numbers together, we need to convert the data to numbers using the parseFloat() function.

<html>
<body>
<script language=JavaScript>
    
var firstNumber = parseFloat(prompt("Enter the first number",""));
var secondNumber = parseFloat(prompt("Enter the second number",""));
var theTotal = firstNumber + secondNumber;
document.write(firstNumber + " added to " + secondNumber + " equals " +
   theTotal);
    
</script>
</body>
</html>

Save this as ch02_q2.htm.

Now the data returned by the prompt() function is converted to a floating-point number before being stored in the firstNumber and secondNumber variables. Then, when we do the addition that is stored in theTotal, JavaScript makes the correct assumption that, because both the variables are numbers, we must mean to add them up and not concatenate them.

The general rule is that where we have expressions with only numerical data, the + operator means "do addition." If there is any string data, the + will mean concatenate.


Team LiB
Previous Section Next Section


JavaScript Editor JavaScript Validator     JavaScript Editor


©

R7