let totalBooking = 10
let upper = 5
let middle = 3
let lower = 2
let totalReserv = 5
let bookedSeats = []
let reservSeats = []
let waitingSeats = []
const booking = (name, type) => {
Pobj = { name, type } // Setting the object
if (bookedSeats.length < totalBooking) { // check wheather all seats are booked
let istype = bookedSeats.filter((data) => data.type === type) // filter the type for upper,lower , middle
// check weather the type present in the booking
let isavai = type === "upper" ? istype.length < upper :
type === "middle" ? istype.length < middle :
istype.length < lower
if (isavai) { // if available pushed to the booking seat
bookedSeats.push(Pobj)
}
else if ( //if not available moved to the reservation
(!isavai) && (reservSeats.length < totalReserv)) {
console.log(`${type} seat type is not avialable ${name} you have moved to reservation`)
reservSeats.push(Pobj)
}
else { // if reservation filled moved to waiting
waitingSeats.push(Pobj)
}
}
else if (
(bookedSeats.length === totalBooking) && (reservSeats.length < totalReserv)) {
console.log(`${type} seat type is not avialable ${name} you have moved to reservation`)
reservSeats.push(Pobj)
}
else {
waitingSeats.push(Pobj)
}
}
const cancelBooking = (name, type) => {
filterdata = bookedSeats.filter((data) => data.name !== name) // filter the value that does not contains the name
cancelledData = bookedSeats.find(data => data.name === name) // taking out the data of deleting object
bookedSeats = [...filterdata] // set the current filtered array as new array
let waitingtypedata = reservSeats.filter((data) => {
return data.type === cancelledData.type
}) // getting the array which contains only the cancelled type
result = waitingtypedata.pop() // getting the first array from the list of selected type array
bookedSeats.push(result) // joinig to the booked array
reserFilterdata = reservSeats.filter((data) => data.name !== result.name)
reservSeats = [...reserFilterdata] // setting the new reserv array as reserve array
}
booking("Dev", "upper")
booking("Jon Pandian", "upper")
booking("Vijay kumar", "upper")
booking("John wick", "upper")
booking("Tony Stark", "upper")
booking("Peter", "upper")
booking("Rakesh", "lower")
booking("Josap kurumila", "lower")
booking("Dr Octobus", "middle")
booking("Optimus Prime", "middle")
booking("Tanos", "middle")
booking("Joker", "middle")
cancelBooking("Jon Pandian", "upper")
console.log("Booked seates", bookedSeats)
console.log("Reerved seates", reservSeats)
console.log("Waiting seates", waitingSeats)
For further actions, you may consider blocking this person and/or reporting abuse
Top comments (0)