Run statement only if 5 conditions are true. Javascript

Asked

Viewed 122 times

3

Which command should I use to execute an instruction only if 5 conditions are true ?

For example In portugol :

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 ?

2 answers

6


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

The same is true for loops and other decision structures, not to mention keywords (Keywords).

To answer your question, you can implement it this way:

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');       
}

  • I did it that way and then I thought I was wrong, now I see that the mistake is another... Vlw man

6

The answer to your question is very simple, however it is more convenient you understand the concepts first so you can solve this and other problems with conditions in the future using Javascript ok?

In Javascript (link to a practical tutorial of the language and not its definition itself), conditions are manipulated through a conditional block called if.

The if works as follows:

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

The if simply expects a expression that must 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 the following, the expression (2+2) == 4, basically, evaluates if (2+2) is equal to 4, if this is true, a message will be written in the browser console.

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

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

else: Case the condition that the if wait, return 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: Must come right after one if or else if (yes, it is possible to use several else if), is executed 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");
}

Understand the && above, it is a logical conjunction operator that evaluates the expression to the left and to the right of it, if both have the same boolean value, it returns true, else he 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 tenary operator ?: which is normally used when you want to assign a value depending on a boolean expression and the switch which can replace the if in cases where you have several decision-making options according to a certain value.

  • Your intention was the best, but your answer did not answer the question, do you agree ? See the reply of @Uilque Messias, although he did not enter deeply into the theme, he replied and I do not know if I have done the same thought correct... But I thank you in the same way...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.