JS exercícios com condicionais, conditional exercises.

...

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div>
        <h1>Test Logics</h1>
        <h2>Test1 conditionals</h2>
            <p id="p1"><?php echo "test1"; ?></p>
            
            <input type="text" id="field1"> 
            <br>
            <input type="text" id="loginStatusField" placeholder="Login Status"> 
            <br>
            <input type="text" id="rankField" placeholder="Rank Status">
            <br>
            <input type="text" id="pointsField" placeholder="Points total">
            <br>

            <input type="submit" id="click" onclick= "testIfs()" value="teste">

            <p>Resultado:</p>
            <p id="resultado">teste resultado</p>
            <p id="logged">teste logged</p>
            <p id="rank">teste rank</p>
            <p id="points">teste pontuação</p>
            
            <script>
                // 
/*                 var isLogged = document.getElementById('loginStatusField').value;
                    console.log('field data: ' + isLogged);
                    var rank = document.getElementById('rankField').value;
                    var points = document.getElementById('pointsField').value; 
                    
                    These have to be set inside the function to refresh on every call!
                */
                
                function testFunction()
                {
                    document.getElementById('field1').value = "Test2";
                    document.getElementById('click').value = "Test2";

                }

                function testIfs()
                {   
                    var isLogged = document.getElementById('loginStatusField').value == 1;
                    console.log('field data: ' + isLogged);
                    var rank = document.getElementById('rankField').value;
                    var points = document.getElementById('pointsField').value;
                    var campo = document.getElementById('field1').value;
                    var compara = campo > 10;
                    
                    function resultado(arg)
                    {
                        document.getElementById('resultado').innerHTML = arg;

                        document.getElementById('resultado').style.display="block";
                        document.getElementById('resultado').style.fontSize = '35px'; 

                        console.log('true');
                    }
                    if (compara)
                    {

                        resultado('maior que 10, uhull');
                    }
                    else 
                    {
                        //document.getElementById('resultado').innerHTML = 'é menor que 10';
                        console.log('Menor que 10');
                        document.getElementById('resultado').style.display = 'none';

                    }

                    if (isLogged) // validates if is 1. if used 'if(islooged)' it validates when not empty... Set the var as var = field == 1 to validate;
                    {
                        console.log('is logged');
                        document.getElementById('logged').innerHTML = 'You are logged';
                        if (rank == 1)
                        {
                            document.getElementById('rank').innerHTML = 'Your rank is ' + rank + ' and ';
                            if( points > 1000)
                            {
                                document.getElementById('points').innerHTML = ' and your points are ' + points + ' right now.';
                            } 
                            else 
                            {
                                document.getElementById('points').innerHTML = 'you dont have enough points.';
                            }
                        }
                        else
                        {
                            document.getElementById('rank').innerHTML = 'But you are not rank 1';
                            document.getElementById('points').innerHTML = '';
                        }
                    }
                    else
                    {
                        console.log('is not logged');
                        document.getElementById('logged').innerHTML = 'is not logged';
                        document.getElementById('rank').innerHTML = '';
                        document.getElementById('points').innerHTML = '';
                    }
                }
            </script>

    </div>
</body>
</html>

...

Leave a Reply

Your email address will not be published. Required fields are marked *