Search code examples
javascriptfunctionmathsubtraction

JavaScript Math inside function not working


The problem I am having is it only returns 10 nothing more or less. Is there is also a better way to do this please share as I am learning and from most of the YouTube videos I have seen they do math outside of a function or math inside the function but only has 1 argument to the string.

var player = {hp:10, at:1};
var enemy ={hp:10, at:1};
function hit() {
    function minus() {
        
        return player.at - enemy.hp;
       }
           console.log(enemy.hp);
           return ;
}
<button onclick="hit()">Hit</button>


Solution

  • I think this may be what you intended to do.

       
    var player = {
      hp: 10,
      at: 1
    };
    var enemy = {
      hp: 10,
      at: 1
    };
    
    function hit() {
      enemy.hp = enemy.hp - player.at;
      console.log(enemy.hp);
    }
    <button onclick="hit()">Hit</button>