1

enter image description here

above is result of below snippet of code

 var total_points = 0
for(var i = 0; i < req.body.requisites.length; i++){

    total_points = total_points + req.body.requisites[i].points
    console.log(req.body.requisites[i].points ,  total_points)
}
console.log('total points :' + total_points)
req.body.points = total_points

I am not getting why one time it is concatenating the results (see the last values before 'total points') and next time it calculates correctly.

Appreciated if you can help.

Thanks in advance!

3
  • We'd have to see the data you start with, but strings concatenate when added and numbers add when added. Perhaps one of your values (like the "6") is a string, not a number? Commented Jul 29, 2018 at 18:31
  • And, when you mix and match numbers and strings, it follows Javascript's complicated type conversion rules to figure out what happens. Commented Jul 29, 2018 at 18:36
  • Can you post the input? req.body.requisites?? Commented Jul 29, 2018 at 18:39

2 Answers 2

2

As per my earlier comment, it seems like some of your input must be a string instead of number and because of Javascript's coercion rules when adding a string and a number you are getting string concatenation instead of math addition.

You can force all the input to numbers so you always get addition like this:

var total_points = 0
for (var i = 0; i < req.body.requisites.length; i++) {
    total_points = total_points + (+req.body.requisites[i].points);
    console.log(req.body.requisites[i].points ,  total_points)
}
console.log('total points :' + total_points)
req.body.points = total_points

And, it might be easier to use .reduce():

req.body.points = req.body.requisites.reduce((total, val) => total + (+val)), 0);

The (+req.body.requisites[i].points) or (+val) converts it to a number if it was a numeric string.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks mate! it worked. Just to understand it better, if you notice concatenation is happening only in the last case. If you can put some light that would be helpful.
@MohitAggarwal - Do you have a mix of strings and numbers in the input?
No, I have purely numbers as input.
@MohitAggarwal - There's no way you have purely numbers. Look at this demo: jsfiddle.net/jfriend00/m746jc9L. Add a console.log(typeof req.body.requisites[i].points); to your output to see for sure.
0

You code seems to be okay.

This might be happening because you are sending the value for one of req.body.requisites[points] as string and that is why it gets concatenated instead on addition.

Check your input or update the question with the input you are passing ie, req.body

Hope this helps you know the reason behind the mess!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.