1
resposta

Resolução final da aula 07

<meta charset="UTF-8">

<script>

    function pularLinha ( ) {
        document.write ( "<br>" );
    }

    function msg ( texto, type ) {
        if ( type == "alert" ) {
            alert ( texto );
        } else {
            document.write ( texto );
            pularLinha ( );
        }
    }

    function parabens ( ) {
        for ( i = 0; i <= 10; i++ ) {
            for ( i1 = 0; i1 <= 30; i1++ ) {
                document.write ( "*" );
            }
            pularLinha ( );
        }
    }

    var numeroAleatorio = Math.round ( Math.random ( ) * 10 );
    console.log ( numeroAleatorio );

    var tentativas = 1;
    var maxTentativas = 3;

    var chute = parseInt ( prompt ( "Informe seu chute de 0 a 10!" ) );

    while ( tentativas <= maxTentativas ) {

        if ( chute == numeroAleatorio ) {
            msg ( "* Parabens, você acertou!", "alert" );
            parabens ( );
            break;
        } else {
            tentativas++;
            if ( tentativas > maxTentativas ) {
                msg ( "* Suas chances acabaram, o resultado era " + numeroAleatorio + "!", "alert" );
            } else {
                msg ( "* Você errou, tente novamente!", "alert" );
                chute = parseInt ( prompt ( "Informe seu chute de 0 a 10!" ) );
            }
        }

    }

</script>
1 resposta

Olá, Jeferson! Tudo bem?

Excelente solução. Parabéns :)

Qualquer dúvida compartilhe com a gente aqui no fórum!

Continue praticando e ótimos estudos.

Abraços!

Quer mergulhar em tecnologia e aprendizagem?

Receba a newsletter que o nosso CEO escreve pessoalmente, com insights do mercado de trabalho, ciência e desenvolvimento de software