So I am developing a game using only alerts and prompts. I have been having trouble with getting the game to stop if there is no text in the prompt. Can someone help please? Here is my code.
var food = 20;
var happy = 20;
var sleep = 20;
function setup(){
var input1 = prompt("Welcome to pet sim\r\nYour pet\'s food stat is " food ", happy stat is " happy ", and sleep stat is " sleep ".\r\nWhat would you like to do?\r\n\"food\" \"play\" \"sleep\"");
if(input1="food"){
food ;
alert("Food eaten");
setup();
}else if(input1=null){
void(0);
}
}
setup();
CodePudding user response:
While Comparing Items In Javascript you need to use ==
or ===
not =
as it is used for assignment. So in your code you are basically making value of input1 as food
in the if statement. Also you can add cases for checking if its undefined
or
in JS using OR operator.
Below is the updated code:
var food = 20;
var happy = 20;
var sleep = 20;
function setup(){
var input1 = prompt("Welcome to pet sim\r\nYour pet\'s food stat is " food ", happy stat is " happy ", and sleep stat is " sleep ".\r\nWhat would you like to do?\r\n\"food\" \"play\" \"sleep\"");
if(input1=="food"){
food ;
alert("Food eaten");
setup();
}else if(input1==null || input1 == undefined || input1 == ""){
void(0);
}
}
setup();