Home > OS >  Numbers adding next to each other (ex. 123 123 = 123123)
Numbers adding next to each other (ex. 123 123 = 123123)

Time:10-31

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')] })
    }
    }

CodePudding user response:

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
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

you will need to parse it to an integer before adding

let a = '123'
let b = 123

a = parseInt(a)   b
console.log(a)
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

more references here

CodePudding user response:

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

inventoryS.currency = parseInt(inventoryS.currency)   parseInt(payment)
  • Related