0

Here is what I've done so far

function setcanvas(ID,Count,Count1,Count2,Count3,Count4,Count5,Count6,Count7,Count8,Count9,Count10){
  var ctx = document.getElementById(ID).getContext('2d');
  ctx.canvas.width = 350;
  ctx.canvas.height = 300;
  var arr=[];
  var y=1;
  var str = "Count";
  while(y <= Count){
    var str2 = str + y;
    arr.push(str2);
    y++;
  }
}

I expect the value of my array to be the value on the parameters but Its not. Instead the values I see are Count1, Count2, Count3 .... Can someone explain this to me and are there any way to achieve this goal ?

Solution Found

Thanks from the comment below. I've come up with this solution.

  function setcanvas(ID,Count,Count1,Count2,Count3,Count4,Count5,Count6,Count7,Count8,Count9,Count10){
  var ctx = document.getElementById(ID).getContext('2d');
  ctx.canvas.width = 350;
  ctx.canvas.height = 300;
  var myBarChart = new Chart(ctx, {
    type: 'horizontalBar',
    data: {
        labels: ["Analytics 1", "Analytics 2", "Analytics 3", "Analytics 4", "Analytics 5"],
        datasets: [{
            label: "Count",
            data: getCountByArguments(Count1, Count2, Count3, Count4, Count5, Count6, Count7, Count8, Count9, Count10),
            backgroundColor: setUpBC(Count)
        }]
    },
    options: {}
  });

  function getCountByArguments(){
    var arr = [];
    var i=0; 
    while(i < Count) {
        arr.push(arguments[i]);
        i++;
    }
    return arr;
  }

 }

Added the function getCountByArguments

Gilbert Mendoza
  • 405
  • 1
  • 7
  • 16
  • use can use `arguments` object to get arguments this might be helpful https://stackoverflow.com/questions/4633125/is-it-possible-to-get-all-arguments-of-a-function-as-single-object-inside-that-f – Srinivas ML Jun 29 '17 at 08:50
  • Yeah, `str + y` does not magically make Javascript evaluate that as a variable. What you want is *variable variables*, but in this specific case you should be looking at the `arguments` object instead. – deceze Jun 29 '17 at 08:50
  • thanks guys just from knowing this arguments I found a solution – Gilbert Mendoza Jun 29 '17 at 09:00

1 Answers1

0

use can use arguments object to get arguments , something like below code

function setcanvas(ID,Count,Count1,Count2,Count3,Count4,Count5,Count6,Count7,Count8,Count9,Count10){

  var arr=[];
for (var i=2; i < arguments.length; i++) {
        arr.push(arguments[i])
    }
    console.log(arr);
}

setcanvas(1,10,"a","b","c","d","e","f","g","h","i","j");

setcanvas(1,3,"a","b","c");
Srinivas ML
  • 732
  • 3
  • 12