0
function Book(title, author, isbn, stock, cost) {
  this.title = title;
  this.author = author;
  this.isbn = isbn;
  this.stock = stock;
  this.cost = cost;
  this.summary = function () {
    `${this.title} by ${this.author} (ISBN: ${this.isbn}) costs $ ${this.cost}`;
  };
  this.isAvailable = function () {
    this.stock > 0
      ? `${this.title} is available`
      : `${this.title} is not available at the moment`;
  };
}

const product = new Book(
  "The Design of Everyday Things",
  "Don Norman",
  "978 - 0 - 465 - 05571 - 5",
  10,
  17.99
);
console.log(product.summary());
console.log(product.isAvailable());

It has to print:

"The Design of Everyday Things by Don Norman (ISBN: 978 - 0 - 465 - 05571 - 5) costs $17.99"

"The Design of Everyday things is available"

2
  • 2
    You have to use return to get a result returned by a normal function call. Commented Dec 25, 2022 at 15:23
  • Use linters like ESLint or JSHint to find problems with your code immediately. Relevant linter warning: “Expected an assignment or function call and instead saw an expression.”. Commented Dec 25, 2022 at 15:28

1 Answer 1

2

You're not returning anything in your functions:

function Book(title, author, isbn, stock, cost) {
  this.title = title;
  this.author = author;
  this.isbn = isbn;
  this.stock = stock;
  this.cost = cost;
  this.summary = function () {
    return `${this.title} by ${this.author} (ISBN: ${this.isbn}) costs $ ${this.cost}`;
  };
  this.isAvailable = function () {
    return this.stock > 0
      ? `${this.title} is available`
      : `${this.title} is not available at the moment`;
  };
}

const product = new Book(
  "The Design of Everyday Things",
  "Don Norman",
  "978 - 0 - 465 - 05571 - 5",
  10,
  17.99
);
console.log(product.summary());
console.log(product.isAvailable());

Using arrow functions, you can skip the return keyword if you prefer:

function Book(title, author, isbn, stock, cost) {
  this.title = title;
  this.author = author;
  this.isbn = isbn;
  this.stock = stock;
  this.cost = cost;
  this.summary = () =>
    `${this.title} by ${this.author} (ISBN: ${this.isbn}) costs $ ${this.cost}`;
  
  this.isAvailable = () =>
    this.stock > 0
      ? `${this.title} is available`
      : `${this.title} is not available at the moment`;
}

const product = new Book(
  "The Design of Everyday Things",
  "Don Norman",
  "978 - 0 - 465 - 05571 - 5",
  10,
  17.99
);
console.log(product.summary());
console.log(product.isAvailable());

Sign up to request clarification or add additional context in comments.

1 Comment

i have used : return ${this.title} by ${this.author} (ISBN: ${this.isbn}) costs $ ${this.cost}

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.