Skip to content

Commit

Permalink
Add exercise-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Nguyen committed Jan 28, 2022
1 parent 82ca86e commit 08c98de
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions 7-classes/exercises/exercise-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
"use strict";
//Your code does here

class Book {
constructor(title, author, pages) {
this.title = title;
this.author = author;
this.#pages = pages;
this.copiesSold = 0;
}

get pages() {
return this._pages;
}

// private
set #pages(value) {
this._pages = value;
}

get copiesSold() {
return this._copiesSold;
}

set copiesSold(value) {
if (value < 0) {
throw new Error("Value can't be negative");
}
this._copiesSold = value;
}
}

const book = new Book("Who Moved My Cheese?", "Spencer Johnson", 96);
console.log(book.title); //Who Moved My Cheese
console.log(book.pages); //96
try {
book.pages = 96;
} catch (ex) {
console.log(ex.message);
//Cannot set property pages of #<Book> which has only a getter
}
console.log(book.copiesSold); //0
book.copiesSold = 1;
console.log(book.copiesSold); //1
try {
book.copiesSold = -2;
} catch (ex) {
console.log(ex.message); //Value can't be negative
}
console.log(book.copiesSold); //1

0 comments on commit 08c98de

Please sign in to comment.