Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Ehs 2.0 Revolutionizing The Future Of Safety With Digital Technology

Authors: Tony Mudd

1st Edition

B0CN69B3HW, 979-8867463663

More Books

Students also viewed these Databases questions

Question

#NAME

Answered: 1 week ago

Question

Explain the strength of acid and alkali solutions with examples

Answered: 1 week ago

Question

Introduce and define metals and nonmetals and explain with examples

Answered: 1 week ago