How go I get the booked property to increase when the makeBooking
method is called. Not getting the desired result, what am I doing wrong learning JavaScript.
var hotel = { name: "pacific", rooms: 40, bookings: 35, booked: 30, roomType: ['deluxe', 'double', 'suite'], pool: true, gym: true, checkAvailability: function() { return this.rooms - this.booked; }, makeBooking: function() { var roomSpace = this.checkAvailability(); var addBooking = this.booked; if (roomSpace > 0) { addBooking = addBooking++; console.log('room has been booked'); } else { console.log('no room available'); } } }; console.log(hotel.checkAvailability()); var roomTypePush = hotel.roomType; roomTypePush.push('rental'); console.log(roomTypePush); console.log(hotel.booked); console.log(hotel.makeBooking()); console.log(hotel.booked)
this.booked++, when you asign a simple type to a variable it does not link back to the original property
var hotel = { name: "pacific", rooms: 40, bookings: 35, booked: 30, roomType: ['deluxe', 'double', 'suite'], pool: true, gym: true, checkAvailability: function() { return this.rooms - this.booked; }, makeBooking: function() { var roomSpace = this.checkAvailability(); if (roomSpace > 0) { this.booked++; console.log('room has been booked'); } else { console.log('no room available'); } } }; console.log(hotel.checkAvailability()); var roomTypePush = hotel.roomType; roomTypePush.push('rental'); console.log(roomTypePush); console.log(hotel.booked); console.log(hotel.makeBooking()); console.log(hotel.booked)
Please use this snippet.
var hotel = {
name: "pacific",
rooms: 40,
bookings: 35,
booked: 30,
roomType: ['deluxe', 'double', 'suite'],
pool: true,
gym: true,
checkAvailability: function() {
return this.rooms - this.booked;
},
makeBooking: function() {
var roomSpace = this.checkAvailability();
var addBooking = this.booked;
if (roomSpace > 0) {
addBooking = this.booked++
console.log('room has been booked');
} else {
console.log('no room available');
}
}
};
console.log(hotel.checkAvailability());
var roomTypePush = hotel.roomType;
roomTypePush.push('rental');
console.log(roomTypePush);
console.log(hotel.booked);
console.log(hotel.makeBooking());
console.log(hotel.booked)
when you do addbooking = this.booked and then increment addbooking it does not point to the original variable.
Hope this be of some help.
Happy Learning
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.