I ran the following code in Google Chrome Version 54.0.2840.100 (64-bit). It displays an alert dialog successively asking for input until the user enters the correct answer, but the problem is that when i click the close button or cancel button in the alert dialog instead of closing, it continues asking for input.Also the close button on the tab is unclickable.Nevertheless I can close chrome main window but is there a code to correct this.
var answer = Number(Math.floor(Math.random()*10));
do{
var number = Number(prompt("Guess a number"));
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);
The easiest and straightforward answer would be to remove the do...while loop. 'coz that would keep the code running till you input the right answer.
So, you could check for the inputed number, if it is null break it, else convert it to your number and run the loop.
checking for null firsthand would make it easy to check for 0 as an input too
var answer = Number(Math.floor(Math.random()*10));
do{
var number = prompt("Guess a number");
if(number!=null){
number = Number(number);
} else {
break;
}
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);
var answer = Number(Math.floor(Math.random()*10));
do{
var number = Number(prompt("Guess a number"));
if(number === 0){ // this check is needed
break;
}
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);
on closing the prompt, null is returned
passing null to Number function will return 0
hence the check for 0 is added
you could also modify the code as follows to allow 0 as input from the user
var input= prompt("Guess a number");
if(input === null){
break;
}
var number = Number(input);
... rest of the code
If click cancel, the input value will be null, then Number(null) will be 0.Unless the guess number is 0, the alert dialog would not be closed.
You'd better check the input value is null before cast it.
Window.prompt()
I have added a line of code to check whether user has cancelled the prompt.
var answer = Number(Math.floor(Math.random()*10));
do{
var number = Number(prompt("Guess a number"));
// Add the below code to check for null and exit
if (number === null)break;
// END
if(number-answer >10){
alert("Too big!!");
}
else if(number-answer < 10 && number>answer ){
alert("It's bigger, but you are close!");
}
else if(answer-number < 10 && number<answer){
alert("It's smaller, but you are close!");
}
else if(answer-number > 10){
alert("Too small!!");
}
else if(number==answer){
alert("You WIN !!");
break;
}
}while(number!=answer);
Related
So as a practice, I made a guess game in JavaScript where you have to guess the randomly generated number between 1 and 10 in three tries. It worked fine, but when the three tries are completed (or the user guesses the number), it starts all over again. I want it to stop when the above given circumstances are met.
Here is the code:
function runGame() {
var isPlaying = true;
var num = Math.floor(Math.random() * 10);
var guess;
var tries = 3;
alert("You have 3 chances to guess a mindset between 1 and 10!");
while (tries >= 0) {
guess = prompt("Enter a guess:");
if (guess > num) {
alert("Too high!");
}
else if (guess < num) {
alert("Too low!");
}
else {
alert("Exactly! " + num + " it is! You've won!");
}
tries--;
}
if (tries == 0) {
isPlaying = false;
}
}
while (isPlaying = true) {
runGame();
}
A few things:
Put isPlaying variable global. Although you can remove it entirely as well. You already have a while loop condition that does the same thing.
Remove the equal sign when comparing your tries to zero. Otherwise it will run still when the tries reached zero.
Use a break statement when the user guessed the right answer, otherwise it will still run after guessing.
Other than those your code is fine. Here's the final code:
function runGame() {
var num = Math.floor(Math.random() * 10);
var guess;
var tries = 3;
alert("You have 3 chances to guess a mindset between 1 and 10!");
while (tries > 0) {
guess = prompt("Enter a guess:");
if (guess > num) {
alert("Too high!");
}
else if (guess < num) {
alert("Too low!");
}
else {
alert("Exactly! " + num + " it is! You've won!");
break;
}
tries--;
}
}
runGame();
= in JavaScript is used for assigning values to a variable. == in JavaScript is used for comparing two variables.
So change isPlaying = true to isPlaying == true and it will be fine.
while (tries >= 0) here you can use just while (tries > 0)
You can also declare these variables outside of the function but it's not necessary.
var isPlaying = true;
var tries = 3;
function runGame() {
var num = Math.floor(Math.random() * 10);
var guess;
alert("You have 3 chances to guess a mindset between 1 and 10!");
while (tries >= 0) {
guess = prompt("Enter a guess:");
if (guess > num) {
alert("Too high!");
}
else if (guess < num) {
alert("Too low!");
}
else {
alert("Exactly! " + num + " it is! You've won!");
}
tries--;
}
if (tries == 0) {
isPlaying = false;
}
}
while (isPlaying == true) {
runGame();
}
Remove the isPlaying and call runGame() directly, not in a while loop, You can break the execution if chances gets done and rest tries if the user wins
function runGame() {
var num = Math.floor(Math.random() * 10);
var guess;
var tries = 3;
alert("You have 3 chances to guess a mindset between 1 and 10!");
while (tries >= 0) {
if (tries == 0) {
alert("You have finished your chances");
break;
}
guess = prompt("Enter a guess:");
if (guess > num) {
alert("Too high!");
} else if (guess < num) {
alert("Too low!");
} else {
alert("Exactly! " + num + " it is! You've won!");
// reset tries back to three
tries = 3;
}
tries--;
}
}
runGame();
how to tell javascript that the user enter a string in the prompt box and work with it in an if statment ? If the user enters a string of letters I want it to alert "You did not enter a number", and if they entered a string if digits then continue with the logic.
var userGess = prompt("guess a number");
var secretNumber = 7;
if (Number(userGess) > secretNumber) {
alert("the number is to high")
} else if (Number(userGess) < secretNumber) {
alert("the number is to low")
} else if (Number(userGess) == secretNumber) {
alert("you are correct")
} else if (userGess == toString(userGess)) {
alert("you didnt type a number")
}
You can use isNaN(userGess) to check if a given string userGess is non-numeric.
However, that returns false if userGess is empty string, so you have to explicitly check it. So your final condition becomes
userGess === "" || isNaN(userGess)
var userGess = prompt("guess a number");
var secretNumber = 7;
if(userGess === "" || isNaN(userGess)) {
alert("You didn't enter a number")
} else if (Number(userGess) > secretNumber) {
alert("the number is to high")
} else if (Number(userGess) < secretNumber) {
alert("the number is to low")
} else if (Number(userGess) == secretNumber) {
alert("you are correct")
}
Here is my current code:
if (ageCheck < 18) {
alert("YOU ARE TOO YOUNG");
} else if (ageCheck => 18) {
alert("WELCOME");
} else {
alert("test");
}
ageCheck();
The problem is that when running this and I put in random words rather then give me "test" it gives me "Welcome". How do I make it so that when I enter in something other then a number it runs the else part and gives me "test"?
Why don't you add ane more check first, before comparing the number with 18?
if(isNaN(parseInt(ageCheck))) alert('not a number')
Change else if statement to,
} else if (ageCheck >= 18) {
greater than or equal to is represented by >=
Maybe the below function can help you
if (isNaN(ageCheck)) {
alert("test"); }
else if (ageCheck < 18) {
alert("YOU ARE TOO YOUNG");
} else if (ageCheck >= 18) {
alert("WELCOME");
}
isNaN will check, whether its a number or not first
I am writing this basic control structure for a lesson and I am getting some unexpected behavior.
var answer = prompt('what is your age');
if (answer >= 21) {
alert('good to go!');
}
else if (answer < 21) {
alert('sorry not old enough');
}
else if (answer != typeof Number) {
alert('please enter your age as a number');
}
else if (answer === null) {
alert('you did not answer!');
}
On the very last conditional, I would expect that if I left the prompt empty, it would execute the last alert. However, it just says 'not old enough'. Is it treating no input into the prompt as 0? How can fix this?
Thanks.
Prompt doesn't return null if the user hits OK, to test for emptiness, you need to check if the string is empty answer === ""
You need to move the last two checks to the top since "" < 21 is true:
var answer = prompt('what is your age');
if (answer === '') {
alert('you did not answer!');
} else if (isNaN(answer)) {
alert('please enter your age as a number');
} else if (answer >= 21) {
alert('good to go!');
} else if (answer < 21) {
alert('sorry not old enough');
}
I am getting value from a text field. I have one if and several else if statement.
The problem the last else if doesn't execute even if the condition is true.
If I change the last else if to if it executes and gives alert. When I change that back to else if the statement doesn't execute. The else if before that is fine as it's firing/executing on a particular condition.
function Valcheck()
{
var txtVal = document.getElementById("sometextField").value;
if(txtVal =="%")
{
alert("% is only allowed with other characters.");
return;
}
else if(txtVal.indexOf("%") != -1)
{
if((txtVal.indexOf('%')) != (txtVal.length-1))
{
alert(" % is only allowed at the end.");
return;
}
}
else if(txtVal.indexOf(",") != -1)
{
alert("Comma or comma separated values are not allowed.");
return;
}
else if(( txtVal.length >0) && (txtVal.indexOf("%") == -1))
{
alert("Please enter % at the end of the value.");
return;
}
else if( txtVal.length > 11 )
{
alert(" Value can't be greater than 11 characters.");
return;
}
}
Please help. Thanks
The problem is that if txtVal.length > 11, then either this is met:
else if(txtVal.indexOf("%") != -1)
or this is met:
else if(( txtVal.length >0) && (txtVal.indexOf("%") == -1))
So that it will never reach the else if( txtVal.length > 11 ). You need to change this:
else if(txtVal.indexOf("%") != -1)
{
if((txtVal.indexOf('%')) != (txtVal.length-1))
{
alert(" % is only allowed at the end.");
return;
}
}
to this:
else if(txtVal.indexOf("%") != -1 && txtVal.indexOf('%') != (txtVal.length-1))
{
alert(" % is only allowed at the end.");
return;
}
so that it doesn't "capture" the case where txtVal.indexOf('%') == (txtVal.length-1).
There isn't a limit to the number of if statements. The issue is that one of the previous if statements catches the case you're testing.
Go through each if statements for the case your testing and see if it's beging caught by a previous one.
This happens because your else if(txtVal.indexOf("%") != -1) is true (so, second if from the top), but condition for "if" inside it is not true (so it doesn't go to "return".
there is no way that the last 'else-if' be hit:
if the textVal has a '%' in it it will go to the second 'else-if'
and if it does not have '%' it will go to the one before the last one.
so the last if never be hit.
There is no limit to if else nesting. There is a logical barrier to getting to nested if else clauses. Look into using
switch(caseVariable){
case 1: document.write("caseVariable = " + 1);
break;
case 35: document.write("caseVariable = " + 35);
break;
default: break;
}