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

Need help to convert NaN value to 0 inside app.post in .js file

In below code question1 value is coming as NaN i want to convert it to 0 but i am not able to do it using parseInt.Please help me if i can convert NaN value to 0

app.post('/auth/total',function(req,res){
    console.log(req.body);
    const {  question1, question2, question3, question4} = req.body;
    let total1 = parseInt(question1) + parseInt(question2) 
    let total2 = parseInt(question3) + parseInt(question4) 
    
    //some code to insert data in db and render the view 

});

>Solution :

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

you can do this:

let total1 = (parseInt(question1) || 0) + (parseInt(question2) || 0)

this will convert any falsy value to 0

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