Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am trying to make a while loop that checks for a condition from user input. If the user inputs anything except for valid integers,

I am trying to make a while loop that checks for a condition from user input. If the user inputs anything except for valid integers, the while loop requires the user to reenter until a valid integer is input. I have tested this code, but the while loop isn't overwriting the maxNotNumber variable when reentered. Please tell me what I'm doing wrong.

let min = 1;

//minimum number

let score = 0;

let max = Math.ceil(prompt("Enter a maximum number 1 or greater for the guessing game."));//asks for user's max number input and rounds any decimals up

maxNotNumber = Number.isInteger(max);//checks for nonnumbers

console.log(maxNotNumber);

//initial user max number input

let random = Math.ceil(Math.random() * (max - min) + min);

console.log(random);

//randomly generated number between 1 and user max

while (maxNotNumber != true) {//Illegal number check

maxNotNumber = Number.isInteger(max);//checks for nonnumbers

max = prompt("That's not a number. Try again.");

console.log("This is from the while loop for maxNotNumber. " + max);

// while (max < 1) {//Less than 1 check

// max = prompt("Needs to be 1 or greater.");

// }

}//max number parameter check

let guess = parseInt(prompt("Now guess the random number."));//asks user to guess randomly generated number, converts string to integer if necessary

let isNumber = Number.isInteger(guess);//Checks that input from user is integer

while (guess > max) {

let score = score++;//increments score each wrong guess

guess = prompt("Your guess is too high. Try again.");

while (guess < max) {

score = score++;//increments score each wrong guess

prompt("Your guess is too low. Try again.");

console.log("This is the console.log for score. " + score)

}

}

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

Database Development For Dummies

Authors: Allen G. Taylor

1st Edition

978-0764507526

More Books

Students also viewed these Databases questions

Question

1. What causes musculoskeletal pain?

Answered: 1 week ago