Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Logical operators#401

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
14 commits
Select commitHold shift + click to select a range
8a9997d
Merge pull request #1 from javascript-tutorial/master
Mr-SaxobeatSep 17, 2019
ae5c0ae
Create article-pt.md
Mr-SaxobeatSep 17, 2019
ba126b4
Finish translating the OR part
Mr-SaxobeatSep 17, 2019
240ce48
Fix identation
Mr-SaxobeatSep 17, 2019
7797c95
Finish a simple translation
Mr-SaxobeatSep 17, 2019
8ee735b
Finish portuguese article
Mr-SaxobeatSep 18, 2019
e5a21be
Replace the english article by the portuguese
Mr-SaxobeatSep 18, 2019
7a982ad
Delete empty lines at the end of article
Mr-SaxobeatSep 18, 2019
02f7bd3
Translate the tasks and solutions
Mr-SaxobeatSep 19, 2019
34ce10e
refactor: changes correction suggestions according to PR number 56 an…
jonnathan-lsApr 16, 2023
67cb941
Merge branch 'master' of https://github.com/jonnathan-ls/pt.javascrip…
jonnathan-lsApr 17, 2023
a2483bf
refactor: translate words from svg file in article 'logical operators'
jonnathan-lsApr 17, 2023
ef863ab
Update article.md
odsantosJun 16, 2023
e645c6c
Merge branch 'master' of https://github.com/javascript-tutorial/pt.ja…
jonnathan-lsDec 23, 2023
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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
The answer is `2`,that's the first truthy value.
A resposta é `2`,pois é o primeiro valor verdadeiro.

```js run
alert( null || 2 || undefined );
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,10 @@ importance: 5

---

#What's the result of OR?
#Qual o resultado do *OR*?

What is the code below going to output?
Qual é a saída do código abaixo?

```js
alert( null || 2 || undefined );
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
The answer: first `1`,then `2`.
A resposta: primeiro `1`,depois `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`.
Ao chamar`alert`não é retornado nenhum valor. Ou seja, é retornado `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 outeralert.
1.O primeiro *OR* `||`avalia o operando da esquerda`alert(1)`.Que mostra a primeira mensagem com `1`.
2.O `alert`retorna `undefined`,então *OR* vai ao segundo operando procurando por um valor verdadeiro.
3.O segundo operando `2`é verdadeiro, então a execução é interrompida, `2`é retornado e, é mostrado pelo `alert` externo.

There will be no`3`,because the evaluation does not reach `alert(3)`.
Não haverá`3`,pois a execução não chega a `alert(3)`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,10 @@ importance: 3

---

#What's the result of OR'ed alerts?
#Qual o resultado do alerta de encadeamento de *OR*'s?

What will the code below output?
Qual a saída do código abaixo?

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

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
The answer: `null`,because it's the first falsy value from the list.
Resposta: `null`,pois é o primeiro valor falso da lista.

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

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,10 @@ importance: 5

---

#What is the result ofAND?
#Qual o resultado de *AND*?

What is this code going to show?
O que este código irá mostrar?

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

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
The answer: `1`,and then `undefined`.
Resposta: `1`,e depois `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).

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.
A chamada de `alert` retorna `undefined` (apenas mostra uma mensagem, então não existe nenhum retorno significativo).

Por causa disso, `&&` avalia o operando à esquerda (mostra `1`), e imediatamente interrompe, pois, `undefined` é um valor falso. *AND* `&&` procura por um valor falso e o retorna, então está feito.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,10 @@ importance: 3

---

#What is the result of AND'ed alerts?
#Qual o resultado dos alerts encadeados em *AND*?

What will this code show?
O que este código irá mostrar?

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

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
The answer: `3`.
Resposta: `3`.

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

The precedence ofAND `&&`is higher than`||`, so it executes first.
A precedência de *AND* `&&`é maior que *OR*`||`. Então ele é executado primeiro.

The result of `2 && 3 = 3`,so the expression becomes:
O resultado de `2 && 3 = 3`,então a expressão se torna:

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

Now the result is the first truthy value: `3`.

Agora o resultado é o primeiro valor verdadeiro: `3`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,11 +2,10 @@ importance: 5

---

#The result of ORAND OR
#O resultado de *OR* *AND* *OR*

What will the result be?
Qual será o resultado?

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

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -3,4 +3,3 @@
```js
if (age >= 14 && age <= 90)
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,8 @@ importance: 3

---

#Check the range between
#Verifique o intervalo entre

Write an `if` condition to check that`age`is between `14` and `90` inclusively.
Escreva uma condição "if" para verificar se`age`está no intervalo fechado de 14 a 90.

"Inclusively" means that `age`can reach the edges `14`or `90`.
"Fechado" significa que `age`pode chegar a ser `14`ou `90`.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
The first variant:
A primeira variação:

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

The second variant:
A segunda variação:

```js
if (age < 14 || age > 90)
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,8 +2,8 @@ importance: 3

---

#Check the range outside
#Verifique o exterior do intervalo

Write an`if`condition to check that`age`is NOT between `14` and `90` inclusively.
Escreva uma condição`if`para verificar se`age`NÃO está no intervalo fechado de 14 a 90.

Create two variants: the first one using NOT`!`,the second one --without it.
Crie duas variantes: a primeira usando NÃO`!`,e uma segunda --sem ele.
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
The answer: the first and the third will execute.
Resposta: o primeiro e o terceiro serão executados.

Details:
Detalhes:

```js run
//Runs.
//The result of -1 || 0 = -1,truthy
if (-1 || 0) alert( 'first' );
//Executa.
//O resultado de -1 || 0 = -1,verdadeiro.
if (-1 || 0) alert( 'primeiro' );

//Doesn't run
// -1 && 0 = 0,falsy
if (-1 && 0) alert( 'second' );
//Não executa.
// -1 && 0 = 0,falso
if (-1 && 0) alert( 'segundo' );

//Executes
//Operator&&has a higher precedence than ||
//so -1 && 1executes first, giving us the chain:
//Executa.
//O operador&&tem precedência maior que ||
//então -1&& 1executa primeiro, nos dando o encadeamento:
// null || -1 && 1 -> null || 1 -> 1
if (null || -1 && 1) alert( 'third' );
if (null || -1 && 1) alert( 'terceiro' );
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,15 +2,14 @@ importance: 5

---

#A question about "if"
#Uma questão sobre "if"

Which of these`alert`sare going to execute?
Qual destes`alert`sserão executados?

What will the results of the expressions be inside`if(...)`?
Qual será o resultado das expressões dentro dos`if(...)`s?

```js
if (-1 || 0) alert( 'first' );
if (-1 && 0) alert( 'second' );
if (null || -1 && 1) alert( 'third' );
if (-1 || 0) alert( 'primeiro' );
if (-1 && 0) alert( 'segundo' );
if (null || -1 && 1) alert( 'terceiro' );
```

View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@


```js run demo
let userName = prompt("Who's there?", '');
let userName = prompt("Quem está aí?", '');

if (userName === 'Admin') {

let pass = prompt('Password?', '');
let pass = prompt('Senha?', '');

if (pass === 'TheMaster') {
alert( 'Welcome!' );
alert( 'Bem vindo!' );
} else if (pass === '' || pass === null) {
alert( 'Canceled' );
alert( 'Cancelado.' );
} else {
alert( 'Wrong password' );
alert( 'Senha incorreta.' );
}

} else if (userName === '' || userName === null) {
alert( 'Canceled' );
alert( 'Cancelado' );
} else {
alert( "I don't know you" );
alert( "Eu não conheço você." );
}
```

Notethe vertical indents inside the`if` blocks. They are technically not required, but make the code more readable.
Noteas indentações verticais dentro dos blocos de`if`s. Tecnicamente, elas não são necessárias, mas tornam o código mais legível.
20 changes: 10 additions & 10 deletions1-js/02-first-steps/11-logical-operators/9-check-login/task.md
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,24 +2,24 @@ importance: 3

---

#Check the login
#Verifique o login

Write the code which asks for aloginwith `prompt`.
Escreva o código que irá perguntar por umlogincom um `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 anotherstring --then show "I don't know you".
Se o visitante digitar `"Admin"`,então faça`prompt`para uma senha, se a entrada é uma linha vazia ou `key:Esc` --mostre "Cancelada.",se for qualquer outra `string` --então mostre "Eu não conheço você.".

The password is checked as follows:
A senha é checada da seguinte forma:

-If it equals"TheMaster",then show "Welcome!",
-Anotherstring --show "Wrong password",
-For an emptystring or cancelled input, show "Canceled"
-Se for igual a"TheMaster",então mostre "Bem vindo!",
-Qualquer outra `string` --mostre "Senha incorreta",
-Para uma `string` vazia ou cancelada, mostre "Cancelada.".

The schema:
O esquema:

![](ifelse_task.svg)

Please use nested`if` blocks. Mind the overall readability of the code.
Use blocos agrupados de`if`s. Tenha em mente a legibilidade do código.

Hint: passing an empty input to aprompt returns an emptystring `''`.Pressing `key:ESC`during aprompt returns `null`.
Dica: passar uma entrada vazia para um `prompt` retorna umastringvazia`''`.Pressionar `key:ESC`durante o `prompt` retorna `null`.

[demo]
Loading

[8]ページ先頭

©2009-2025 Movatter.jp