0

i try to access this function in my Object with the console.log but i don't really understand why i can't access it! I'm beginning Javascript but i'm really stuck with accessing functions in Object.

Thanks for the help

const hotel = {
 name: "Grand Hotel",
 location: "Stockholm",
 pricePerNight: 2200,
 roomBooked: 23,
 totalRoom: 223, 
 roomAvailable: function(){
 return this.totalRoom - this.roomBooked;
 
 } 
 };
 hotel.roomAvailable();
 console.log(hotel.roomAvailable);

asked Nov 12, 2018 at 19:15
1
  • 3
    First you call the function but ignore the return value, then you log the function itself but without calling it. Did you mean console.log(hotel.roomAvailable());? Commented Nov 12, 2018 at 19:17

3 Answers 3

2

You're just missing the parentheses in the log function:

hotel.roomAvailable()
answered Nov 12, 2018 at 19:17
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! i feel dumb haha
0

You are already invoking the function which will return the value. Just log the return value instead.

In your code you are just logging the definition of the function but not really invoking it.

const hotel = {
 name: "Grand Hotel",
 location: "Stockholm",
 pricePerNight: 2200,
 roomBooked: 23,
 totalRoom: 223,
 roomAvailable: function() {
 return this.totalRoom - this.roomBooked;
 }
};
var isRoomAvailable = hotel.roomAvailable();
console.log(isRoomAvailable);

answered Nov 12, 2018 at 19:18

Comments

0

Is this what you want?

const hotel = {
 name: "Grand Hotel",
 location: "Stockholm",
 pricePerNight: 2200,
 roomBooked: 23,
 totalRoom: 223, 
 roomAvailable: function(){
 return this.totalRoom - this.roomBooked;
 
 } 
 };
 
 console.log(hotel.roomAvailable());

answered Nov 12, 2018 at 19:19

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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