Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have a problem with the '+=' operator in NodeJs: when I add a number variable to an another number variable it combine them together

const ricarica = (conto, deposito) => {
    deposito = prompt("Quanti soldi vuole depositare sul suo conto? ");
    
    conto += deposito;
    return console.log(`Ora il suo conto ha una disponibilità residua di ${conto}€`);
}

If the ‘conto’ statement is equal to 0 and ‘deposito’ is equal to 200, the final output is 0200.

I’ve tried to convert ‘deposito’ and ‘conto’ into float values but nothing changed.

Please help me to solve this problem 🥺

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Since Javascript is loosely-typed, it is viewing conto and deposito as strings and appending them. Convert the to numbers before performing the addition:

let a = Number.parseInt(conto);
let b = Number.parseInt(deposito);
a += b;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading