HTML and CSS Reference
In-Depth Information
Exercises
1. Copy the following file and execute it in your browser. What's wrong with it
and why? Can you fix it?
<html>
<head><title>link</title>
<script type="text/javascript">
function addem(){
var n = 2;
var y = 3;
document.write( n + y , "<br />");
}
</script>
</head>
<body bgcolor="red">
<a href="JavaScript:addem()">Click here</a>
<h2>Hello</h2>
</body>
</html>
2. Write a function that will calculate and return the amount that should be paid
as a tip for a restaurant bill. The tip will be 20 percent of the total bill.
3. Create a function called changeColor() that will be called when the user presses
one of two buttons. The first button will contain the text “ Press here for a yellow
background ”. The second button will contain the text “ Press here for a light green
background ”. The function will take one parameter, a color. Its function is to
change the background color of the current document; for example,
bgColor=“yellow”.
4. Write a function called isLeapYear() that will return true if this is a leap year
and false if it isn't.
5. Write an anonymous function that will calculate and return the distance you
have traveled using the formula, rate * time . The function definition will be
assigned to a variable. The function will be called using the variable.
6. The following example is written in the C language. It is a very common recur-
sive function call factorial . Can you explain how this function works and can
you write it in JavaScript?
unsigned int factorial(unsigned int n){
if (n <= 1) {
return 1;
} else {
Search WWH ::




Custom Search