Execute statement only if 5 conditions are true. Javascript

3

What command should I use to execute a statement only if 5 conditions are true?

For example In Portuguese:

Se 1 +1 = 2 e 
   2 +2=  4 e 
   4 +4=  8 e 
   5 +5= 10 e
   6 +6= 12 então faz isso.

How do I do this in javascript?

    
asked by anonymous 02.06.2016 / 06:15

2 answers

6

Simple, you use if with or without { and } . It seems kind of obvious, but for those who are starting it is not so, but most commands in programming languages have a good translation into Portuguese and the best way to understand (aka. Decorate) a command is to know the real meaning of it; In the case of if in free translation means Se .

The same holds true for loops and other decision structures, not counting key words.

Answering your question, you can implement it like this:

if ((1 + 1) == 2 && (2 + 2) == 4 &&
    (4 + 4) == 8 && (5 + 5) == 10 &&
    (6 + 6) == 12) {
    // faça alguma coisa aqui
    alert('Este é um alerta');       
}
    
02.06.2016 / 06:50
6

The answer to your question is quite simple, but is it more convenient for you to understand the concepts first so you can solve this and other problems with conditionals in the future using JavaScript ok?

In JavaScript (link to a practical tutorial of the language and not the definition of it in itself), conditions are handled through a conditional block called if .

The if works as follows:

if (expressao) {
 // executa se a expressão for verdadeira
}

The if simply expects an expression which must necessarily return a true Boolean value ( true ) or false ( false ).

A practical example:

if ( (2+2) == 4 ) {
  console.log("2+2 é igual a 4");
}

What happened above was as follows, the expression (2+2) == 4 basically evaluates if (2 + 2) equals 4, if this is true, a message will be written to the browser console.

The == operator is used to check if the value on the left is equal to the value on the right, if it is, it returns true , else it returns false .

The if also has two complementary blocks (not required):

else : If the condition that if expects, returns a value that is not true, an else block (if implemented) will be executed.

if ( (3+2) == 4 ) {
  console.log("3 + 2 é igual a 4");
}
else {
  console.log("3 + 2 não é igual a 4");
}

else if : It should come soon after a if or else if (yes, it is possible to use multiple else if ), it will execute if the previous condition is not true.

Example:

if ( age <= 13 ) {
  console.log("É apenas uma criança");
}
else if (age >= 14 && age <= 18) {
  console.log("É um adolescente");
}
else {
  console.log("É apenas um adulto");
}

Perceive the && above, it is a logical conjunction operator that evaluates the expression to the left and right of it, if both have the same Boolean value, it returns true , otherwise it returns false .

With this operator, you can test various conditions as follows

var condition1 = (1+1) == 2 // condition1 vai ser true, porque o operador == vai dizer que (1+1) é igual a 2

var condition2 = 2 < 5 // o < compara se o número a esquerda é menor do que o da direita, se sim, retorna true, senão, false

if (condition1 && condition2) {
  console.log("as condições 1 e 2 são verdadeiras")
}
// a variável condition1 por sí só, será avaliada como booleana, logo não é necessário escrever condition1 == true se eu quiser saber se ela é verdadeira
else if (condition1) {
  console.log("a condição 1 é verdadeira")
}
else if (condition2) {
  console.log("a condição 2 é verdadeira")
}
else {
  console.log("as condições 1 e 2 são falsas")
}

Other ways to evaluate conditions in JavaScript are with% operator ?: which is normally used when you want to assign a value depending on a Boolean expression and switch that can replace if in cases where you have various decision-making options according to a certain value.

    
02.06.2016 / 07:01