I'm using a nested loop to iterate over an array bookingArray. If the bookingArray item roomNumber matches the index of the first loop i, that bookingArray item should be pushed to a new array tableArray.
I'm running into an issue whereby multiple entries from my else statement are added to the resultant tableArray. It could potential be related to correct usage of break / continue? I've read up on the documentation but the correct solution eludes me.
Expected Result:
"1: room booked",
"2: room available",
"3: room available",
"4: room available",
"5: room booked",
"6: room available",
"7: room available",
"8: room available",
"9: room booked",
"10: room available"
Fiddle of current code:
var bookingArray = [{
"roomNumber": 1,
},
{
"roomNumber": 5,
},
{
"roomNumber": 9,
}
];
var tableArray = [];
for (var i = 1; i < 11; i++) {
for (var j = 0; j < bookingArray.length; j++) {
if (i == bookingArray[j].roomNumber) {
tableArray.push(bookingArray[j].roomNumber + ": room booked");
break;
} else {
tableArray.push(i + ": room available");
}
}
}
console.log(tableArray);