I'm trying to create a simple program where a user needs to pick a maximum number, then try to guess a number between 1 and that maximum number. When they enter their guess, I need to validate the input, and offer a choice of feedback: different messages if it's (1) not a number, (2) a number not within the range, (3) the correct guess, (4) too high, but within range, and (5) too low, but within range.
function do_guess(prompt) {
let valid_input = false;
let guess = Number(document.getElementById("guess").value);
let message = document.getElementById("message");
while(!valid_input) {
input = window.prompt(prompt);
guess = Number(input);
if (guess == NaN) {
message.innerHTML = "That is not a number!";
}
else if (guess < 1 || guess > val) { \\val = max_num - I wasn't sure how to code this.
message.innerHTML = "That number is not in the range, try again.";
}
else if (guess == num) {
valid_input = true;
message.innerHTML = "You got it!";
}
else if (guess > num) {
message.innerHTML = "No, try a lower number.";
}
else {
message.innerHTML = "No, try a higher number.";
}
}
}
Aucun commentaire:
Enregistrer un commentaire