so I am fairly new to javascript and I was trying to write a special shift scheduling code for a friend. The way it currently works is as follow :
function walk(currentDay) {
var today = allWorkDays[currentDay]; // An array of all workdays we need to schedule
var vertices = fetchCombinationsForToday(today); // Fetch an array of 0 length or more
// containing possibilities for the day
// according to rules set by user
for (var i=0; i<vertices.length; i++) {
[we add the vertices[i] to a running array]
walk(currentDay+1);
}
if (currentDay == sumOfAllDays) { // We are at a leaf
analyzeSchedule(); // This will keep a copy of the current schedule
// if it has a higher score than X
}
[some business to pop the last node/day we added to our global array]
}
Now the rules specified in the comments are rules that usually analyze the last 5-10 last added elements(days) and return what could be the shifts for today.
The problem I have here is that I want the program to be able to come up with schedules even with an array of more than a thousand days, but I would exceed the function calls limit due to recursion. Is there any way to walk a tree without using recursion in javascript? I can’t seem to find one even though most say that problems solvable by recursion can be solved by loops and vice versa.
Keep in mind that the vertices array is big (20-30 elements) early in the tree but quickly gets small (0-5 elements). I never ran this code [EDIT: and got a “function calls limit reached” error] by the way it is all theory [EDIT: the fact that I will reach it] for now.
JavaScript Arrays provide methods to push/pop and shift/unshift values onto/off of their beginning and end, so you can use them like a queue. For example:
This way you can walk a tree structure and keep track of nodes to-be-visited by pushing and popping/unshifting from an array.