Question
Hi I have posted this problem already and have already made the suggested changes but nothing my outcome is still the same. I am still
Hi I have posted this problem already and have already made the suggested changes but nothing my outcome is still the same. I am still stuck the the prompt loop.
HTML
Higher Lower
Guess a number between 1 and N.
Javascript
let maxRange;
while (true) {
maxRange = prompt("What should the highest Range be?");
if (isNaN(maxRange)) {
continue;
}
if (!Number.isInteger(maxRange)) {
maxRange = Math.round(maxRange);
}
if (maxRange <= 0) {
console.log("Enter a number higher than 0");
continue;
}
const maxNumberSpan = document.getElementsByTagName('span')[0];
maxNumberSpan.innerHTML = maxRange;
break;
}
let inputArray = [];
let num = Math.floor(Math.random() * maxRange) + 1;
document.querySelector('.btn').onclick = (e) => {
e.preventDefault();
do_guess();
};
console.log(num);
function do_guess() {
let guess = Number(document.getElementById("guess").value);
let message = document.getElementById("message");
if (isNaN(guess)) {
message.innerHTML = "That is not a number!";
return;
}
if (guess > maxRange || guess < 1) {
message.innerHTML = "That number is not in range, try again";
return;
}
if (inputArray.includes(guess)) {
message.innerHTML = "Number guessed already";
return;
}
inputArray.push(guess);
if (guess == num) {
message.innerHTML = "You got it! It took you " + inputArray.length + " tries and you guessed " + inputArray.join();
} else if (guess > num) {
message.innerHTML = "No, try a lower number.";
} else {
message.innerHTML = "No, try a higher number.";
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started