��� ����: ����� ����� / ����� 4 / �� 7
 


������ �� ��� �- JavaScript
 


���� ������ ��:
1: JavaScript �����
2: ����� ���
3: ����� ������ �
4: ����� ������ �
5: ������ ��������
6: ����� ������� ����
7: ���� ���� ���������
8: ����� �������
9: ��������� ���� �
10: ��������� ���� �
11: ���� �� ���� 4
|
|
|
|
|
|
|
|
|
|
|
|

����� 4

�� 7: ���� ����� ���������� ����� ������ ���.

����� ��� ����� ���� �� �� ��� ��� ��� ���� ���� ����������. ���� ������� ����� ����� ������. �� ��� ������� ���� ���� �� ���� ����, �� ����. ��� ���� ��� ���� �"� ����� �� ����� ����� (start):

Name:
Status:
Name:
Status:

��� ������� �����, ��� �� ���� �� �� ������� ���� ���� ���� ���������. ���� ����� ������ ��� ��� ����� ��� ��� ����.

��� ����, �� ���� ��� ����� ����� ���������. �� ���� ���� ��� ���� �� ������� ���� �����. ��� ���� ���� �� �� ������� �����:

  • ������.
  • ����.
  • ���.
  • ���.
  • �� (pet_name).
  • ���� ���� (form_number) � ���� �-HTML ��� ���� ����� ��� ��..

������ ��� ������� �������� �����:

  • ()play - ����� ����.
  • ()feed � ����� ���.
  • ()medicate � ���� �� ������� ���.
  • ()makeOlder � ���� �� ���� ����� ������� ����.
  • ()display � ���� ���������� ���� ���� ����� ���� ���� �����.

���, �� ���� ��"�, ��� ����� ������� ���� ����. ���� ������ ���� ������ ����� ��� ������ �����. ��� ���� ����� (��� ��� ����� ��� ����� call ��� ���):

function Pet(the_pet_name, the_form_number)
{
  this.age = 0;
  this.hunger = Math.random() * 5; // random number between 0 and 4.99
  this.health = Math.random() * 5 + 1 ; // random number between 1 and 3.99
  this.happiness = Math.random() * 5;
  this.pet_name = the_pet_name;
  this.form_number = the_form_number;
  window.document.forms[the_form_number].pet_name.value = the_pet_name;
}

����� ���� ��� �������: �� ���� ����� ������� �� ����� ��� ���� ����� ����� ����� �����. �� ��� ����� ��� ���� ����, ����� �� ���� ���:

var pet1 = new Pet("barney",0);
var pet2 = new Pet("betty",1);

������ ���� �� ��� ���� ���� ��������� ���� ����. ��� ����� ������ ()Math.random ������ ������ ������� ��� ��� ����. ����� �- ()Math.random ���� ������ ������, �� ���� ������ ���� ��� �����, ���� ��� �� ����� ��� ��� ��� ���� ���� ��� ������ ����, ������ ���� ����� ����. �� ���� ����� ����� �"� ������ �� ��� ���� ��� �����, ���� ���� ����� ����� ����. ��� ������ �������� ������ �� ����� �� ���� ���� ����� ��� �� ���� ����� ����� ���� ���� ����� �������. ���� �� �� ����� ������ ����� ���� ������� �- [window.document.forms[0 ��� ����� ���� ����� ��� � [window.document.forms[1 ��� ����.

�� ��� �� �������; ���� ����� �� �������. ����� ���� �� �����, ��� �� ���� ���� ������� �� ��� ������ �����:

function feed()
{
  var meal_quality = Math.random() * 2;
  this.hunger = this.hunger - meal_quality;
  if (this.hunger < 0)
  {
    this.hunger = 0;
  }
  this.display();
}

���� ������ �� ������� �� ��� ������ �� ���� �����. �� ���� �� ������ ()pet1.feed, ����� �� �����. ��� �����, ��� ������ ���� ����� ��� ��� ������. ��� ��� ������ �� ����� ��� ����� �� ����� ����� �������. ��� ������� �� ���� ��� ���� ����, ��� ����� ������ ������ �����. ����� ����� ����� ��:

function display()
{
  var the_string = "";

  if (this.health < min_health)
  {
    the_string = this.pet_name + " IS DEAD!";
  } else {
    the_string += "Happiness " + parseInt(this.happiness);
    the_string += ". Health: " + parseInt(this.health);
    the_string += ". Hunger: " + parseInt(this.hunger);
    the_string += ". Age: " + parseInt(this.age);
    the_string += ".";
  }

  window.document.forms[this.form_number].pet_status.value = the_string;
}

����� �� ���� ������ ������ ���� ����� ����� ������. ������ form_number ������ ���������� �� ������ ����� ����� �������. ���� ����� �� ���� ����, ������ ��� form_number (���� �����). ���� ����� pet1 ����� �� ���� ����� '���', ����� pet2 ����� �� ���� ����� '���'. �"� ������ �- form_number, ��� ������ �� ������ �� pet1 ���� ���� �� window.forms[0].pet_status.value ������� �� pet2 ���� ���� �� window.document.forms[1].pet_status.value. ����� ������� �� ������ ��� ����� ����� ���� ������ �� ������ ������ �- this.form_number.

���� ������� ���� ������ �� ��� ������� �-parseInt. ����� ���� �� ��� ������� �� ������� ��������, ������ ���� ����� ��� ������ ����� ����� ���: 2.738993720. ��� ������ �������, �� �� ����� �� ������. ��� parseInt ���� �� �� ������ ���� ������ ��������; ������, (parseInt(2.738 ����� ����� �� ���� 2.

���� ������� ������ ������ �� ��� ()makeOlder. ����� �� ����� ����� ����� ����� ������ ����, ���� ���� ����� ����. ������ ����� ������ �� �� ���� �"� ����� �-setTimeout, ��� ���� �� ��� ���. �� ����� �� �� ����� �- ()makeOlder, ����� ���� ��� �� ����� ����� �� �� ���� �����.

function makeOlder()
{

  var max_hunger = 5;
  var good_happiness = 5;

  if (this.health > min_health)
  {
      
    this.age +=1;
    this.happiness -= Math.random() * 2;
    this.hunger += Math.random() * 2;
    if (this.hunger > max_hunger)
    {
      this.health -= Math.random() * 2;
      this.happiness -= Math.random() * 2;
    }

  }

  this.display();
}

��� ���� ����, ������ ������ ��� ��� ���� ���� �����, ������ �� �����, ������� �� ���� �������� ���� ������. �� ����� �����, ��� ��� ���� ����� ���, ����� "�� ���� ����� ��� ����, ���� ���� ����� ����� ����� �����".

��� ��� ��� �� ������� ����, ����� ���� ����� �� ��� ������ �� ������� ���� ����� ����������:

function Pet(the_pet_name, the_form_number)
{
  this.age = 0;
  this.hunger = Math.random() * 5; // random number between 0 and 4.99
  this.health = Math.random() * 3 +1 ; // random number between 1 and 3.99
  this.happiness = Math.random() * 5;
  this.pet_name = the_pet_name;
  this.form_number = the_form_number;

  this.feed = feed;
  this.play = play;
  this.medicate = medicate;
  this.display = display;
  this.makeOlder = makeOlder;

  window.document.forms[the_form_number].pet_name.value = the_pet_name;
  this.display();
}

���� ������, �� ��� ������ �� ������, ��� ���� ��� ����� �� ����� ()makeOlder ��� ���� ���� �� ���� �����. ������� �� ������ ���� ��� ���� �� ������ ���� (start).

function moveTime()
{
  pet1.makeOlder();
  pet2.makeOlder();
  the_time_out = setTimeout("moveTime();", 3000);
}

��� ���� ����, ����� ()moveTime, ������ �� ()makeOlder �� �� ���� ���� ������� �� ���� ��� ���� ����� ��� ��. ���� ���� ������, �������� ������ ���. ��� ���� ����� ��- setTimeout �������. ����� �� ������ �� ������� ���� � - (clearTimeout(the_time_out.

����� �� ����� ���� �� �� ������ �� ����. �� ���� �� �� �� ����, ��� ���� ���� ��� ���� ����� ������ ����� JavaScript. �� ����� ����� ���� ���� ����� ����� ������ �����: ����� ������� JavaScript, �� ��� ����� ��������� ���� �����.

���� ������ ���

��� ��� «--

 
       
       
   
 
 

���� ����
����� �����
����� �����
����� ������
����� ���
��� ����