I'm trying to use global arrays to store some data to be used at a later course.
However when I run through the chrome debugger console the push function doesn't add them to the array associated either, nor do I get any errors.
I've looked at many other examples and cannot see where I'm going wrong.
Could someone show me where I'm going wrong?
JavaScript:
var hv = ['1', '2', '3', '4'];
$(document).ready(function () {
for (var i = 0; i < hv.length; i++) {
GetVmsOnHyper(hv[i]);
}
});
function GetVmsOnHyper(id) {
$.ajax({
type: "GET",
url: "/api/function/" + id,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: virtualmachineinfo
})
}
var avrg_mem = [];
var avrg_cpu = [];
var avrg_lng = [];
function virtualmachineinfo(vmData) {
var vmcount = vmData.length;
avrg_lng.push(vmcount);
var mem_size = "medium";
var cpu_size = "medium";
for (var i = 0; i < vmData.length; i++) {
var vm_mem = vmData[i].memory;
if (vm_mem > 6143) {
mem_size = "large";
}
else if (vm_mem < 2047) {
mem_size = "small";
}
avrg_mem.push(mem_size);
var vm_cpu = vmData[i].cpus;
if (vm_cpu > 4) {
cpu_size = "large";
}
else if (vm_cpu < 2) {
cpu_size = "medium";
}
avrg_cpu.push(cpu_size);
}
Solution
Not a solution as such but building a function to check the contents of the array rather than just going on what the Chrome debug console told me shows that the code was working.
Adding the below at the end to check:
function checkArrays() {
for (i = 0; i < avrg_mem.length; i++) {
var whatis = avrg_mem[i];
}