I’m trying to build a simple plugin to randomize the order of an array of a custom data type.
I’m running a function that shuffles the array randomly and then publishing the results back via instance.publish
On doing this, I get an error:
My shuffling function in INITIALIZE:
instance.data.shuffle = function () {
var a = instance.data.inputList;
var j, x, i;
for (i = a.length - 1; i > 0; i--) {
j = Math.floor(Math.random() * (i + 1));
x = a[i];
a[i] = a[j];
a[j] = x;
}
instance.data.outputList = a;
instance.publishState("randomized_list", instance.data.outputList);
}
UPDATE:
function(instance, properties, context) {
instance.data.inputList = properties.list;
console.log(properties.list, instance.data.inputList);
instance.data.outputList = [];
try{
instance.data.randomizeList();
instance.publishState("randomized_list", instance.data.outputList);
}
catch(err){
instance.publishState("randomized_list", null);
console.log("Shuffling error", err);
}
}
It’s probably something simple - but I think there’s a lot for me to learn from solving this problem. Any help is welcome!
Thanks,
Ranjit