//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;
}
}