Ans: No form is needed; a variable can be used for the ID.
<input type="text" name="txtInput" size="20" />Ans:
<input type="text" id="txtInput" size="20" />
function milesToCm( ) { var m, c; m = parseFloat(document.frmConvert.txtMiles.value); c = 5280 * 12 * 2.54 * m; document.frmConvert.txtCm.value = c; }Ans:
function milesToCm( ) { var m, c; m = parseFloat(document.getElementById("txtMiles").value); c = 5280 * 12 * 2.54 * m; document.getElementById("txtCm").value = c; }
Ans: For our uses, div tags allow absolute positioning of text or images. This is useful if you want to place images or text on top of each other.
Ans:Abstraction means ignoring irrelavant details and concentrating on essential ones. To use a builtin function, you don't need to know how the code works, you only need to know what it expects as input and what it does or produces as output.
Ans: (1) Abstraction, (2) Code Reuse or Avoid Code Duplication, (3) Event handlers.
<html> <head> <script type="text/javascript"> function f(x) { document.writeln(2 * x + 1); } </script> </head> <body> <script type="text/javascript"> f(5); </script> </body> </html>Ans: 11
Note: Math.floor together with Math.random will be useful for generating random integers.
Ans: Math.sqrt, Math.random, Math.pow are the ones that we have discussed. Math.round is also available. The trig functions Math.sin, Math.cos, and Math.tan are also available if you remember your trigonometry from highschool.
Function display roll[ ] ( var roll1 roll2:Ans: Here is the fixed version:roll1 = 1 + Math.floor(6 * math.random( ): frmDisplayDice.txt Roll1 = roll1; roll1 = 1 + Math.floor(6 * math.random( ): frmDisplayDice.txt Roll2 = roll1;
function displayRoll( ) { var roll1, roll2;roll1 = 1 + Math.floor(6 * Math.random( )); document.frmDisplayDice.txtRoll1 = roll1; roll2 = 1 + Math.floor(6 * Math.random( )); document.frmDisplayDice.txtRoll2 = roll2; }
var a = "dog", b = "cat", c = "mouse"; d = c; c = b; b = a; a = d; document.writeln(a + " " + b + " " + c);Ans:
a | b | c | d |
---|---|---|---|
"dog" | "cat" | "mouse" | |
"mouse" | "dog" | "cat" | "mouse" |
Output: mouse dog cat
<img src="blank.jpg" name="imgAnimal" />
document.images.animal.src = "dog.jpg";
roll = 1 + Math.floor(6 * Math.random( ));