-5

So I am calculating values, snapshotting them and send them into a interval. But the issue is, is its wildly increasing what the sum should be.

Here is the calculations:

// Set up mob attributes - SNAPSHOT
const mobId = '1';
var mob = {
  mhp: (mobId + 10) * 15,
  hp: (mobId + 10) * 15,
  dmg: (mobId + 2) * 3,
  defenseRating: (mobId + 3) * 4,
  hitRating: (mobId + 2) * 2,
  critRating: (mobId + 3) * 2,
  critDmg: (mobId + 2) * 2,
  dodgeRating: (mobId + 1) * 3,
  firstHitRating: (mobId + 3) * 4,
};
console.log(mob);

I don't know about you, but in what world does 11 * 15 = 1650. This is the calculations BEFORE I send it through the interval, so it isn't messing up there. It is the intial calculations that being very weird.

Note: It may be worth noting that I am doing this through a socket event: socket.on('fight mob', (mobId) => { I doubt that is the issue but I am at a loss right now.

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
William
  • 1,175
  • 2
  • 17
  • 32
  • 5
    Looks like `mobId` is a string. Cast it to a number first, else `+` will concatenate – CertainPerformance Jul 12 '18 at 08:10
  • 1
    Before using the + operator, which concatenates strings, make sure mobId is an integer, you can use mobId = parseInt(mobId, 10); Without that casting, mhp is 110 * 15 (110 is the result of concatenating 1 and 10), and 110*15 = 1650. – Koas Jul 12 '18 at 08:12
  • I don't know about you but for me string addition means concatenation not mathematical addition. If `mobId = '1'` then `mobId + 10 === '110'`. – axiac Jul 12 '18 at 08:14
  • Slow down william, you are obviously full of anger as you are writing this, try to debug the code before you post it here. – Dellirium Jul 12 '18 at 08:15
  • Uhm, not angry, was just confused, stop assuming things. – William Jul 12 '18 at 08:17
  • 1
    Take a look at the [documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Addition). It says in the example: *"Number + String -> concatenation"* – axiac Jul 12 '18 at 08:18

2 Answers2

1

This is happening because your mobId variable is a string, not a number.

(1 + 10) * 15 = 165

("1" + 10) * 15 = 1650

To resolve this, you can coerce mobId's type using Number():

(Number(mobId) + 10) * 15 = 165

Good luck!

Pancake
  • 739
  • 1
  • 5
  • 20
  • Question: whats the difference between: `parseInt(mobId)` and `Number(mobId)` because they seem to do the exact same thing. – William Jul 12 '18 at 08:16
  • "Number(object) tries to convert the entire string to a number(which can also be a float) while parseInt(object) parses up to first non-digit and returns whatever value has been parsed. Also, the complete format of parseInt() is parseInt(object, radix) where radix defines the base of the integer to be parsed into." – Pancake Jul 12 '18 at 08:18
  • This question is to be addresed here: https://stackoverflow.com/questions/4564158/what-is-the-difference-between-parseintstring-and-numberstring-in-javascript. Mate you need to google – Dellirium Jul 12 '18 at 08:18
1

Due to loose typing, JavaScript automatically tries to change the types of variables when performing operations on them. In your case, mobId is a string and not a number. So, instead of addition, JavaScript performs concatenation.

So the operation

(mobId + 10) * 15

becomes something like this for mobId being '1':

'110' * 15

Again, JavaScript's type caster kicks in, but this time it converts the string to number for multiplication, so the result becomes 1650.

To mitigate, convert the mobId to number first. Use parseInt or parseFloat for that:

mobId = parseInt(mobId, 10);
(mobId + 10) * 15 // Yields 165
31piy
  • 23,323
  • 6
  • 47
  • 67