diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md index 8869d32e6..d3b14ae33 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/solution.md @@ -1,6 +1,5 @@ -The answer is `2`, that's the first truthy value. +A resposta é `2`, que é o primeiro valor verdadeiro. ```js run alert( null || 2 || undefined ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md index a7c9addfc..579909855 100644 --- a/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md +++ b/1-js/02-first-steps/11-logical-operators/1-alert-null-2-undefined/task.md @@ -1,12 +1,11 @@ -importance: 5 +importância: 5 --- -# What's the result of OR? +# Qual o resultado do OU? -What is the code below going to output? +Qual é a saída do código abaixo? ```js alert( null || 2 || undefined ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md index 8f4d664e8..8cd9d1dfd 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/solution.md @@ -1,13 +1,14 @@ 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 it's 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. O primeiro OU `||` avalia o operando da esquerda `alert(1)`. Que mostra a primeira mensagem com `1`. +2. O `alert` retorna `undefined`, então OU 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)`. diff --git a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md index 3908fa2ec..ad240ee4c 100644 --- a/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/2-alert-or/task.md @@ -1,12 +1,11 @@ -importance: 3 +importância: 3 --- -# What's the result of OR'ed alerts? +# Qual o resultado do alerta de encadeamento de OU's? -What will the code below output? +Qual a saída do código abaixo? ```js alert( alert(1) || 2 || alert(3) ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md index 5c2455ef4..788d42300 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/solution.md @@ -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 ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md index 043d431e4..d6d5d1484 100644 --- a/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md +++ b/1-js/02-first-steps/11-logical-operators/3-alert-1-null-2/task.md @@ -1,12 +1,11 @@ -importance: 5 +importância: 5 --- -# What is the result of AND? +# Qual o resultado de E? -What is this code going to show? +O que este código irá mostrar? ```js alert( 1 && null && 2 ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md index b6fb10d72..e54ee7326 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/solution.md @@ -1,10 +1,11 @@ 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). +A chamada de `alert` retorna `undefined` (apenas mostra uma mensagem, então não existe nenhum retorno significativo). -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. - +Por causa disso, `&&` avalia o operando à esquerda (mostra `1`), e imediatamente interrompe, pois `undefined` é um valor falso. E `&&` procura por um valor falso e o retorna, então está feito. diff --git a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md index 69f877b95..e064c6b08 100644 --- a/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md +++ b/1-js/02-first-steps/11-logical-operators/4-alert-and/task.md @@ -1,12 +1,11 @@ -importance: 3 +importância: 3 --- -# What is the result of AND'ed alerts? +# Qual o resultado dos alerts encadeados em E? -What will this code show? +O que este código irá mostrar? ```js alert( alert(1) && alert(2) ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md index 25e3568f8..a66daff69 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/solution.md @@ -1,16 +1,15 @@ -The answer: `3`. +Resposta: `3`. ```js run alert( null || 2 && 3 || 4 ); ``` -The precedence of AND `&&` is higher than `||`, so it executes first. +A precedência de E `&&` é maior do que OU `||`. 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`. diff --git a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md index b18bb9c51..6a7d96bcf 100644 --- a/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md +++ b/1-js/02-first-steps/11-logical-operators/5-alert-and-or/task.md @@ -1,12 +1,11 @@ -importance: 5 +importância: 5 --- -# The result of OR AND OR +# O resultado de OR E OR -What will the result be? +Qual será o resultado? ```js alert( null || 2 && 3 || 4 ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md index 87c733b22..02c994050 100644 --- a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md +++ b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/solution.md @@ -3,4 +3,3 @@ ```js if (age >= 14 && age <= 90) ``` - diff --git a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md index cc00ca9fc..eefe771ea 100644 --- a/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/6-check-if-in-range/task.md @@ -1,9 +1,9 @@ -importance: 3 +importância: 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 `idade` está entre o intervalo fechado `14` e `90`. -"Inclusively" means that `age` can reach the edges `14` or `90`. +"Fechado" significa que `idade` pode chegar a ser `14` ou `90`. diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md index d1946a967..ca0e0f8f6 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/solution.md @@ -1,12 +1,11 @@ -The first variant: +A primeira variação: ```js -if (!(age >= 14 && age <= 90)) +if (!(idade >= 14 && idade <= 90)) ``` -The second variant: +A segunda variação: ```js -if (age < 14 || age > 90) +if (idade < 14 || idade > 90) ``` - diff --git a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md index 7c22d6ad1..a57227d29 100644 --- a/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md +++ b/1-js/02-first-steps/11-logical-operators/7-check-if-out-range/task.md @@ -1,9 +1,9 @@ -importance: 3 +importância: 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 `idade` NÃO está entre o intervalo fechado `14` e `90`. -Create two variants: the first one using NOT `!`, the second one -- without it. +Crie duas variações: a primeira usando NÃO `!`, e uma segunda -- sem ele. diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md index 210509758..b6f568df1 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/solution.md @@ -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 && 1 executes first, giving us the chain: +// Executa. +// O operador && tem precedência maior que || +// então -1 && 1 executa primeiro, nos dando o encadeamento: // null || -1 && 1 -> null || 1 -> 1 -if (null || -1 && 1) alert( 'third' ); +if (null || -1 && 1) alert( 'terceiro' ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md index 55987121b..48d3dd8f4 100644 --- a/1-js/02-first-steps/11-logical-operators/8-if-question/task.md +++ b/1-js/02-first-steps/11-logical-operators/8-if-question/task.md @@ -1,16 +1,15 @@ -importance: 5 +importância: 5 --- -# A question about "if" +# A questão sobre "if" -Which of these `alert`s are going to execute? +Qual destes `alert`s serã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' ); ``` - diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md index b535650ec..e79ced5b3 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/solution.md @@ -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ê." ); } ``` -Note the vertical indents inside the `if` blocks. They are technically not required, but make the code more readable. +Note as identações verticais dentro dos blocos de `if`s. Tecnicamente, elas não são necessárias, mas fazem o código mais legível. diff --git a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md index 0728efad1..a17632052 100644 --- a/1-js/02-first-steps/11-logical-operators/9-check-login/task.md +++ b/1-js/02-first-steps/11-logical-operators/9-check-login/task.md @@ -1,25 +1,25 @@ -importance: 3 +importância: 3 --- -# Check the login +# Verifique o login -Write the code which asks for a login with `prompt`. +Escreva o código que irá perguntar por um login com 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 another string -- then show "I don't know you". +Se o visitante digitar `"Admin"`, então faça `prompt` para a senha, se a entrada é uma linha vazia ou `key:Esc` -- mostre "Cancelado.", 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!", -- Another string -- show "Wrong password", -- For an empty string 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 "Cancelado.". -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 a prompt returns an empty string `''`. Pressing `key:ESC` during a prompt returns `null`. +Dica: passar uma entrada vazia para um `prompt` retorna uma string vazia `''`. Pressionar `key:ESC` durante o `prompt` retorna `null`. [demo] diff --git a/1-js/02-first-steps/11-logical-operators/article.md b/1-js/02-first-steps/11-logical-operators/article.md index 4932020ae..35e336137 100644 --- a/1-js/02-first-steps/11-logical-operators/article.md +++ b/1-js/02-first-steps/11-logical-operators/article.md @@ -1,24 +1,24 @@ -# Logical operators +# Operadores lógicos -There are three logical operators in JavaScript: `||` (OR), `&&` (AND), `!` (NOT). +Existem três operadores lógicos em JavaScript: `||` (OU), `&&` (E), `!` (NÃO). -Although they are called "logical", they can be applied to values of any type, not only boolean. Their result can also be of any type. +Embora eles sejam chamados de "lógicos", podem ser aplicados a valores de qualquer tipo, não apenas a boolean. Seus resultados também podem ser de qualquer tipo. -Let's see the details. +Vamos ver os detalhes. -## || (OR) +## || (OU) -The "OR" operator is represented with two vertical line symbols: +O operador "OU" é representado com dois símbolos de linha vertical: ```js result = a || b; ``` -In classical programming, the logical OR is meant to manipulate boolean values only. If any of its arguments are `true`, it returns `true`, otherwise it returns `false`. +Em programação clássica, o operador OU é mencionado para manipular apenas valores booleanos. Se qualquer um dos seus argumentos for `true`, ele retorna `true`, se não, retorna `false`. -In JavaScript, the operator is a little bit trickier and more powerful. But first, let's see what happens with boolean values. +Em JavaScript, este operador é um pouco mais útil e poderoso. Mas primeiro, vamos ver o que acontece com valores booleanos. -There are four possible logical combinations: +Existem quatro combinações lógicas possíveis: ```js run alert( true || true ); // true @@ -27,83 +27,83 @@ alert( true || false ); // true alert( false || false ); // false ``` -As we can see, the result is always `true` except for the case when both operands are `false`. +Como podemos ver, o resultado é sempre `true`, exceto para o caso onde os dois operandos são `false`. -If an operand is not a boolean, it's converted to a boolean for the evaluation. +Se um operando não é um boolean, ele é convertido em um boolean para ser avaliado. -For instance, the number `1` is treated as `true`, the number `0` as `false`: +Por exemplo, o número `1` é tratado com `true` e o número `0` como `false`. ```js run -if (1 || 0) { // works just like if( true || false ) - alert( 'truthy!' ); +if (1 || 0) { // funciona como if( true || false) + alert( 'verdadeiro!'); } ``` -Most of the time, OR `||` is used in an `if` statement to test if *any* of the given conditions is `true`. +Na maioria das vezes, OU `||` é usado dentro de uma expressão `if` para testar se *qualquer* uma das condições dadas é `true`. -For example: +Por exemplo: ```js run -let hour = 9; +let hour = 0; *!* if (hour < 10 || hour > 18) { */!* - alert( 'The office is closed.' ); + alert( 'O escritório está fechado.' ); } ``` -We can pass more conditions: +Nós podemos passar mais condições: ```js run let hour = 12; let isWeekend = true; if (hour < 10 || hour > 18 || isWeekend) { - alert( 'The office is closed.' ); // it is the weekend + alert( 'O escritório está fechado.' ); // é final de semana } ``` -## OR finds the first truthy value +## OU encontra o primeiro valor verdadeiro -The logic described above is somewhat classical. Now, let's bring in the "extra" features of JavaScript. +A lógica descrita acima é algo clássico. Agora, vamos ver as funcionalidades "extras" do JavaScript. -The extended algorithm works as follows. +O algorítmo extendido funciona da seguinte forma. -Given multiple OR'ed values: +Dando múltiplos valores encadeados em OU's: ```js result = value1 || value2 || value3; ``` -The OR `||` operator does the following: +O operador OU `||` faz o seguinte: -- Evaluates operands from left to right. -- For each operand, converts it to boolean. If the result is `true`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were `false`), returns the last operand. +- Avalia os operandos da esquerda para a direita. +- Para cada operando, o converte para boolean. Se o resultado é `true`, para e retorna o valor original daquele operando. +- Se todos os operandos foram avaliados (i.e. todos são `false`), retorna o último operando. -A value is returned in its original form, without the conversion. +Um valor é retornado na sua forma original, sem conversão. -In other words, a chain of OR `"||"` returns the first truthy value or the last one if no such value is found. +Em outras palavras, uma cadeia de OU `"||"` retorna o primeiro valor verdadeiro ou o último se nenhum `true` for encontrado. -For instance: +Por exemplo: ```js run -alert( 1 || 0 ); // 1 (1 is truthy) -alert( true || 'no matter what' ); // (true is truthy) +alert( 1 || 0 ); // 1 (1 é verdadeiro) +alert( true || 'não importa o quê' ); // (true é verdadeiro) -alert( null || 1 ); // 1 (1 is the first truthy value) -alert( null || 0 || 1 ); // 1 (the first truthy value) -alert( undefined || null || 0 ); // 0 (all falsy, returns the last value) +alert( null || 1 ); // 1 (1 é o primeiro valor verdadeiro) +alert( null || 0 || 1 ); // 1 (o primeiro valor verdadeiro) +alert( undefined || null || 0 ); // 0 (todos falsos, retorna o último valor) ``` -This leads to some interesting usage compared to a "pure, classical, boolean-only OR". +Isso nos mostra algumas utilidades interessantes comparadas ao "puro, clássico, apenas-boolean OU". -1. **Getting the first truthy value from a list of variables or expressions.** +1. **Obtendo o primeiro valor verdadeiro de uma lista de variáveis ou expressões.** - Imagine we have several variables which can either contain data or be `null/undefined`. How can we find the first one with data? + Imagine que temos várias variáveis que podem conter algum dado ou ser `null/undefined`. Como podemos encontrar a primeira com algum dado? - We can use OR `||`: + Nós podemos usar OU `||`: ```js run let currentUser = null; @@ -113,27 +113,27 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl let name = currentUser || defaultUser || "unnamed"; */!* - alert( name ); // selects "John" – the first truthy value + alert( name ); // seleciona "John" - o primeiro valor verdadeiro ``` - If both `currentUser` and `defaultUser` were falsy, `"unnamed"` would be the result. -2. **Short-circuit evaluation.** + Se ambos `currentUser` e `defaultUser` forem falsos, o resultado será `"unnamed"`. +2. **Avaliação de curto-circuito** - Operands can be not only values, but arbitrary expressions. OR evaluates and tests them from left to right. The evaluation stops when a truthy value is reached, and the value is returned. This process is called "a short-circuit evaluation" because it goes as short as possible from left to right. + Operandos podem não ser apenas valores, mas operações arbitrárias. OU interpreta e testa elas da esquerda para a direita. A avaliação é interrompida quando um valor verdadeiro é encontrado e este valor é retornado. Este processo é chamado de "avaliação de curto-circuito" pois vai o mais curto possível da esquerda para a direita. - This is clearly seen when the expression given as the second argument has a side effect like a variable assignment. + Isto é claramente visto quando a expressão dada como segundo argumento tem um efeito como a atribuição de uma variável. - In the example below, `x` does not get assigned: + No exemplo abaixo, `x` não tem nenhuma atribuição: ```js run no-beautify let x; *!*true*/!* || (x = 1); - alert(x); // undefined, because (x = 1) not evaluated + alert(x); // undefined, pois (x = 1) não é avaliado ``` - If, instead, the first argument is `false`, `||` evaluates the second one, thus running the assignment: + Se, por outro lado, o primeiro argumento é `false`, `||` avalia o segundo, fazendo assim a atribuição: ```js run no-beautify let x; @@ -143,21 +143,21 @@ This leads to some interesting usage compared to a "pure, classical, boolean-onl alert(x); // 1 ``` - An assignment is a simple case. Other side effects can also be involved. + Uma atribuição é um caso simples. Outros ##SIDE EFFECTS(???)## podem também estarem envolvidos. - As we can see, such a use case is a "shorter way of doing `if`". The first operand is converted to boolean. If it's false, the second one is evaluated. + Como podemos ver, esse caso é como "uma maneira mais curta de se usar `if`". O primeiro operando é convertido para boolean. Se for false, o segundo operando é avaliado. - Most of time, it's better to use a "regular" `if` to keep the code easy to understand, but sometimes this can be handy. + Na maioria das vezes, é melhor usar o `if` "regular" para manter a facilidade de entendimento do código, mas vez ou outra isso pode ser útil. -## && (AND) +## && (E) -The AND operator is represented with two ampersands `&&`: +O operador E é representado com dois e's comerciais `&&`: ```js result = a && b; ``` -In classical programming, AND returns `true` if both operands are truthy and `false` otherwise: +Em programação clássica, E retorna `true` se ambos os operandos forem verdadeiros ou `false`, caso contrário: ```js run alert( true && true ); // true @@ -166,138 +166,137 @@ alert( true && false ); // false alert( false && false ); // false ``` -An example with `if`: +Um exemplo com `if`: ```js run let hour = 12; let minute = 30; if (hour == 12 && minute == 30) { - alert( 'The time is 12:30' ); + alert( 'Agora são 12:30' ); } ``` -Just as with OR, any value is allowed as an operand of AND: +Da mesma forma que o OU, qualquer valor é permitido como um operando de E: ```js run -if (1 && 0) { // evaluated as true && false - alert( "won't work, because the result is falsy" ); +if (1 && 0) { // interpreta como true && false + alert( "não funciona, pois o resultado é falso" ); } ``` +## "E" encontra o primeiro valor falso -## AND finds the first falsy value - -Given multiple AND'ed values: +Dados múltiplos valores encadeados em E's: ```js result = value1 && value2 && value3; ``` -The AND `&&` operator does the following: +O operador `&&` faz o seguinte: -- Evaluates operands from left to right. -- For each operand, converts it to a boolean. If the result is `false`, stops and returns the original value of that operand. -- If all operands have been evaluated (i.e. all were truthy), returns the last operand. +- Avalia os operandos da esquerda para a direita. +- Para cada operando, o converte para um boolean. Se o resultado for `false`, interrompe e retorna o valor original daquele operando. +- Se todos os operandos foram avaliados (i.e. todos são verdadeiros), retorna o último operando. -In other words, AND returns the first falsy value or the last value if none were found. +Em outras palavras, E retorna o primeiro valor falso ou o último valor se nenhum for falso. -The rules above are similar to OR. The difference is that AND returns the first *falsy* value while OR returns the first *truthy* one. +As regras acima são similares ao OU. A diferença é que E retorna o primeiro valor *falso* enquanto OR retorna o primeiro valor *verdadeiro*. -Examples: +Exemplos: ```js run -// if the first operand is truthy, -// AND returns the second operand: +// se o primeiro valor é verdadeiro, +// E retorna o segundo parâmetro: alert( 1 && 0 ); // 0 alert( 1 && 5 ); // 5 -// if the first operand is falsy, -// AND returns it. The second operand is ignored +// se o primeiro valor é falso, +// E retorna ele. O segundo operando é ignorado. alert( null && 5 ); // null -alert( 0 && "no matter what" ); // 0 +alert( 0 && "não importa o quê" ); // 0 ``` -We can also pass several values in a row. See how the first falsy one is returned: +Nós podemos também passar vários valores seguidos. Veja como o primeiro falso é retornado: ```js run alert( 1 && 2 && null && 3 ); // null ``` -When all values are truthy, the last value is returned: +Quando todos valores são falsos, o último valor é retornado: ```js run -alert( 1 && 2 && 3 ); // 3, the last one +alert( 1 && 2 && 3 ); // 3, que é o último ``` -````smart header="Precedence of AND `&&` is higher than OR `||`" -The precedence of AND `&&` operator is higher than OR `||`. +````smart header="Precedência do E `&&` é maior do que do OU `||`" +A precedência do operador E `&&` é maior do que do OU `||`. -So the code `a && b || c && d` is essentially the same as if the `&&` expressions were in parentheses: `(a && b) || (c && d)`. +Portanto, o código `a && b || c && d` é essencialmente o mesmo como se as expressões `&&` estivessem entre parênteses: `(a && b) || (c && d)`. ```` -Just like OR, the AND `&&` operator can sometimes replace `if`. +Assim como OU, o operador E `&&` às vezes pode substituir um `if`. -For instance: +Por exemplo: ```js run let x = 1; -(x > 0) && alert( 'Greater than zero!' ); +(x > 0) && alert( 'Maior que zero!' ); ``` -The action in the right part of `&&` would execute only if the evaluation reaches it. That is, only if `(x > 0)` is true. +A ação na parte direita do `&&` executaria somente se a avaliação chegasse até ela. Ou seja, apenas se `(x > 0)` for verdadeiro. -So we basically have an analogue for: +Então, basicamente temos uma analogia para: ```js run let x = 1; if (x > 0) { - alert( 'Greater than zero!' ); + alert( 'Maior que zero!' ); } ``` -The variant with `&&` appears shorter. But `if` is more obvious and tends to be a little bit more readable. +A variação com `&&` parece mais curta. Mas `if` é mais óbvio e tende a ser um pouco mais legível. -So we recommend using every construct for its purpose: use `if` if we want if and use `&&` if we want AND. +Então recomendamos a utilização de cada CONSTRUCT para seu propósito: use `if` se queremos SE e use `&&` se queremos E. -## ! (NOT) +## ! (NÃO) -The boolean NOT operator is represented with an exclamation sign `!`. +O operador booleano NÃO é representado com um sinal de exclamação `!`. -The syntax is pretty simple: +Sua sintaxe é bem simples: ```js result = !value; ``` -The operator accepts a single argument and does the following: +O operador aceita um único argumento e faz o seguinte: -1. Converts the operand to boolean type: `true/false`. -2. Returns the inverse value. +1. Converte o operando para um tipo boolean: `true/false`. +2. Retorna o seu valor inverso. -For instance: +Por exemplo: ```js run alert( !true ); // false -alert( !0 ); // true +alert( !0 ); // true ``` -A double NOT `!!` is sometimes used for converting a value to boolean type: +Uma repetição do NÃO `!!` às vezes é usado para converter um valor para o tipo boolean: ```js run -alert( !!"non-empty string" ); // true -alert( !!null ); // false +alert( !!"string não vazia" ); // true +alert( !!null ); // false ``` -That is, the first NOT converts the value to boolean and returns the inverse, and the second NOT inverses it again. In the end, we have a plain value-to-boolean conversion. +Ou seja, o primeiro NÃO converte o valor para boolean e retorna o seu inverso e o segundo NÃO o inverte de novo. No final, nós temos uma conversão do valor para boolean. -There's a little more verbose way to do the same thing -- a built-in `Boolean` function: +Existem outras formas mais extensas de se fazer a mesma coisa -- uma função `Boolean`: ```js run -alert( Boolean("non-empty string") ); // true +alert( Boolean("string não vazia") ); // true alert( Boolean(null) ); // false ``` -The precedence of NOT `!` is the highest of all logical operators, so it always executes first, before `&&` or `||`. +A precedência do NÃO `!` é a mais alta entre todos os operadores lógicos, então ele é executado primeiro, antes que `&&` ou `||`.