0

in my current project I need a Pie Chart in which calculated values should be displayed. I have now five values as array, which I need to add, so that I have the desired value. But now I am a bit confused, because no matter if I convert the arrays to sting, or use them directly in the addition, they are always lined up and not added. What am I missing here?

In a subtraction directly after the calculation works, but here I still have a date value (number of days in the month) in the calculation. Why does this calculation work?

My Problem

For example I get here "02400" as result and not "6".

var training = training_intervall + training_longrun + training_speedwork + training_stabilisation + training_competition;

My JS function:

function userDiaryMonthTrainingStats(user_id) {

    $.ajax({
        url: "../diary/includes/training/diary-training-monthly-training-stats.php?user_id=" + user_id,
        type: "GET",
        success: function(monthly_training_stats) {

            var training_intervall = [];
            var training_longrun = [];
            var training_speedwork = [];
            var training_stabilisation = [];
            var training_competition = [];
            var training_injury = [];

            for(var i in monthly_training_stats) {
                training_intervall.push(monthly_training_stats[i].training_intervall),
                training_longrun.push(monthly_training_stats[i].training_longrun),
                training_speedwork.push(monthly_training_stats[i].training_speedwork),
                training_stabilisation.push(monthly_training_stats[i].training_stabilisation),
                training_competition.push(monthly_training_stats[i].training_competition),
                training_injury.push(monthly_training_stats[i].training_injury)
            }

            var date = new Date();
            var month = date.getMonth() + 1;
            var year = date.getFullYear();
            daysInMonth = new Date(year, month, 0).getDate();

            training_intervall = training_intervall.toString();
            training_longrun = training_longrun.toString();
            training_speedwork = training_speedwork.toString();
            training_stabilisation = training_stabilisation.toString();
            training_competition = training_competition.toString();

            var training = training_intervall + training_longrun + training_speedwork + training_stabilisation + training_competition;
            var training_free = daysInMonth - training_intervall - training_longrun - training_speedwork - training_stabilisation - training_competition - training_injury;

            var userMonthlyTrainingStatsData = {
                datasets:  [{
                    data: [training, training_injury, training_free],
                    backgroundColor: ['#36a2eb', '#e33b3b', '#4bc07d']
                }],

                labels: [
                    'Training',
                    'Injury',
                    'Free'
                ]
            };

            ........

        }
    })
}

2
  • 2
    + is also used for string concatenation. If one operand is of type string the result is also a string. Commented Aug 10, 2021 at 17:59
  • I get 6 different values from the DB with different names and then separate them into one array value each in one to one variable before the toString() function. Commented Aug 10, 2021 at 18:11

2 Answers 2

1

use parseInt() to change from a string to int then you can add the strings as they are now numbers

var training = parseInt(training_intervall) + parseInt(training_longrun) + parseInt(training_speedwork + parseInt(training_stabilisation) + parseInt(training_competition);

if you want the result back to a string simply put after this

training=""+training
Sign up to request clarification or add additional context in comments.

Comments

1

Two problems in your code:

  1. training_intervall and the other 4 variables you want to add are arrays, you should iterate them.
  2. The values are strings, using + with strings results in a new concatenated string. To convert easily a string number to a number (example "1" to 1), you can:
const myString = "1"
const myNumber = myString * 1 // myNumber = 1

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.