Практическая часть
1. Разберите следующий скрипт
html>
<body>
<script LANGUAGE="JavaScript1.2">
<!-- Begin
var no = 3; // snow number
var speed = 1; // smaller number moves the snow faster
var snowflake = "your image.gif";
var ns4up = (document.layers) ? 1 : 0; // browser sniffer
var ie4up = (document.all) ? 1 : 0;
var dx, xp, yp; // coordinate and position variables
var am, stx, sty; // amplitude and step variables
var i, doc_width = 800, doc_height = 600;
if (ns4up) {
doc_width = self.innerWidth;
doc_height = self.innerHeight;
} else if (ie4up) {
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
}
dx = new Array();
xp = new Array();
yp = new Array();
am = new Array();
stx = new Array();
sty = new Array();
for (i = 0; i < no; ++ i) {
dx[i] = 0; // set coordinate variables
xp[i] = Math.random()*(doc_width-50); // set position variables
yp[i] = Math.random()*doc_height;
am[i] = Math.random()*20; // set amplitude variables
stx[i] = 0.02 + Math.random()/10; // set step variables
sty[i] = 0.7 + Math.random(); // set step variables
if (ns4up) { // set layers
if (i == 0) {
document.write("<layer name=\"dot"+ i +"\" left=\"15\" top=\"15\" visibility=\"show\"><img src=\"" + snowflake + "\" border=\"0\"></layer>");
} else {
document.write("<layer name=\"dot"+ i +"\" left=\"15\" top=\"15\" visibility=\"show\"><img src=\"" + snowflake + "\" border=\"0\"></layer>");
}
} else if (ie4up) {
if (i == 0) {
document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"; VISIBILITY: visible; TOP: 15px; LEFT: 15px;\"><img src=\"" + snowflake + "\" border=\"0\"></div>");
} else {
document.write("<div id=\"dot"+ i +"\" style=\"POSITION: absolute; Z-INDEX: "+ i +"; VISIBILITY: visible; TOP: 15px; LEFT: 15px;\"><img src=\"" + snowflake + "\" border=\"0\"></div>");
}
}
}
function snowNS() { // Netscape main animation function
for (i = 0; i < no; ++ i) { // iterate for every dot
yp[i] += sty[i];
if (yp[i] > doc_height-50) {
xp[i] = Math.random()*(doc_width-am[i]-30);
yp[i] = 0;
stx[i] = 0.02 + Math.random()/10;
sty[i] = 0.7 + Math.random();
doc_width = self.innerWidth;
doc_height = self.innerHeight;
}
dx[i] += stx[i];
document.layers["dot"+i].top = yp[i];
document.layers["dot"+i].left = xp[i] + am[i]*Math.sin(dx[i]);
}
setTimeout("snowNS()", speed);
}
function snowIE() { // IE main animation function
for (i = 0; i < no; ++ i) { // iterate for every dot
yp[i] += sty[i];
if (yp[i] > doc_height-50) {
xp[i] = Math.random()*(doc_width-am[i]-30);
yp[i] = 0;
stx[i] = 0.02 + Math.random()/10;
sty[i] = 0.7 + Math.random();
doc_width = document.body.clientWidth;
doc_height = document.body.clientHeight;
}
dx[i] += stx[i];
document.all["dot"+i].style.pixelTop = yp[i];
document.all["dot"+i].style.pixelLeft = xp[i] + am[i]*Math.sin(dx[i]);
}
setTimeout("snowIE()", speed);
}
if (ns4up) {
snowNS();
} else if (ie4up) {
snowIE();
}
// End -->
</script>
</body>
</html>
2. Разберите скрипт математического текста:
<HTML>
<!--
Author: Pavel Khramtsov
Date: February 23, 1996
URL: http://144.206.192.100/radleg/mo_input.htm
-->
<HEAD>
<TITLE>Проверка устного счета.</TITLE>
<SCRIPT LANGUGE="JavaScript">
<!--
var max_value = 0
var operand1 = 0
var operand2 = 0
var result = 0
var flag = 0
var sign = "+"
var input = ""
var v_date = new Date()
var number = v_date.getTime()
function init(factor) {
var today = new Date()
return (factor*today.getTime()/1000)%30000
}
//Инициализировать счетчик случайных чисел.
IX = init(2)
IY = init(3)
IZ = init(4)
//2, 3, and 4 below were arbitrarily chosen.
//They put some distance between the initial values of IX, IY, and IZ
//Датчик случайных чисел.
function random() {
IX = Math.floor(171 * IX % 30269)
IY = Math.floor(172 * IY % 30307)
IZ = Math.floor(170 * IZ % 30323)
return ( IX/30269.0 + IY/30307.0 + IZ/30323.0 ) % 1.0
}
//Выбрать сложение или вычитание.
function set_sign(x)
{
if( x == "+")
{
flag = 0
sign = "+"
}
if( x == "-")
{
flag = 1
sign = "-"
}
return true
}
// Определить первый операнд.
function f_operand()
{
operand1 = random()*max_value
return parseInt(operand1)
}
// Определить второй операнд.
function s_operand()
{
if(flag == 0)
{
operand2 = random() * (max_value-operand1)
}
else
{
operand2 = random() * operand1
}
return parseInt(operand2)
}
// Проверить введенные данные.
function input_sign(x)
{
if(x == 10)
{
if(flag == 0)
{
if( operand1+operand2 == parseInt(input))
{
window.document.test.r0.value = "Молодец!"
}
else
{
window.document.test.r0.value = "Думай!?"
window.document.test.input = ""; input = ""
}
}
if(flag == 1)
{
if( operand1-operand2 == parseInt(input) )
{
window.document.test.r0.value ="__-__-_р!"
}
else
{
window.document.test.r0.value = "Думай!?"
window.document.test.input = ""; input = ""
}
}
return true
}
input += x
window.document.test.result.value = input
return true
}
// Генерация варианта.
function main_calc()
{
operand1 = f_operand()
window.document.test.op1.value = operand1
operand2 = s_operand()
window.document.test.op2.value = operand2
window.document.test.s_sign.value = sign
input = ""
window.document.test.input = ""
window.document.test.r0.value = " ??? "
return true
}
// -->
</SCRIPT>
</HEAD>
<BODY>
<CENTER>
<H1>Математический тест</H1>
<HR>
<FORM NAME=test>
<TABLE BORDER=0>
<TR>
<TD><INPUT NAME=i10 TYPE=button VALUE="0-10"
onClick="max_value=10"></TD>
<TD><INPUT NAME=i20 TYPE=button VALUE="0-20"
onClick="max_value=20"></TD>
<TD><INPUT NAME=i100 TYPE=button VALUE="0-100"
onClick="max_value=100"></TD>
<TD><INPUT NAME=i+ TYPE=button VALUE=" + "
onClick="set_sign('+')"></TD>
<TD><INPUT NAME=i- TYPE=button VALUE=" - "
onClick="set_sign('-')"></TD>
</TR>
</TABLE>
<HR>
<TABLE BORDER=0>
<TR>
<TD><INPUT NAME=op1 SIZE=2 MAXLENGTH=2></TD>
<TD><INPUT NAME=s_sign SIZE=1 MAXLENGTH=1></TD>
<TD><INPUT NAME=op2 SIZE=2 MAXLENGTH=2></TD>
<TD>=</TD>
<TD><INPUT NAME=result SIZE=3 MAXLENGTH=3></TD>
<TD><INPUT NAME=award TYPE=button VALUE=" ? " onClick="main_calc()">
<TD><INPUT NAME=r0 VALUE=" ??? ">
</TR>
</TABLE>
<HR>
<TABLE BORDER=2>
<TR>
<TD><INPUT NAME=b1 TYPE=button VALUE=" 1 " onClick="input_sign('1')"></TD>
<TD><INPUT NAME=b2 TYPE=button VALUE=" 2 " onClick="input_sign('2')"></TD>
<TD><INPUT NAME=b3 TYPE=button VALUE=" 3 " onClick="input_sign('3')"></TD>
</TR>
<TR>
<TD><INPUT NAME=b4 TYPE=button VALUE=" 4 " onClick="input_sign('4')"></TD>
<TD><INPUT NAME=b5 TYPE=button VALUE=" 5 " onClick="input_sign('5')"></TD>
<TD><INPUT NAME=b6 TYPE=button VALUE=" 6 " onClick="input_sign('6')"></TD>
</TR>
<TR>
<TD><INPUT NAME=b7 TYPE=button VALUE=" 7 " onClick="input_sign('7')"></TD>
<TD><INPUT NAME=b8 TYPE=button VALUE=" 8 " onClick="input_sign('8')"></TD>
<TD><INPUT NAME=b9 TYPE=button VALUE=" 9 " onClick="input_sign('9')"></TD>
</TR>
<TR>
<TD><INPUT NAME=b0 TYPE=button VALUE=" 0 " onClick="input_sign('0')"></TD>
<TD COLSPAN=2><INPUT NAME=bs TYPE=button VALUE=" OK " onClick="input_sign('10')">
</TD></TR>
</TABLE>
</FORM>
</CENTER>
<HR>
</BODY>
</HTML>
Задание для самостоятельного выполнения:
Напишите скрипт вычисляющий синус, косинус, тангенс и котангенс заданного угла.
