Skip to content

translate part 1, 2.11- logical operators to AR #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer is `2`, that's the first truthy value.
الإجابة هي `2` لأنها أول قيمة truthy.

```js run
alert( null || 2 || undefined );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 5
الأهمية: 5

---

# What's the result of OR?
# ما نتيجة OR?

What is the code below going to output?
ما خرج الأمر التالي ؟

```js
alert( null || 2 || undefined );
Expand Down
12 changes: 6 additions & 6 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
The answer: first `1`, then `2`.
الإجابة: أولًا `1` ثم `2`.

```js run
alert( alert(1) || 2 || alert(3) );
```

The call to `alert` does not return a value. Or, in other words, it returns `undefined`.
استدعائ `alert` لا يرجع أي قيمة أو بمعنى آخر يرجع `undefined`.

1. The first OR `||` evaluates its left operand `alert(1)`. That shows the first message with `1`.
2. The `alert` returns `undefined`, so OR goes on to the second operand searching for a truthy value.
3. The second operand `2` is truthy, so the execution is halted, `2` is returned and then shown by the outer alert.
1. أول OR `||` تنفذ العملية على يسارها `alert(1)`. وهذا يعرض أول رسالة `1`.
2. إن `alert` ترجع `undefined` لذلك تنتقل OR للعملية الثانية بحثًا عن قيمة truthy.
3. القيمة الثانية `2` هي truthy لذلك يتوقف التنفيذ وترجع `2` ويتم عرضها.

There will be no `3`, because the evaluation does not reach `alert(3)`.
لن يتم عرض `3` لأن التنفيذ لم يصل إلى `alert(3)`.
6 changes: 3 additions & 3 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/task.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 3
الأهمية: 3

---

# What's the result of OR'ed alerts?
# نتيجة التنبيهات التي بينها OR?

What will the code below output?
ما خرج الكود التالي ؟

```js
alert( alert(1) || 2 || alert(3) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
The answer: `null`, because it's the first falsy value from the list.
الإجابة: `null` لأنها أول قيمة falsy في القائمة.

```js run
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 5
الأهمية: 5

---

# What is the result of AND?
# نتيحة AND?

What is this code going to show?
ما خرج الكود التالي ؟

```js
alert( 1 && null && 2 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
The answer: `1`, and then `undefined`.
الإجابة: `1` ثم `undefined`.

```js run
alert( alert(1) && alert(2) );
```

The call to `alert` returns `undefined` (it just shows a message, so there's no meaningful return).
استدعاء `alert` يرجع `undefined` (إنها تعرض رسالة لذلك لا يوجد معنى لإرجاع قيمة).

Because of that, `&&` evaluates the left operand (outputs `1`), and immediately stops, because `undefined` is a falsy value. And `&&` looks for a falsy value and returns it, so it's done.
وبسبب هذا تقوم `&&` بتنفيذ العملية على اليسار (تعرض `1`) وتتوقف لأن `undefined` تعتبر قيمة falsy. And `&&` تبحث عن قيمة falsy وترجعها.

5 changes: 3 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/4-alert-and/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ importance: 3

---

# What is the result of AND'ed alerts?
# نتيجة التنبيهات التي بينها AND?

ما خرج الكود التالي ؟

What will this code show?

```js
alert( alert(1) && alert(2) );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
The answer: `3`.
الإجابة: `3`.

```js run
alert( null || 2 && 3 || 4 );
```

The precedence of AND `&&` is higher than `||`, so it executes first.
أولوية AND `&&` أعلى من `||` لذلك تنفذ أولًا.

The result of `2 && 3 = 3`, so the expression becomes:
نتيجة `2 && 3 = 3` لذلك يصبح التعبير كالتالي:

```
null || 3 || 4
```

Now the result is the first truthy value: `3`.
النتيجة الآن هي أول قيمة truthy: `3`.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
importance: 5
الأهمية: 5

---

# The result of OR AND OR
# نتيجة OR AND OR

What will the result be?
ما ناتج تنفيذ الأمر التالي ؟

```js
alert( null || 2 && 3 || 4 );
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
importance: 3
الأهمية: 3

---

# Check the range between
# فحص المدى بين

Write an "if" condition to check that `age` is between `14` and `90` inclusively.
اكتب تعبير "if" يفحص أن العمر `age` بين `14` و `90`.

"Inclusively" means that `age` can reach the edges `14` or `90`.
يمكن للعمر `age` أن يكون `14` أو `90`.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
The first variant:
الطريقة الأولى:

```js
if (!(age >= 14 && age <= 90))
```

The second variant:
الطريقة الثانية:

```js
if (age < 14 || age > 90)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
importance: 3
الأهمية: 3

---

# Check the range outside
# فحص المدى بالخارج

Write an `if` condition to check that `age` is NOT between 14 and 90 inclusively.
اكتب تعبير `if` يفحص أن العمر `age` ليس بين.

Create two variants: the first one using NOT `!`, the second one -- without it.
قم بالحل بطريقتين مختلفتين: الأولى باستخدام NOT `!` والثانية بدونها.
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
The answer: the first and the third will execute.
الإجابة: سيتم تنفيذ الأول والثالث

Details:
التفاصيل:

```js run
// Runs.
// The result of -1 || 0 = -1, truthy
// ينفذ
// نتيجة -1 || 0 = -1 هي truthy
if (-1 || 0) alert( 'first' );

// Doesn't run
// لا ينفذ
// -1 && 0 = 0, falsy
if (-1 && 0) alert( 'second' );

// Executes
// Operator && has a higher precedence than ||
// so -1 && 1 executes first, giving us the chain:
// ينفذ
// المعامل && له أولوية أعلى من ||
// لذلك يتم تنفيذ -1 && 1 أولا وينتج التالي:
// null || -1 && 1 -> null || 1 -> 1
if (null || -1 && 1) alert( 'third' );
```
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
importance: 5
الأهمية: 5

---

# A question about "if"
# سؤال عن "if"

Which of these `alert`s are going to execute?
أي من هذه التنبيهات `alert` سيتم تنفيذه ؟

What will the results of the expressions be inside `if(...)`?
ماذا ستكون نتيجة التعبيرات داخل `if(...)`?

```js
if (-1 || 0) alert( 'first' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ if (userName == 'Admin') {
}
```

Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable.
لاحظ المسافات داخل `if`. غير مطلوبة فعليًا ولكنها تجعل الكود مقروء أكثر.
22 changes: 11 additions & 11 deletions 1-js/02-first-steps/11-logical-operators/9-check-login/task.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
importance: 3
الأهمية: 3

---

# Check the login
# فحص تسجيل الدخول

Write the code which asks for a login with `prompt`.
أكتب كود يكلب تسجيل الدخول باستخدام `prompt`.

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".
إذا كتب المستخدم كلمة `"Admin"` اطلب منه `prompt` كلمة المرور فإذا كتب سطر فارغ أو استخدم `key:Esc` -- أظهر رسالة "Canceled" وإذا كان نص آخر أظهر له "I don't know you".

The password is checked as follows:
يتم فحص كلمة المرور كالتالي:

- If it equals "TheMaster", then show "Welcome!",
- Another string -- show "Wrong password",
- For an empty string or cancelled input, show "Canceled"
- إذا كانت تساوي "TheMaster" يتم عرض "Welcome!",
- نص آخر يتم عرض "Wrong password",
- نص فارغ أو إلغاء العملية يتم عرض "Canceled"

The schema:
الصيغة العامة:

![](ifelse_task.svg)

Please use nested `if` blocks. Mind the overall readability of the code.
استخدم تعبيرات `if` متداخلة. انتبه أن يكون الكود مقروء.

Hint: passing an empty input to a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`.
ملحوظة: تمرير نص فارغ إلى prompt يرجع نص فارغ `''`. تمرير `key:ESC` يرجع `null`.

[demo]
Loading