����� 2
�� 2: ����� ����� ��������
��� ������ ���� ���� ���� ����� �� ����� ��������, ���� ������ ����� �- cookies ������? ����, ����� �- cookies �� �������. �� ��� ����� �� ����� ����� �����, ����� ���� ����� ������ cookies ������. ���, �� ��� ����� �� ����� �������� ���� ��� �����, ���� ����� �� ������ �- cookies. �� ��� ����� ������ �������, ���� ����� �� ���� ������ ������� �������� �� JavaScript.
��� �����, ��� ���� ����� ���� ������, ����� ������ ��� ���� �� ��, ��� ����� ����� ����, ���� ����� ������ 2 - �� 4, �� ��� ����� ���� ����� ������ ����� �������:
var normal_monkey = "I am a monkey!<br>";
document.writeln("Normal monkey " + normal_monkey);
var bold_monkey = normal_monkey.bold();
document.writeln("Bold monkey " + bold_monkey);
var bold_monkey = normal_monkey.bold();
var bold_monkey = "<b>" + normal_monkey + "</b>";
������ �������, ���� ����� ����� ����. ��� �������� ����� ����� ������ �� ������� ��� �����, ���� ��� ������. ��� ���� ������� �� �������: indexOf, charAt, substring �- split, ������ ������ �� ���� �� ��������, ������ ��� �� �������. ��� ���� �- indexOf �����.
indexOf
������ indexOf ����� �� ������ �� ����� ����� ���� �������, ������� ���� ���-������ (����� ������) ������ ������� ������. �� ������� ������ ���� ����� �� �� ������� �������, ������ indexOf ����� �� ���� "1-". ��� ���� ������:
var the_word = "monkey";
����� �� ����� "monkey" �������.
var location_of_m = the_word.indexOf("m");
����� ���� 'm' ���� "0" � ����� ���� ������ ������� ��� ������, ���� ������.
var location_of_o = the_word.indexOf("o");
var location_of_key = the_word.indexOf("key");
����� ���-������ "key" ���� "3", ����� ��� ������� "key" ������ ��� 'k', ������� ������� ������� ��� 3.
var location_of_y = the_word.indexOf("y");
var cheeky = the_word.indexOf("q");
���� ���� �� ���� "-1" ����� ����� 'q' �� ������ ������� "monkey".
����� ��� ���� ������ ������ �- indexOf ���:
var the_email = prompt("What's your email address?", "");
var the_at_is_at = the_email.indexOf("@");
if (the_at_is_at == -1)
{
alert("You loser, email addresses must have @ signs in them.");
}
���� ���� ���, ����� ������� ������ �� ����� �- email ���, ��� ����� �� ������ ����� ��� �����. �� ������ ���� ����� �� ��� '@' ��� ��� ���� ���� �����. ������ indexOf ����� �� ��� '@' ������� ������.
charAt
������ charAt ������ �� ��� ������� ����� ����� ����� �������. ��� ��� ������:
var the_word = "monkey";
var the_first_letter = the_word.charAt(0);
var the_second_letter = the_word.charAt(1);
var the_last_letter = the_word.charAt(the_word.length-1);
���� ���� ����, the_first_letter = 'm', the_second_letter = 'o' �- the_last_letter = 'y'. ��� ��, �� ������� ���� ��� ����� �� ������� ������� ����� ������� length �� �������. ����� �� the_word ��� "monkey", ��� �� the_word.length ��� '6'. �� ���� ���� ������ ������� ��� ������ 0 ���� ��� ������ ���� ����� length-1 (������ ��: 5). �� ����� ������ ������� ������ �� ��� ����� �- "length-1".
���� ������ �- substring �- split ��� ����� �� charAt ��� �� indexOf.
����� ������� 1
������ ��� ����� ��� ������ ��� ��� ������ ������� ��� ��� ������� ���� (a,e,i,o,u) �� �����. ������� ����� ��� ����� ���� ��� ����� �- indexOf ��- charAt. �����, ����� ���� ��� ����� ���� ���� ��� ��� ����� ����� �- indexOf, ��� ����: ���� ���� ���� ����� �����.
��� ����� ���� �����. �������, �� ���������, ��� ���� ���� ���, �� ���� �� ������ ������� ������ ���. �� �� ��� �� �� ������ ������� ��� ��� ����� �����, ���� ���� ������ ���-������-���.
���� ������ ���
��� ��� «--
|