0

I'm working on a custom application. It is storing a user's workout plan in an array. I have my array in the following format: [week][day][exercise]. A user might have multiple exercises in a day.

I have a function that is validating the workout to make sure the proper types of workouts are selected per week.

Here is my function:

function verifyWorkout(workout, wtype){
    var errorlist = "";
    try{
        for(i=1;i<workout.length;i++){
            var cardiocount = 0;
            var strengthcount = 0;
            var corecount = 0;
            var hiitcount = 0;

            for(j=1;j<workout[i].length;j++){
                for(k=0;k<workout[i][j].length;k++){
                    if (workout[i][j][k].extype == "Cardio"){
                        cardiocount += cardiocount + 1;
                    }else if (workout[i][j][k].extype == "Strength"){
                        strengthcount += strengthcount + 1;
                    }else if (workout[i][j][k].extype == "Core"){
                        corecount += corecount + 1;
                    }else if (workout[i][j][k].extype == "Hiit"){
                        hiitcount += hiitcount + 1;
                    }
                }
            }

            if (wtype === "Beginner"){
                if (i = 1){
                    if (cardiocount < 1){
                        errorlist = errorlist + "Week 1 requires at least 1 cardio exercise.</br>";
                    }
                    if (strengthcount < 1){
                        errorlist = errorlist + "Week 1 requires at least 1 strength exercise.</br>";
                    }
                    if (corecount < 1){
                        errorlist = errorlist + "Week " + i + " requires at least 1 core exercise.</br>";
                    }
                } else if (i = 2){
                    if (cardiocount < 2){
                        errorlist = errorlist + "Week 2 requires at least 2 cardio exercises.</br>";
                    }
                    if (strengthcount < 1){
                        errorlist = errorlist + "Week 2 requires at least 1 strength exercise.</br>";
                    }
                    if (corecount < 1){
                        errorlist = errorlist + "Week " + i + " requires at least 1 core exercise.</br>";
                    }
                } else {
                    if (cardiocount < 3){
                        errorlist = errorlist + "Week " + i + " requires at least 3 cardio exercises.</br>";
                    }
                    if (strengthcount < 2){
                        errorlist = errorlist + "Week " + i + " requires at least 2 strength exercises.</br>";
                    }
                    if (corecount < 1){
                        errorlist = errorlist + "Week " + i + " requires at least 1 core exercise.</br>";
                    }
                }
            }else if (wtype === "Intermediate"){
                if (cardiocount < 3){
                    errorlist = errorlist + "Week " + i + " requires at least 3 cardio exercises.</br>";
                }
                if (strengthcount < 2){
                    errorlist = errorlist + "Week " + i + " requires at least 2 strength exercises.</br>";
                }
                if (corecount < 1){
                    errorlist = errorlist + "Week " + i + " requires at least 1 core exercise.</br>";
                }
            }else if (wtype === "Advanced"){
                if (cardiocount < 2){
                    errorlist = errorlist + "Week " + i + " requires at least 2 cardio exercises.</br>";
                }
                if (strengthcount < 2){
                    errorlist = errorlist + "Week " + i + " requires at least 2 strength exercises.</br>";
                }
                if (hiitcount < 2){
                    errorlist = errorlist + "Week " + i + " requires at least 2 hiit exercises.</br>";
                }
                if (corecount < 1){
                    errorlist = errorlist + "Week " + i + " requires at least 1 core exercise.</br>";
                }
            }

        }

        return errorlist;

    } catch(err) {
        console.log("error retrieving validation....");
        console.log(errorlist);
        console.log(err);
    }

}

When I try to call it, I'm getting an allocation size overflow error.

Please let me know what I'm doing wrong.

Brett Davis
  • 152
  • 1
  • 14
  • 2
    `if (i = 1)` makes this into an infinite loop. Change to `if (i == 1)` (and similarly elsewhere). – Amadan Jul 29 '15 at 06:10

1 Answers1

0

Your problem is in these lines:

if (i = 1){
} else if (i = 2){

Those are assignments. You want to use the equality check instead:

if (i === 1){
} else if (i === 2){
Cerbrus
  • 70,800
  • 18
  • 132
  • 147