Error in Javascript ternary operator

2
// Write JavaScript here 
(function() {
    var contador = 0;
    document.getElementById('play').addEventListener("click", identificar);

    function identificar() {
        contador === 2 ? play.disabled = true : contador++ 
    }
});

<!DOCTYPE html>
<html>
    <head>
    <title>HTML5, CSS3 and JavaScript demo</title>
    </head>
    <body>
        <input id="play" type="button" value="Jugar"/>
    </body>
</html>

The error is in the funcion identificar , but in the Google console it appears:

  

Uncaught SyntaxError: Unexpected identifier

So, I do not know what the error is.

And second thing, how can more than one instruction be included if true comes out in the ternary operator?

    
asked by Eduardo Sebastian 23.05.2017 в 19:31
source

1 answer

3

You must define the method in this way when configuring a listener:

document.getElementById('play').addEventListener("click", identificar);

You must also have an element called play which calls the identify method when clicking.

Example:

var contador = 0;

function identificar() {
contador === 2 ? play.disabled = true : contador++;
alert("Se ejecuta metodo identificar");
}

document.getElementById('play').addEventListener("click", identificar);
 <html>
<head>
<title>HTML5, CSS3 and JavaScript demo</title>
</head>
<body>
    <input id="play" type="button" value="Jugar"/>
</body>
</html>

As for the ternaría operation, you can only obtain a value ( true or false ) depending on the defined expression, but you can execute other instructions separating them by comma and containing them in parentheses.

  

How you can include more than one statement if it is true on the   ternary operator?

Example:

contador === 2 ? (contador+=19, play.disabled = true, alert("valor true")) : contador++;
    
answered by 23.05.2017 / 19:46
source