1
resposta

[Projeto] FINAL PROJECT - SECRET NUMBER GAME

Insira aqui a descrição dessa imagem para ajudar na acessibilidadeInsira aqui a descrição dessa imagem para ajudar na acessibilidade

//SECRET NUMBER GAME (JAVASCRIPT). BY: DANIEL FERREIRA CRESPO, beginner dev.
alert("Hi Folk!");
//numbMul is the variable responsable for the range of possible numbers in the game.
let numbMul = 10
//"guess" is the variable responsable for armazenating the info related to the user is guessing.
let guess;
//"question" is the variable responsable for keeping the loop activated and for the afirmative answer of the user."
let question=1;
// while the variable "question" is equal to 1, the code will keep asking the user.
while (question==1){
    // Code to generate a random number.
    let secretNumber = (parseInt(Math.random()*numbMul+1));
    console.log(`secret number is ${secretNumber}`);
    question = prompt("Do you wanna play the secret number game? yes(type 1) no(type 2)");
    //If the users answer 2, it means they don't want to play. After that, the variable "question" is redifined to "1" keeping the loop activated.
    if (question==2){
        alert("Okay, Bye!");
        question=1;
    //If the user answers "1", wich means (yes) the game starts.
    }else if (question == 1){
        //variable responsable for armazenating the user is guesses.
        let trys = 1;
            //While guess is not equal to secretNumber, the code will keep asking the user.
            while (guess != secretNumber){
                guess = prompt(`Try to guess the secret number! TIP: 1-${numbMul}`);
                //if guess == secretNumber, it means the player won the game.
                    if (guess == secretNumber){
                        break;
                // if not, it means the user must keep trying to guess.
                    } else{
                        if (guess < secretNumber)
                        alert(`Secret Number greater than ${guess}`);
                        if (guess > secretNumber)
                        alert(`Secret number is less than ${guess}`);
                    }
                        //trys = trys + 1; Used to atualize the variable "trys".
                        trys++;
    }
    //code used to format the victory phrase in case of the user gets the number right in the first time (singular)
    wordtime = trys > 1 ? "trys" : "try";
    alert(` Congrats!!! You got the secret number ${secretNumber} in ${trys} ${wordtime}!`)
    //the code below makes the game keep asking the user to answer the question only by typing "1" or "2", and don't allow the user to proceed, until they type what is asked.
}   else{
    alert("PLEASE TYPE 1 OR 2 !!!!");
    question=1;
}
}
1 resposta

Oi Daniel,

Excelente trabalho no seu projeto do jogo do número secreto! 👍

Seu código está bem estruturado e fácil de entender, com comentários claros que explicam cada parte do processo.

A lógica do jogo está implementada corretamente, com o loop while para continuar jogando até o usuário acertar o número, e as mensagens de dica para guiar o jogador.

Para saber mais: Documentação da função isNaN() - Aprenda mais sobre a função isNaN() e como ela pode ser usada para validar entradas numéricas.

Continue praticando e explorando novas funcionalidades! 💪