'mousePressed to for loop
I haven't coded in a long time and I'm pretty rusted. I'm doing a project in which I need to draw a bunch of flight paths from JSON files. For testing, I created this mouse pressed function but I want to automate the process and have the flight path drawn once the previous one is done. I assume I would need nested for loops for it to work but I'm having issues coming up with the loop structure
function mousePressed() {
if (flightSelector < flightArray[daySelector].length - 1) {
flightSelector++;
console.log(flightSelector);
} else {
flightSelector = 0
if (daySelector < dayArray.length - 1) {
daySelector++;
}
}
getFlightData(dayArray[daySelector], flightArray[daySelector][flightSelector]);
}
Thanks
Solution 1:[1]
This is a classic scenario for a pair of nested loops. You are running some code repeatedly for each day, and for each day you are running it repeatedly for each flight that happened that day:
for (let daySelector = 0; daySelector < dayArray.length; daySelector++) {
for (let flightSelector = 0; flightSelector < flightArray[daySelector].length; flightSelector++) {
getFlightData(dayArray[daySelector], flightArray[daySelector][flightSelector]);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paul Wheeler |
