I'm trying to create a booking system using HTML and JavaScript. This is a simplified version of my code which should check if the selected time matches any variables from the bookings array.
The loop is working (it works when I replace the if statements with console.log(bookings[i]);).
<html>
<head></head>
<body>
<input type="time" name="time" id="time">
<button id="submit" class="button">Submit</button>
<script>
var bookings = ["10:00", "10:15", "10:30"];
function addBooking() {
for (i = 0; i < bookings.length; i++) {
if (bookings[i] == document.getElementById("time").value) {
alert("Time unavailable.");
} else {
alert("Booking succesful.");
}
break;
}
}
document.getElementById("submit").addEventListener("click", addBooking);
</script>
</body>
</html>
When the form is submitted, the function only checks the first variable (if I type in 10:00 it alerts that time is unavailable but if I type 10:15 or 10:30 it alerts "Booking successful") when it should alert "Time unavailable" if the input matches any variables from the bookings array. EDIT: removing break does NOT solve the problem.
breakkeyword is culprit