1

I am retrieving data from a table. The data is numbers, but it was saved as TINYTEXT in the mySql database. I can retrieve it and access it just fine, but I'm running into trouble because I need to add numbers.

So while I want 4 + 5 + 3 = 12.... instead I get the result of "453".

This is my calculation on the retrieved data:

let consultingScore = 0;
let consultingScoreDiv = 0;
   for (let i = 0; i < result.length; i++){
   let tempConScore = result[i].formValue01 + 
   result[i].formValue02 + result[i].formValue03;

   consultingScore = consultingScore + tempConScore;

   this.setState({ surveyScoreConsulting: consultingScore }); <--- (This should be 12, but is instead "453"
                }

I have tried to use parseInt() like below, but when I do, then the value then becomes NaN. Am I not using parseInt correctly? It seems it should be easier than this to convert sting numbers to integers so I can perform calculations with them.

let consultingScore = 0;
let consultingScoreDiv = 0;
   for (let i = 0; i < result.length; i++){
   let tempConScore = parseInt(result[i].formValue01) + 
   parseInt(result[i].formValue02) + parseInt(result[i].formValue03);

   consultingScore = consultingScore + tempConScore;

   this.setState({ surveyScoreConsulting: consultingScore }); <--- (This should be 12, but is instead "453"
                }
Adam Norton
  • 512
  • 2
  • 5
  • 21
  • Does this answer your question? [Adding two numbers concatenates them instead of calculating the sum](https://stackoverflow.com/questions/14496531/adding-two-numbers-concatenates-them-instead-of-calculating-the-sum) – pilchard Oct 19 '21 at 22:56
  • Actually yes that fixed it. Thanks! I've never seen the solution of adding the +as a prefix to the variable before. I appreciate the help! – Adam Norton Oct 19 '21 at 23:02
  • Good to hear that it worked, it's weird for me too first time I hear about this – Haroun Darjaj Oct 20 '21 at 00:05

1 Answers1

0

Try to parse with base 10, like this parseInt(yourValue, 10)

Haroun Darjaj
  • 274
  • 1
  • 6