Final answer:
To create a guessing game with a while loop in programming, define a variable for the guess count, prompt for a guess, use a while loop to check the guess, and output the result once the correct guess is made.
Step-by-step explanation:
To create a program that prompts the user to guess your favorite color and uses a while loop to check their guess, you would do the following:
First, you'll need to create a variable to keep track of the number of guesses. You can assign it the value of 1 since the user hasn't made any guesses yet.
let guessCount = 1;Next, prompt the user to guess your favorite color. In this scenario, let's say your favorite color is blue.
let userGuess = prompt("Guess my favorite color:");Now set up a while loop that runs as long as the user's guess isn't correct. Inside the loop, inform them that they are incorrect, prompt them to guess again, and increment the guess count variable.
while (userGuess !== 'blue') {
alert("Incorrect, try again.");
userGuess = prompt("Guess my favorite color:");
guessCount++;
}When the user finally guesses 'blue', the loop will exit. Provide feedback that they are correct and tell them how many guesses it took.
alert("Correct! It took you " + guessCount + " guesses.");Using this logic, the user gets immediate feedback on their guesses and knows exactly how many attempts they made before they succeeded.