You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/02-first-steps/10-ifelse/article.md
+51-50Lines changed: 51 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -40,78 +40,78 @@ Let's recall the conversion rules from the chapter <info:type-conversions>:
40
40
- A number `0`, an empty string `""`, `null`, `undefined`, and `NaN` all become `false`. Because of that they are called "falsy" values.
41
41
- Other values become `true`, so they are called "truthy".
42
42
43
-
So, the code under this condition would never execute:
43
+
Ошондо, мындай шарттан кийин кеткен код эч качан аткарылбайт:
44
44
45
45
```js
46
46
if (0) { // 0 бул жалган
47
47
...
48
48
}
49
49
```
50
50
51
-
...and inside this condition -- it always will:
51
+
...жана мындай шарттан кийин ал ар дайым аткарылат:
52
52
53
53
```js
54
54
if (1) { // 1 бул чындык
55
55
...
56
56
}
57
57
```
58
58
59
-
We can also pass a pre-evaluated boolean value to `if`, like this:
59
+
Биз ошондой эле алдын ала айландырылган логикалык маанини `if`ге өткөрө алабыз, мисалы:
60
60
61
61
```js
62
-
let cond = (year ==2015); //equality evaluates to true or false
62
+
let cond = (year ==2015); //теңештирүү true же false'го айланат
63
63
64
64
if (cond) {
65
65
...
66
66
}
67
67
```
68
68
69
-
## The "else" clause
69
+
## "else" пункту
70
70
71
-
The `if`statement may contain an optional "else" block. It executes when the condition is falsy.
71
+
`if`нускамасы кошумча "else" блогун камтышы мүмкүн. Шарт жалган болгондо ал аткарылат.
72
72
73
-
For example:
73
+
Мисалы:
74
74
```js run
75
-
let year =prompt('In which year was the ECMAScript-2015 specification published?', '');
75
+
let year =prompt('ECMAScript-2015 спецификациясы кайсы жылы жарыяланган?', '');
76
76
77
77
if (year ==2015) {
78
-
alert( 'You guessed it right!' );
78
+
alert( 'Туура таптыңыз!' );
79
79
} else {
80
-
alert( 'How can you be so wrong?' ); //any value except 2015
80
+
alert( 'Кантип ушунчалык ката кетиресиз?' ); // 2015-тен башкасы
81
81
}
82
82
```
83
83
84
-
## Several conditions: "else if"
84
+
## Бир нече шарт түзүү: "else if"
85
85
86
-
Sometimes, we'd like to test several variants of a condition. The `else if`clause lets us do that.
86
+
Кээде биз шарттын бир нече варианттарын сынагыбыз келет. `else if`пункту бизге муну кылууга жол берет.
87
87
88
-
For example:
88
+
Мисалы:
89
89
90
90
```js run
91
-
let year =prompt('In which year was the ECMAScript-2015 specification published?', '');
91
+
let year =prompt('ECMAScript-2015 спецификациясы кайсы жылы жарыяланган?', '');
92
92
93
93
if (year <2015) {
94
-
alert( 'Too early...' );
94
+
alert( 'Өтө эрте...' );
95
95
} elseif (year >2015) {
96
-
alert( 'Too late' );
96
+
alert( 'Өтө кеч' );
97
97
} else {
98
-
alert( 'Exactly!' );
98
+
alert( 'Дал ошондой!' );
99
99
}
100
100
```
101
101
102
102
In the code above, JavaScript first checks `year < 2015`. If that is falsy, it goes to the next condition `year > 2015`. If that is also falsy, it shows the last `alert`.
103
103
104
-
There can be more `else if`blocks. The final`else`is optional.
104
+
`else if`блоктору көбүрөөк болушу мүмкүн. Эң акыркы`else`кошумча болуп саналат.
105
105
106
-
## Conditional operator '?'
106
+
## Шарттык оператор '?'
107
107
108
-
Sometimes, we need to assign a variable depending on a condition.
108
+
Кээде биз шартка көз каранды болгон өзгөрмө ыйгарышыбыз керек.
109
109
110
-
For instance:
110
+
Мисалы үчүн:
111
111
112
112
```js run no-beautify
113
113
let accessAllowed;
114
-
let age =prompt('How old are you?', '');
114
+
let age =prompt('Сиз канча жаштасыз?', '');
115
115
116
116
*!*
117
117
if (age >18) {
@@ -124,18 +124,18 @@ if (age > 18) {
124
124
alert(accessAllowed);
125
125
```
126
126
127
-
The so-called "conditional" or "question mark" operator lets us do that in a shorter and simpler way.
127
+
"Шарттык" же "суроо белгиси" деп аталган оператор жогорудагы шартты кыскараак жана жөнөкөй жол менен жасоого жол берет.
128
128
129
129
The operator is represented by a question mark `?`. Sometimes it's called "ternary", because the operator has three operands. It is actually the one and only operator in JavaScript which has that many.
130
130
131
-
The syntax is:
131
+
Анын жазылышы:
132
132
```js
133
133
let result = condition ? value1 : value2;
134
134
```
135
135
136
-
The `condition`is evaluated: if it's truthy then`value1`is returned, otherwise -- `value2`.
136
+
`condition`мындай эсептелинет: эгерде ал чындык болсо`value1`кайтарылат, болбосо -- `value2`.
137
137
138
-
For example:
138
+
Мисалы:
139
139
140
140
```js
141
141
let accessAllowed = (age >18) ?true:false;
@@ -162,18 +162,18 @@ let accessAllowed = age > 18;
162
162
```
163
163
````
164
164
165
-
## Multiple '?'
165
+
## Бир нече '?'
166
166
167
-
A sequence of question mark operators `?`can return a value that depends on more than one condition.
167
+
Суроо белгиси операторлорунун `?`ырааттуулугу бирден ашык шартка көз каранды болгон маанини кайтара алат.
168
168
169
-
For instance:
169
+
Мисалы үчүн:
170
170
```js run
171
-
let age =prompt('age?', 18);
171
+
let age =prompt('жашыңыз?', 18);
172
172
173
-
let message = (age <3) ?'Hi, baby!':
174
-
(age <18) ?'Hello!':
175
-
(age <100) ?'Greetings!':
176
-
'What an unusual age!';
173
+
let message = (age <3) ?'Салам, балакай!':
174
+
(age <18) ?'Салам!':
175
+
(age <100) ?'Саламатсызбы!':
176
+
'Жашыңыз кандай укмуштуу!';
177
177
178
178
alert( message );
179
179
```
@@ -186,54 +186,55 @@ It may be difficult at first to grasp what's going on. But after a closer look,
186
186
4. If that's true -- it returns `'Greetings!'`. Otherwise, it continues to the expression after the last colon '":"', returning `'What an unusual age!'`.
187
187
188
188
Here's how this looks using `if..else`:
189
+
Мында `if..else` колдонгондон кийинки көрүнүшү:
189
190
190
191
```js
191
192
if (age <3) {
192
-
message ='Hi, baby!';
193
+
message ='Салам, балакай!';
193
194
} elseif (age <18) {
194
-
message ='Hello!';
195
+
message ='Салам!';
195
196
} elseif (age <100) {
196
-
message ='Greetings!';
197
+
message ='Саламатсызбы!';
197
198
} else {
198
-
message ='What an unusual age!';
199
+
message ='Жашыңыз кандай укмуштуу!';
199
200
}
200
201
```
201
202
202
-
## Non-traditional use of '?'
203
+
## '?' белгисинин адатсыз колдонушу
203
204
204
-
Sometimes the question mark `?`is used as a replacement for `if`:
205
+
Кээде суроо белгиси `?`- `if`'ти алмаштыруучу катары колдонулат:
205
206
206
207
```js run no-beautify
207
-
let company =prompt('Which company created JavaScript?', '');
208
+
let company =prompt('Кайсы компания JavaScript'ти түзгөн?', '');
208
209
209
210
*!*
210
211
(company == 'Netscape') ?
211
-
alert('Right!') :alert('Wrong.');
212
+
alert('Туура!') : alert('Туура эмес.');
212
213
*/!*
213
214
```
214
215
215
-
Depending on the condition `company == 'Netscape'`, either the first or the second expression after the `?`gets executed and shows an alert.
216
+
`company == 'Netscape'` шартына жараша, `?` белгисинен кийинки биринчи же экинчи туюнтма аткарылып, билдирүү көрсөтүлөт.
216
217
217
-
We don't assign a result to a variable here. Instead, we execute different code depending on the condition.
218
+
Бул жерде натыйжаны өзгөрмөгө ыйгарбайбыз. Анын ордуна, шартка жараша ар кандай кодду аткарабыз.
218
219
219
-
**It's not recommended to use the question mark operator in this way.**
220
+
**Суроо белгиси операторун мындай колдонгону сунушталбайт.**
220
221
221
222
The notation is shorter than the equivalent `if` statement, which appeals to some programmers. But it is less readable.
222
223
223
224
Here is the same code using `if` for comparison:
224
225
225
226
```js run no-beautify
226
-
let company =prompt('Which company created JavaScript?', '');
227
+
let company = prompt('Кайсы компания JavaScript'ти түзгөн?', '');
227
228
228
229
*!*
229
230
if (company =='Netscape') {
230
-
alert('Right!');
231
+
alert('Туура!');
231
232
} else {
232
-
alert('Wrong.');
233
+
alert('Туура эмес.');
233
234
}
234
235
*/!*
235
236
```
236
237
237
-
Our eyes scan the code vertically. Code blocks which span several lines are easier to understand than a long, horizontal instruction set.
238
+
Биздин көзүбүз кодду өйдөдөн ылдый көрөт. Бир нече саптардан турган код блокторун узун, горизонталдуу нускамаларга караганда түшүнүүгө оңой.
238
239
239
-
The purpose of the question mark operator `?`is to return one value or another depending on its condition. Please use it for exactly that. Use `if` when you need to execute different branches of code.
240
+
Суроо белгиси операторунун `?`максаты – анын шартына жараша тигил же бул маанини кайтаруу. Сураныч, аны дал ушул үчүн колдонуңуз. Коддун ар кандай бутактарын аткарыш керек болгондо `if` колдонуңуз.
0 commit comments