This commit is contained in:
Ilya Kantor 2015-08-25 11:27:46 +03:00
parent db39a8be86
commit 04d0e3f425
11 changed files with 51 additions and 66 deletions

View file

@ -1,18 +0,0 @@
<!DOCTYPE html>
<html>
<body>
<script>
var value = prompt('Каково "официальное" название JavaScript?', '');
if (value == 'EcmaScript') {
alert('Верно!');
} else {
alert('Не знаете? "EcmaScript"!');
}
</script>
</body>
</html>

View file

@ -1,14 +1,10 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script>
var value = prompt('Введите число', 0);
var value = prompt('Type a number', 0);
if (value > 0) {
alert(1);

View file

@ -2,7 +2,7 @@
```js
//+ run
var value = prompt('Введите число', 0);
var value = prompt('Type a number', 0);
if (value > 0) {
alert( 1 );

View file

@ -1,12 +1,15 @@
# Получить знак числа
# Show the sign
[importance 2]
Используя конструкцию `if..else`, напишите код, который получает значение `prompt`, а затем выводит `alert`:
Using `if..else`, write the code which gets a number via `prompt` and then shows in `alert`:
<ul>
<li>`1`, если значение больше нуля,</li>
<li>`-1`, если значение меньше нуля,</li>
<li>`0`, если значение равно нулю.</li>
<li>`1`, if the value is greater than zero,</li>
<li>`-1`, if less than zero,</li>
<li>`0`, if equals zero.</li>
</ul>
In this task we assume that the input is always a number.
[demo src="if_sign"]

View file

@ -2,32 +2,31 @@
```js
//+ run demo
var userName = prompt('Кто пришёл?', '');
var userName = prompt('Who's there?', '');
if (userName == 'Админ') {
if (userName == 'Admin') {
var pass = prompt('Пароль?', '');
var pass = prompt('Password?', '');
if (pass == 'Чёрный Властелин') {
alert( 'Добро пожаловать!' );
} else if (pass == null) { // (*)
alert( 'Вход отменён' );
if (pass == 'TheMaster') {
alert( 'Welcome!' );
} else if (pass == null || pass == '') { // (*)
alert( 'Canceled.' );
} else {
alert( 'Пароль неверен' );
alert( 'Wrong password' );
}
} else if (userName == null) { // (**)
alert( 'Вход отменён' );
} else if (userName == null || userName == '') { // (**)
alert( 'Canceled' );
} else {
alert( 'Я вас не знаю' );
alert( "I don't know you" );
}
```
Обратите внимание на проверку `if` в строках `(*)` и `(**)`. Везде, кроме Safari, нажатие "Отмена" возвращает `null`, а вот Safari возвращает при отмене пустую строку, поэтому в браузере Safari можно было бы добавить дополнительную проверку на неё.
Please note the `if` check in lines `(*)` and `(**)`. Every browser except Safari returns `null` when the input is canceled, and Safari returns an empty string. So we must treat them same for compatibility.
Впрочем, такое поведение Safari является некорректным, надеемся, что скоро его исправят.
Кроме того, обратите внимание на дополнительные вертикальные отступы внутри `if`. Они не обязательны, но полезны для лучшей читаемости кода.
Also note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.

View file

@ -1,17 +1,22 @@
# Проверка логина
# Check the login
[importance 3]
Напишите код, который будет спрашивать логин (`prompt`).
Write the code which asks for a login with `prompt`.
Если посетитель вводит "Админ", то спрашивать пароль, если нажал отмена (escape) -- выводить "Вход отменён", если вводит что-то другое -- "Я вас не знаю".
If the visitor enters `"Admin"`, then `prompt` for a password, if the input is an empty line or [key Esc] -- show "Canceled.", if it's another string -- then show "I don't know you".
Пароль проверять так. Если введён пароль "Чёрный Властелин", то выводить "Добро пожаловать!", иначе -- "Пароль неверен", при отмене -- "Вход отменён".
The password is checked as follows:
<ul>
<li>If it equals "TheMaster", then show "Welcome!",</li>
<li>Another string -- show "Wrong password",</li>
<li>For an empty string or cancelled input, show "Canceled."</li>
</ul>
Блок-схема:
The schema:
<img src="ifelse_task.png">
Для решения используйте вложенные блоки `if`. Обращайте внимание на стиль и читаемость кода.
Please use nested `if` blocks. Mind the overall readability of the code.
[demo /]

View file

@ -1,6 +1,6 @@
```js
result = (a + b < 4) ? 'Мало' : 'Много';
result = (a + b < 4) ? 'Below' : 'Over';
```

View file

@ -1,14 +1,14 @@
# Перепишите 'if' в '?'
# Rewrite 'if' into '?'
[importance 5]
Перепишите `if` с использованием оператора `'?'`:
Rewrite this `if` using the ternary operator `'?'`:
```js
if (a + b < 4) {
result = 'Мало';
result = 'Below';
} else {
result = 'Много';
result = 'Over';
}
```

View file

@ -1,9 +1,9 @@
```js
var message = (login == 'Вася') ? 'Привет' :
(login == 'Директор') ? 'Здравствуйте' :
(login == '') ? 'Нет логина' :
var message = (login == 'Employee') ? 'Hello' :
(login == 'Director') ? 'Greetings' :
(login == '') ? 'No login' :
'';
```

View file

@ -1,20 +1,20 @@
# Перепишите 'if..else' в '?'
# Rewrite 'if..else' into '?'
[importance 5]
Перепишите `if..else` с использованием нескольких операторов `'?'`.
Rewrite `if..else` using multiple ternary operators `'?'`.
Для читаемости -- оформляйте код в несколько строк.
For readability, please let the code span several lines.
```js
var message;
if (login == 'Вася') {
message = 'Привет';
} else if (login == 'Директор') {
message = 'Здравствуйте';
if (login == 'Employee') {
message = 'Hello';
} else if (login == 'Director') {
message = 'Greetings';
} else if (login == '') {
message = 'Нет логина';
message = 'No login';
} else {
message = '';
}

Binary file not shown.