While loops in JavaScript - example 2
by danielkwood
HTML
<html>
<head>
<title>While loops</title>
<script src="script.js"></script>
</head>
<body>
</body>
</html>
JavaScript
// Let's generate a random number between 1 and 10
var secretnumber = Math.floor(Math.random() * 10) + 1;
// Let's ask the user to guess the secret number
var attempt = prompt("Enter a number between 1 and 10: ");
// Keep asking the user to guess the number while they get it wrong
while(attempt != secretnumber){
attempt = prompt("Incorrect! Try again. ");
}
console.log("You got it!");