The operator does not work || [closed]

-1

I'm new to learning JavaScript and I'm doing internship but I do not get this right:

var estatura = 1.70 var edad2 = 30

if( edad2 == 30 || estatura = 1.60){
    document. write("true");} else{
    document.write("false");}

I marked it as an error in the browser console, why?

    
asked by Oscar Abraham Carrillo 07.03.2018 в 19:41
source

4 answers

1

Look at this is the solution I'm missing a = and a close the variables with ;

var estatura = 1.70;
var edad2 = 30;

if( edad2 == 30 || estatura == 1.60){
document.write("true");
} else{ 
document.write("false");
} 
    
answered by 07.03.2018 в 19:44
1

Greetings, you need another sign of the same in the comparison you make in the if:

    var estatura = 1.70
    var edad2 = 30

if( edad2 === 30 || estatura === 1.60)
{
    console.log("true");
} else{
    console.log("false");    
}

Remember that if you only use an equal sign you are indicating the variable that will take that value while if you use double sign of equal you indicate that compare if you have that value and if you put triple sign equal to part you ask that check if the data type is the same.

It will return true because although the second comparison is false, the first one is true and the operator you are using verifies that with one of the two conditions that is true.

    
answered by 07.03.2018 в 19:46
0

Your error is in the second part of the conditional. You have to use double == to make it work:

if (edad2 == 30 || estatura == 1.60){ document.write("true")("true");} else { document.write("true")("false");}

Only one = is used for assignment.

On the other hand, if you are going to do a double assignment in the same line you have to separate them by ; :

> var estatura = 1.70; var edad2 = 30;
> estatura
1.7
> edad2
30

Or, failing that, use a , to separate the expressions:

> var estatura = 1.70, var edad2 = 30;
> estatura
1.7
> edad2
30

Both work.

If you are testing code in the browser console (the easiest way to practice it), then you can use multiline by pressing Shift + Enter , that way it looks more ordered and you can practice better without breaking your head reading everything in the same line:

> var estatura = 1.70; var edad2 = 30;
> if( edad2 == 30 || estatura == 1.60){ // Shift + Enter
    console.log("true");                // Shift + Enter
  } else {                              // Shift + Enter
    console.log("false");               // Shift + Enter
  }                                     // Enter
true
>
    
answered by 07.03.2018 в 19:44
0

I leave here my answer:)

     var estatura = 1.70; //Cuando es asignación solo se utiliza un "="
     var edad2 = 30;
     //También debes finalizar las variables con ";"
     if( edad2 == 30 || estatura == 1.60) //Cuando utilizas condicionales siempre es doble "=" 
     {
      document.write("true"); //Tenías un espacio después del document
     } 
     else
     {
     document.write("false");
     }

Greetings

    
answered by 08.03.2018 в 00:45