JSFiddle - React, Tailwind, and code Playground
JavaScript
// instructions
/*
create a constuctor function for hotel and assign is the given properties
cerate a constructor function for hotel and assign is given properties including addroom
add rooms to the a new hotel
add a method to get available tooms for hte given hotel
*/
function hotel(number, capacity, ifoccupied){
}
//3.1
function room(number, capacity, ifoccupied){
this.number = number
this.capacity = capacity
this.ifoccupied = null;
}
let room1 = new room(1, 2, false);
let room2 = new room(1, 2, false);
let room3 = new room(1, 2, false);
let room4 = new room(1, 4, false);
let room5 = new room(1, 4, false);
//roomsArr
//3.2
function hotel(rooms){
this.rooms = [];
this.addrooms = function(roomss){
this.rooms.push(roomss);
//3.3
this.getAvailableRooms = function getAvailableRoomo(capacityneed){
let available = []; //reference to the rooms available array
for(let i =0; i< this.rooms.length; i++){
if(this.rooms[i].capacity === capacityneed || this.rooms[i].capacity > capacityneed) //check ifthe rooms capacity have the desired capacity
available.push(this.rooms[i])
return available;
}
}
this.bookRoom = function(){
if(this.rooms.ifoccupied === false){
console.log(true);
return true
}else{return false}
}
}
}
let hotel1 = new hotel([])
hotel1.addrooms(room1);
hotel1.addrooms(room2);
hotel1.addrooms(room3);
hotel1.addrooms(room4);
hotel1.addrooms(room5);
console.log(hotel1);
hotel1.getAvailableRooms(1);
console.log(hotel1.getAvailableRooms(1));
hotel1.bookRoom();
console.log(hotel1.bookRoom);