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);
}
}
Source: stackoverflow.com
Related Query
- My for loop does not iterate in Javascript
- Android: WebView with JavaScript for Chart.js not working properly
- chart.js version 2.4.0 does not display bars for me
- Problem with script src for Chart.js. The CDN for Chart.js funtions fine, so my code is ok. Somehow I'm not linking the file correctly
- fill: 'origin' property does not work for my chart
- Chartjs backgroundColor for line chart does not appear in Vue app
- Why ChartJS's Bar Chart does not render bar for a specific value?
- React-chart does not render the legend for PIE chart
- How do I loop under Chart js in JavaScript for multiple axis?
- Angular ChartJs does not show Data for multiple Charts
- 'require is not defined' error when attempting to use chartjs in javascript code
- Does iFrame have any events suitable for when a JavaScript & HTML page are embedded in modal(Bootstrap)
- Property 'getContext' does not exist on type 'HTMLElement'
- show label in tooltip but not in x axis for chartjs line chart
- Html chart does not fit a small Android WebView
- chartjs height does not follow parent container
- Chart looks only grey, does not show the color - Chartjs,discordjs
- Wkhtmltopdf does not render Chart.JS 2.5.0 graph
- Angular-chart.js - Make line chart does not curve
- ChartJS bar not showing up for simple data points
- ng2-charts\ng2-charts.js does not export ChartsModule
- Vue Chart.js component does not render
- Is there a JavaScript Method/ Function to loop Objects via Keys into a array?
- Loop Dataset ChartJS Javascript
- Chart.js Chart in Angular does not load until resizing browser window
- Iterate over query for use in Charts.js using Coldfusion
- Display multiple chart.js charts using for loop in Django template
- Chart.js in AngularJS tabset does not render
- react-chart-js-2 in combination with TypeScript for LineCharts: Uncaught Error: "point" is not a registered element
- Chart.js click event to open a custom tooltip does not work sometimes
More Query from same tag
- How to change space between columns in horizontalBar in Chart.js?
- Protractor - Get array from canvas attribute in Angular2 using ng2-charts/Chart.js
- Chart JS axes extension
- React + ChartJS V3: Annoations don't work
- How can I scale my dataset values as a percentage of the index in chart.js?
- Chart.js 2.0 - vertical lines
- How to assign variables to data passed through a WEB API in Angular?
- Chart.js: How to change the x-Axes background color?
- ChartJS 2.6 - Change color of bar in Bar chart programmatically
- Chart.js StackedBar doesn't show labels on bar and maximum y-value
- Hide empty bars in Grouped Stacked Bar Chart - chart.js
- ChartJS - TypeError: Cannot read property '_model' of null - Angular
- Accessing .csv data from views file
- Horizontal scroll in vue-chartjs
- How to dynamically use chartjs-plugin-annotation with ng2-charts?
- how to show REALTIME chartjs of specific ID in mysql when click on corresponding marker on googlemap javascript
- Chart.js core.js:6162 ERROR Error: "line" is not a registered controller
- How to subscribe to elements in a response object
- chart.js v2 setting the y scale options seem to getting ignored
- Chartjs v7.0 with plugin zoom, strange effect when use "drag" mode.
- How to show Y axis ticks for every point in the graph
- Laravel 5 Chart Js rendering multiple charts in blade template error
- ChartJS horizontal label are 1-2px clipped
- ChartJS 2.7.0 updating and exporting as png
- Modify Chart js
- Why chartjs lags pages on mobile browsers?
- Background colour of line charts in chart.js
- charts.js show dates 1 year
- how to combine to array for chartjs width php
- Why chart renders after second click - react-chartjs-2