Trying to return a object property value to be updated(I think )

Just creating a tiny little game for practice. Basically its mario vs bowser. I can get them to hit each-other, but I am unable to save the loss of health that each one takes. This is the code so far…


function player(atk, vit) {
    this.atk = atk;
    this.vit = vit;
}

function enemy(atk, vit) {
    this.atk = atk;
    this.vit = vit;
}

function playerAtk(max, min) {
    return Math.floor(Math.random() * (max -min + 1) + min);
}

var mario = new player(playerAtk(1,100) , 1000);
var bowser = new player(playerAtk(1,100), 1000);

if(mario.atk <= bowser.atk){
    document.write("Bowser Hits Mario for <br/> " + bowser.atk + " <br/>");
     mario.vit = mario.vit - bowser.atk; 
     
} else {
    document.write("Mario Hits bowser for <br/>" + mario.atk + "<br/>");
    bowser.vit = bowser.vit - mario.atk;
    }
    
document.write("Marios health is <br/>" + mario.vit);
document.write("Bowsers health is <br/>" + bowser.vit + "<br/> <br/>");


I want marios.vit to return a different number after he is hit and save the value, this way I can get them to fight until one does and do a if/else to pop up alert that one has been defeated! Been looking around but I can’t seem to find a solution.

Thanks for replies and possible yelling I shall receive :wink:

Hi dave,

I'm presuming you're just starting and really don't know much javascript & html.  If I'm wrong, I'm sorry.  

If I’m right, though, study the code below and if you don’t understand something, ask away. (This is not the best way to do it, but it is a start)


<html>
<body>
<div id='myDiv'></div>
<input type='button' value='Start Game' onclick='game()' />
<script>
function player(atk, vit) {
    this.atk = atk;
    this.vit = vit;
}
function enemy(atk, vit) {
    this.atk = atk;
    this.vit = vit;
}
function playerAtk(max, min) {
    return Math.floor(Math.random() * (max -min + 1) + min);
}
function write2Div(str){
 document.getElementById('myDiv').innerHTML += str;
}
var mario = new player(playerAtk(1,100) , 100);
var bowser = new player(playerAtk(1,100), 100);
function game() {
 do { 
  if(mario.atk <= bowser.atk){
   write2Div("Bowser Hits Mario for <br/> " + bowser.atk + " <br/>");
   mario.vit -=  bowser.atk; 
  } else {
   write2Div("Mario Hits bowser for <br/>" + mario.atk + "<br/>");
   bowser.vit -= mario.atk;
  }
  write2Div("Marios health is <br/>" + mario.vit);
  write2Div("Bowsers health is <br/>" + bowser.vit + "<br/> <br/>");
 } while (mario.vit > 0 && bowser.vit > 0)
}
//I want marios.vit to return a different number after he is hit and save the value, this way 
</script>
</body>
</html>

I’m presuming you’re just starting and really don’t know much javascript & html. If I’m wrong, I’m sorry.

Yup I am fairly new, I am okie at html, but javascript is new, and I have only been at it for a little bit, still haven’t finished a full book yet.

Thank you for reply , I will search into this script and study it more thoroughly. This is my first attempt at really doing anything besides following along with the books.

We all start somewhere :slight_smile: When you’re ready, post your questions, and either I or someone else will answer them