- Code: Select all
{
if(“player's health >=1”)
while(“monster's health is <=0”)
alert(“Congratulations! You win! ^-^");
}
{ else if(“player's health <=0”)
while(“monster's health >=1")
alert(“You lose. :p”);
}
};
This doesn't look right at all - supposing I have an object player and an object monster, however,
- Code: Select all
if( (player.health > 0) && (monster.health > 0) ){
//TODO: Add code for continuing battle
}
else if( (player.health > 0) && (monster.health <= 0) ){
alert('Congratulations! You win! ^-^');
}
else{
//This conditional fires if the player's health is below zero, independent of what the monster's health is
alert('You lose. :p');
}
Here's where your code went wrong:
1. “player's health >=1” isn't valid code. It's like asking the computer 'is the phrase "hello world" true, or false? ' It's neither, it's a string, not a Boolean variable. Instead of this, I recommend creating objects for both the monster and the player, which is really easy in javascript. Just say:
- Code: Select all
player = {};
player.health = 5;
player.health -= 2;
monster = {};
monster.health = 7;
monster.health -= 3;
Now, of course, it's important to remember. Javascript is an oddball, it's not an object oriented language. Because of this, you won't find your typical class declarations and what not. You can force it, but in truth, it's just too darn easy to tack on a variable name with a dot and set it equal to something (including a function as it turns out). Initialize these in one spot, though or your debugging will be a mess. You can also tack on far more than health. What about atk, or def, or a string variable for a name... or
gender (Super effective for helping Professor Oak, who has a horrible time determining the gender of others, even if he lived next to them their whole life).
2. Your logic is off, while in English,
- Code: Select all
if(“player's health >=1”)
while(“monster's health is <=0”)
this is understandable, to a computer, it makes no sense at all. It will instead, happily throw a syntax error. Instead, what you're looking for are two conditionals. One that checks that the player's health is above 0 and another to check that the monsters health isn't below zero. That is,
- Code: Select all
//If the player's health is greater than zero and the monster's health is less than or equal to zero
//or in code...
if( (player.health > 0) && (monster.health <= 0) ){
//Do stuff here if the stuff in the parenthesis above returns true
}
Here, I've included two conditionals,
- Code: Select all
player.health > 0
and
- Code: Select all
monster.health <= 0
with a && between them. The && means that both conditions must be true in order for the conditional to run the code between the brackets { these thingys }. It's the logical equivalent of saying 'and'. || on the other hand means 'or', so that the conditional will run if either condition returns true. The parenthesis are there to simply make sure that each condition is separate from the other one. If you're wondering why we don't just say 'and' and 'or' in the program, well... Python wonders this, too. CoffeeScript, however, fixes that issue - but is only suitable for more advanced users who already understand JavaScript.
3. The other major problem I see is that you're not properly framing your code within your if and while statements. Remember...
- Code: Select all
while(true){
//Stuff you want to complete in the while loop should go here.
}
- Code: Select all
if(true){
//Stuff you want to complete in the if statement go here.
}
And if you want one conditional within another...
- Code: Select all
if(test_1){
if(test_2){
alert('hooray!');
}
else if(test_3){
alert('Yay!');
}
}
Finally, while it isn't likely going to hurt anything, don't include a semicolon after your bracket enclosures (e.g. { } these guys). It just looks bad. I got this working for me when I tried out various numbers, see if that does something similiar to what you want.
- Code: Select all
player = {};
player.health = 0;
monster = {};
monster.health = 1;
if((player.health > 0) && (monster.health > 0)){
alert('let the battle continue!');
}
else if((player.health > 0) && (monster.health <= 0)){
alert('Congratulations! You win!');
}
else{
alert('You lose :p');
}