I have a function
node containing the following code:
// set number of boxes/prisoners
const MaxPrisoners = 20;
// create PrisonerBoxes array
let PrisonerBoxes = [];
// set which box/prisoner to start from
const startingNumber = msg.payload - 1;
// create Loop array
const Loop = [];
// fill PrisonerBoxes with values
for (let i = 1; i <= MaxPrisoners; i++) {
PrisonerBoxes.push(i);
};
// shuffle values in array of PrisonerBoxes
let res = PrisonerBoxes.slice(0);
for (let j, x, i = res.length; i; j = Math.floor(Math.random() * i), x = res[--i], res[i] = res[j], res[j] = x);
PrisonerBoxes = res;
// get value in the currently open box
let boxValue = PrisonerBoxes[startingNumber];
// set value of nextBox to open
let nextBoxValue = boxValue;
// iterate through boxes until starting value is found
for (let i = startingNumber; i != boxValue; i = nextBoxValue - 1) {
if (PrisonerBoxes[i]) {
Loop.push(PrisonerBoxes[i]);
nextBoxValue = PrisonerBoxes[i];
boxValue = PrisonerBoxes[nextBoxValue];
};
};
// display the resulting Loop as msg.loop
msg.loop = Loop;
// display the resulting PrisonerBoxes as msg.boxes
msg.boxes = PrisonerBoxes;
// return the msg object
return msg;
Sometimes when it runs I get a return like this:
{
"_msgid":"e03a58c68912c40b",
"payload":5,
"loop":[17,7,2,3,16,20,14,18,6,11,9,15,10],
"boxes":[13,3,16,5,17,11,2,4,15,1,9,12,8,18,10,20,7,6,19,14]
}
But other times I get this in the console output
# Fatal error in , line 0
# Fatal JavaScript invalid size error 169220804
#
#
#
#FailureMessage Object: 00000080594FD2E0
1: 00007FF747E11B4F v8::internal::CodeObjectRegistry::~CodeObjectRegistry+115135
2: 00007FF747D2EA2F std::basic_ostream<char,std::char_traits<char> >::operator<<+64591
3: 00007FF748AF2432 V8_Fatal+162
4: 00007FF748664D85 v8::internal::FactoryBase<v8::internal::Factory>::NewFixedArray+101
5: 00007FF74852206E v8::debug::Script::GetIsolate+16350
6: 00007FF7483937CD v8::internal::CompilationCache::IsEnabledScriptAndEval+27293
7: 00007FF74883F6A1 v8::internal::SetupIsolateDelegate::SetupHeap+513649
8: 0000019DF2F6D8B3
I'm still somewhat new to JS and Node-RED, normally errors give me something to go off of, but I'm stumped here. Am I doing something to an array wrong? or is it something else?
Happens on a Raspberry Pi 4, Node-RED 2.2.2 node v16.15.1 npm v8.11.0
and on Windows 10 build 21H2, Node-RED 2.1.4 node v17.6.0 npm v8.3.0