Doubts with conditional structure

Asked

Viewed 209 times

0

I have a problem in my conditional structure, where is being displayed all the results of if.

What are my difficulties:

  • 1) Why this is occurring?

  • 2) What to do to resolve?

Just follow my code:

var valor1 =Number(prompt("Digite hum numero: "))
var valor2 =Number(prompt("Digite hum numero: "))
var valor3 =Number(prompt("Digite hum numero: "))
if(valor1 >= valor2 || valor1 >= valor3){
   alert("O valor maior é "+valor1)

}
if(valor2 >= valor1 || valor2 >= valor3){
   alert("O valor maior é "+valor2)

}
if(valor3 >= valor1 || valor3 >= valor2){
   alert("O valor maior é "+valor3)

}

  • My answer became adequate and easy to understand for you?

  • you are messing with short circuit operators, one of the parts that is evaluated, the other will be ignored and results in TRUE in the case of OR. And I didn’t quite understand the code proposal.

  • You want ONLY to know which number is bigger or which is bigger?

4 answers

4

Use the && or || You’ll never get a proper outcome on your probation. You can also use this:

Math.max(2,5,8,10,14)

That will return the highest value reported.

Test on your console in the browser.

Prompt:

var valor1 =Number(prompt("Digite hum numero: "))
var valor2 =Number(prompt("Digite hum numero: "))
var valor3 =Number(prompt("Digite hum numero: "))

Math.max(valor1,valor2,valor3)]

We typed 10, 20 and 5.

Return: 20

  • My thanks so much, I’m learning JS in college, thanks for the help!

  • My dear... would look like this: var valor1 =1 var valor2 =2 var Valor3 =3 Math.max(value1,value2,Valor3) Result: 3

  • @lucas_botina Welcome to Sopt. Then take a look at tour. One of the best practices of the site is to upvote the answers that helped you in some way (even if you weren’t the author of the question) and accept as the correct answer the answer that best suits your question. :)

2

Basically, its logic allows the execution of all if for the check and that only a condition of greater or equal execute the content of the condition. Let’s assume that the user enters with the numbers valor1 = 4, valor2 = 2 and valor3 = 6. So we have:

if(valor1 >= valor2 || valor1 >= valor3) we have that 4 is greater than 2, but that 4 is not greater than 6. Even so, the condition is met, because of the || (or) that you have put in condition, allowing such acceptance and showing such warning.

if(valor2 >= valor1 || valor2 >= valor3) we have that 2 is not greater than 4 and that 2 is not greater than 6. This condition is ignored.

if(valor3 >= valor1 || valor3 >= valor2) we have that 6 is greater than 2 and 6 is greater than 4, therefore this being the true condition of the code(6 is the largest number), and also shows the alert, thus executing two of its conditions.

So, the right thing would be for you to do this check with the logical operator && (e), which allows these conditions to be accepted not only if one is true, but only if the number is actually the highest. Here is the correction:

<script type="text/javascript">
var valor1 =Number(prompt("Digite um numero: "))
var valor2 =Number(prompt("Digite um numero: "))
var valor3 =Number(prompt("Digite um numero: "))
if(valor1 >= valor2 && valor1 >= valor3){
    alert("O valor maior é "+valor1)

}
if(valor2 >= valor1 && valor2 >= valor3){
    alert("O valor maior é "+valor2)

}
if (valor3 >= valor1 && valor3 >= valor2){
    alert("O valor maior é "+valor3)

}
</script>
  • Good. But it would be interesting to use the else since if the highest value is found, no longer need to pass by others ifs :)

  • True. I tried to make it as simple as possible and forgot I could improve the code

2


You need to study more on logical reasoning, and then practice on algorithms...

I will approach 2 matters for you to understand better, which are:

  • 1) Truth Table

  • 2) Logical Operators

What is Truth-Table?

Truth table, truth table or veritative table is a type of mathematical table used in logic to determine whether a formula is valid or whether a sequent is correct.

What are Logical Operators?

Logical operator, as well as an arithmetic operator, is an operation class on predefined variables or elements. AND, NAND, OR, XOR and NOT are the main logical operators, basis for the construction of digital systems and propositional logic, and also widely used in programming language. Operators AND, NAND, OR and XOR are binary operators, that is, they need two elements, while the NOT is unary. In computation, these elements are usually binary variables, whose possible assigned values are 0 or 1. However, the logic used for these variables also serves for sentences (sentences) of human language, where if this is truth corresponds to the value 1, and if it is false corresponds to the value 0.

Now, let’s practice, all right? I will approach giving simple examples and the main logical gates with their respective truth-table for you to better understand their logic.

Illustration of the main logical gates and their respective truth table: inserir a descrição da imagem aqui

Porta Lógica NOT:

This door she is a door of denial, ie if you notice in her truth table, when the entrance A receives 0(false-off) it turns the output into 1(true-connected) and when she receives 1 it turns the output into 0. Algorithm example:

if(!isLarge){
} else {}

What I mean by this code is the following: If it is different from great enter this if, else get in the else.

Porta Lógica AND:

This door checks the condition of the two entrances, to enter the given condition, the two entries must be true, so much that you can analyze in its truth table, she only has the output 1 (truth) when the two entries have the values 1 and 1. Algorithm example:

if(isLarge && tamanho == 50.00){
} else{}

What I mean by this code is this: If it is great and the size is equal to 50 cm enter the if, else get in the else.

Porta Lógica OR:

This door checks the condition of the two entrances, but it is different from the AND, for to be true at least one of the entries must have the value 1, you can notice in your table that it is only true when one of the entries has the value 1 or both have the value 1. Algorithm example:

if(isLarge || tamanho == 50.00){
} else{}

What I mean by this code is this: If it is great and the if the size is equal to 50 cm enter the if, else get in the else. Regardless if it is not great and the size is equal to 50 cm if it is great and the size is different from 50 cm she entered the if, if it’s not great and the size is not equal to 50 cm she entered the else.

0

Besides the problem of using the operator || (or) instead of && (and), where the && means that both conditions must be true, in this case it would be better to use a if else and a else (if you don’t want to use Math.max() and maintain the structure of if's), as it is not necessary to go through each if if you want only 1 set of conditions to be satisfied.

For example, in the first if you already "kill" the question by checking whether the valor1 is greater than the other two values:

if(valor1 >= valor2 && valor1 >= valor3){
  alert("O valor maior é "+valor1)
}else if(valor2 >= valor3){
  alert("O valor maior é "+valor2)
}else{
  alert("O valor maior é "+valor3)
}

If valor1 is greater than or equal to valor2 and valor3 the check will ignore the two other blocks (else if and else) already closing the check in the first conditions.

If valor1 is less than valor2 and greater than or equal to valor3, will not satisfy the first if moving on to else if, therefore, it is already ruled out that valor1 is the largest number, so if valor2 is greater than or equal to valor3, the verification ends there, being valor2 the greater number if it is greater than valor3 or if it equals valor3, both are the higher values. Now, if valor2 is less than valor3, then it will fall into the else because neither of you if'were met, and with that valor3 is the largest number or valor1 and valor3 are equal and are the largest numbers.

Table tests:

valor1 = 1
valor2 = 2
valor3 = 3

     if: condição 1 = falso, porque valor1 não é igual nem maior que valor2
                      (nem precisa verificar a segunda condição)
else if: falso, porque 2 não é maior nem igual a valor3, logo, obrigatoriamente
         irá cair no else

> resultado: número maior = 3

------------------------------

valor1 = 3
valor2 = 2
valor3 = 1

     if: condição 1 = verdadeiro, porque valor1 é maior ou igual que valor2
         condição 2 = verdadeiro, porque valor1 é maior ou igual que valor3
else if: ignorado
   else: ignorado

> resultado: número maior = 3

------------------------------

valor1 = 1
valor2 = 2
valor3 = 1

     if: condição 1 = falso, porque valor1 é menor que valor2
                      (nem precisa verificar a segunda condição)
else if: verdadeiro, porque valor2 é maior que valor3
   else: ignorado

> resultado: número maior = 2

------------------------------

valor1 = 1
valor2 = 1
valor3 = 1

     if: condição 1 = verdadeiro, porque valor1 é igual a valor2
         condição 2 = verdadeiro, porque valor1 é igual a valor3
else if: ignorado
   else: ignorado

> resultado: número maior = 1

------------------------------

valor1 = 1
valor2 = 2
valor3 = 2

     if: condição 1 = falso, porque valor1 é menor que valor2
         (nem precisa verificar a segunda condição)
else if: verdadeiro, porque valor2 é igual a valor3
   else: ignorado

> resultado: número maior = 2

Testing:

var valor1 =Number(prompt("Digite hum numero: "))
var valor2 =Number(prompt("Digite hum numero: "))
var valor3 =Number(prompt("Digite hum numero: "))

if(valor1 >= valor2 && valor1 >= valor3){
  alert("O valor maior é "+valor1)
}else if(valor2 >= valor3){
  alert("O valor maior é "+valor2)
}else{
  alert("O valor maior é "+valor3)
}

Browser other questions tagged

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