Skip to content

Logical operators #100

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 24 commits into from
Mar 27, 2021
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
Expand Up @@ -2,9 +2,9 @@ importance: 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` turthy است، پس عملیات متوقف شده، `2` بر گردانده می شود و سپس توسط alert بیرونی نمایش داده می شود.

There will be no `3`, because the evaluation does not reach `alert(3)`.
هیچ `3`ای در کار نخواهد بود، چون ارزیابی به `alert(3)` نمی رسد.
4 changes: 2 additions & 2 deletions 1-js/02-first-steps/11-logical-operators/2-alert-or/task.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 3

---

# What's the result of OR'ed alerts?
# نتیجه alert های دارای 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
Expand Up @@ -2,9 +2,9 @@ importance: 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` را بر می گرداند (آن فقط یک پیام را نمایش می دهد، پس هیچ return معناداری وجود ندارد).

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 است. و `&&` به دنبال یک مقدار falsy می گردد و آن را بر می گرداند، بنابراین کار تمام می شود.

4 changes: 2 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,9 @@ importance: 3

---

# What is the result of AND'ed alerts?
# نتیجه alert های دارای 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:
نتیجه `3 = 3 && 2`، پس عبارت تبدیل می شود به:

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

Now the result is the first truthy value: `3`.
حالا نتیجه اولین مقدار truthy است: `3`.

Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ importance: 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
Expand Up @@ -2,8 +2,8 @@ importance: 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
Expand Up @@ -2,8 +2,8 @@ importance: 3

---

# Check the range outside
# محدوده خارج را بررسی کنید

Write an `if` condition to check that `age` is NOT between `14` and `90` inclusively.
یک شرط `if` بنویسید که بررسی کند `age` بین `14` و `90` به صورتی که که شامل خود آنها هم بشود نباشد.

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
Expand Up @@ -2,11 +2,11 @@ importance: 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
Expand Up @@ -2,24 +2,24 @@ importance: 3

---

# Check the login
# login را بررسی کنید

Write the code which asks for a login with `prompt`.
کدی بنویسید که با `prompt` برای login درخواست کند.

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" را نمایش دهید، اگر رشته(string) دیگری باشد -- سپس "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!" را نمایش دهید،
- اگر رشته(string) دیگری باشد -- "Wrong password" را نمایش دهید،
- اگر یک رشته(string) خالی یا ورودی cancelled باشد، "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` در حین prompt `null` را بر می گرداند.

[demo]
[دمو]
Loading