Subtract two variables in javascript
The subtraction assignment operator ( Specifications
Browser compatibilityBCD tables only load in the browser See alsoI'm working on a simple game and I've gotten as far as coding it so that when you click on an attack button it should generate a random number based on a base and strength then subtract that from the enemy's health but the subtracting part doesn't seem to work. It always outputs
It may have something to do with not specifically making sure that they're integers but I'm not sure how to do that.
Igor 32.3k14 gold badges76 silver badges111 bronze badges asked Nov 3, 2011 at 0:34
because you redefine the enemyHealth var inside hitEnemy function. Remove the var to fix it.
answered Nov 3, 2011 at 0:40
heisthedonheisthedon 3,6023 gold badges20 silver badges24 bronze badges Here is the most immediate problem. You need to remove
Additionally you should be calling
answered Nov 3, 2011 at 0:37
JaredParJaredPar 712k146 gold badges1216 silver badges1441 bronze badges 1 To add to Jared's response...
is shorthand for...
First, * which sounds nonsensical, I know answered Nov 3, 2011 at 2:03
davidchambersdavidchambers 23k16 gold badges74 silver badges101 bronze badges How do you subtract a variable in JavaScript?The subtraction assignment operator ( -= ) subtracts the value of the right operand from a variable and assigns the result to the variable.
What does =+ mean in JavaScript?The addition assignment operator ( += ) adds the value of the right operand to a variable and assigns the result to the variable. The types of the two operands determine the behavior of the addition assignment operator. Addition or concatenation is possible.
How do you subtract in node JS?subtract() method is used to subtract the two date objects.. Required Module: Install the module by npm or used it locally. ... . Syntax: const subtract(date1, date2). Parameters: This method takes the following arguments as a parameter:. Example 1: ... . Output: total days between them 2270.3951833333335.. Example 2:. Can you use += in JavaScript?What does += mean in JavaScript? The JavaScript += operator takes the values from the right of the operator and adds it to the variable on the left. This is a very concise method to add two values and assign the result to a variable hence it is called the addition assignment operator.
|