{}
CHRISTMAS
OFFER
62
%OFF

Stop copy pasting code you don't actually understand

Build the coding confidence you need to become a developer companies will fight for

Stop copy pasting code you don't actually understand

Become a PRO
Become a PRO
CHRISTMAS
OFFER
62
%OFF

Stop copy pasting code you don't actually understand

Build the coding confidence you need to become a developer companies will fight for

Stop copy pasting code you don't actually understand

Become a PRO
Become a PRO
run-icon
main.js
// --- 1. The Setup (Mise en Place) --- // The computer chooses a secret temperature between 300 and 400. const optimalTemperature = Math.floor(Math.random() * 101) + 300; // Result is between 300 and 400 const numberOfAttempts = 7; console.log("You've discovered a secret ingredient!"); console.log("Let's find the perfect temperature to bake the cake."); console.log(`You have ${numberOfAttempts} attempts. Good luck!`); console.log("------------------------------------------"); // We'll track if the player won inside the loop let hasWon = false; // --- 2. The Baking Loop --- // This loop will run for the number of attempts we have. for (let attemptNumber = 1; attemptNumber <= numberOfAttempts; attemptNumber++) { // --- 3. Get the User's Guess --- // We ask for input and convert it to an integer (a whole number). let guess = parseInt(prompt(`Attempt #${attemptNumber}: Enter your guess (300-400):`)); // --- 4. The Logic (Check the Result) --- if (guess < optimalTemperature) { alert(`Your guess of ${guess}°F is too cold! The cake is gooey and undercooked. Try a higher temperature.`); } else if (guess > optimalTemperature) { alert(`Your guess of ${guess}°F is too hot! The cake is burnt to a crisp! Try a lower temperature.`); } else { // This code runs if the guess is exactly right. alert(`Perfection! ${guess}°F is the exact temperature. The cake is golden-brown and delicious. You've mastered the secret ingredient!`); hasWon = true; // Mark that the player has won break; // Exit the loop immediately since we won. } } // --- 5. The "Game Over" Condition --- // After the loop finishes, we check if the player won. // If they didn't win, it means they ran out of tries. if (!hasWon) { alert(`Oh no! You've run out of the secret ingredient.\nThe correct temperature was ${optimalTemperature} degrees. Better luck next time!`); }
Output