score:0

 for(var i in questions){

       for(var i = 0; i < marks.length; i++){

using i for both loops is causing the behaviour you are seeing.

score:0

you cannot use the same variable name in nested for-loops. this means the i-var will be incremented in all the loops, meaning the code will not be executed as you want it to.

change the variable names to other letters to fix your problems.

score:1

you are using the same variable i ,change the inner loop to something else

       for(var j in questions){
            var name= questions[j]["name"];
            chartjslabels.push(name);
            console.log(name);

            var marks = questions[j]["mark"];
            var sum = 0;
            for(var k = 0; i < marks.length; i++){
                sum+= parseint(marks[k]);
            }
            var avg = sum/marks.length;
            chartjsdata.push(avg);
        }

score:1

the problem here is very simple, you are using the i variable in 3 nested for loops, so its value gets incremented several times, causing the parent for loop to run only once.

score:4

its because you use the same variable i for each loop, use it with different increments [i,j,z] here

$(document).ready(function(){
    $.getjson("/rest/batchservice/batches", function(json)
    {
        //json.length = 2, json is an arraylist of jsonobjects
        for(var i = 0; i < json.length; i++){
            //get batchid
            batchid = json[i]["batch"]["batch_id"];
            //put batchids into an option tag and put it in select tag
            var option=$('<option />').val(json[i]["batch"]["batch_id"]).text("batch " +json[i]["batch"]["batch_id"]);
            $("#dropdownbatches").append(option);

            //get from 'questions' the name and marks and put the variables in a chart
            var questions = json[i]["batch"]["questions"];
            for(var j in questions){
                var name= questions[j]["name"];
                chartjslabels.push(name);
                console.log(name);

                var marks = questions[j]["mark"];
                var sum = 0;
                for(var z = 0; z < marks.length; z++){
                    sum+= parseint(marks[z]);
                }
                var avg = sum/marks.length;
                chartjsdata.push(avg);
            }
        }

Related Query

More Query from same tag