0

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);

2
  • 1
    what would you like tableArray to look like after the loop is complete? Commented Jun 24, 2017 at 23:57
  • I'm just testing the concept, the data is a lot different in the actual application. I'm happy with it just being an item in an array for the purpose of this. Thank you. Commented Jun 25, 2017 at 0:04

2 Answers 2

2

Because the inner loop runs multiple times for every outer loop iteration. Use the inner loop just to set a flag and once it completes then push once per outer loop iteration based on that flag:

var bookingArray = [{
    "roomNumber": 1,
  },
  {
    "roomNumber": 5,
  },
  {
    "roomNumber": 9,
  }
];

var tableArray = [];


for (var i = 1; i < 11; i++) {
  var isBooked = false;
  for (var j = 0; j < bookingArray.length; j++) {
    if (i == bookingArray[j].roomNumber) {
      isBooked = true;
      break;
    }
  }
  var msg = isBooked ? ": room booked" : ": room available";
  tableArray.push(i + msg);
}

console.log(tableArray);

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you chariletfl eloquent solution
0

charlietfl's solution is correct. As an alternative, using the library lodash can shorten this code considerably. It can be done in two lines, as shown below

var bookingArray = [{
    "roomNumber": 2,
  },
  {
    "roomNumber": 5,
  },
  {
    "roomNumber": 9,
  }
];

var booked = _.map(bookingArray, 'roomNumber');
var unbooked = _.difference(_.range(11), booked);

console.log("Booked:", booked);
console.log("Not Booked:", unbooked);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Adding an external library does have a bit of overhead to download, but if you find other spots in your app where lodash is useful (I usually do), then it may be worthwhile to include.

I have no affiliation with the lodash team.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.