4

So I have like kind of a homemade economy thing and when I added a working command and when it works it picks a random number and stuff but when it adds the number to the old currency it's adding like for ex 123+123 = 123123 when it supposed to be 246 I don't know how to fix this I tried everything and nothing has worked for me

    if (message.content.toLowerCase().startsWith(prefixS.prefix + 'work')) {
    const inventoryS = await inventory.findOne({ userID: message.author.id, guildID: message.guild.id });
    if (inventoryS.item1 === 'true') {
      const payment = Math.floor(Math.random() * 125) + 25;
      inventoryS.currency += payment
      inventoryS.save()
      message.channel.send({ embeds: [new Discord.MessageEmbed().setTitle('After a long day of work your balance is').setDescription(`__Your balance__\n > Money: ${inventoryS.currency}`).setColor('BROWN')] })
    }
    }
It'sMateo20
  • 105
  • 2
  • 8

2 Answers2

4

it is possible that your inventoryS.currency is a string value

let a = '123'     //a string
let b = 123      //an integer
a += b
console.log(a)   //logs 123123

you will need to parse it to an integer before adding

let a = '123'
let b = 123

a = parseInt(a) + b
console.log(a)

more references here

cmgchess
  • 7,996
  • 37
  • 44
  • 62
4

Both of them must be Numbers to do addition, otherwise it will add as strings

inventoryS.currency = parseInt(inventoryS.currency) + parseInt(payment)
MrMythical
  • 8,908
  • 2
  • 17
  • 45