diff --git a/JS-object-constructors/book-constructor-exercise.js b/JS-object-constructors/book-constructor-exercise.js new file mode 100644 index 0000000..5ebf4bf --- /dev/null +++ b/JS-object-constructors/book-constructor-exercise.js @@ -0,0 +1,17 @@ +function Book(title, author, pages, isRead) { + this.title = title; + this.author = author; + this.pages = pages; + this.isRead = isRead; + this.info = function () { + if (!isRead) { + return `${this.title} by ${this.author}, ${this.pages} pages, not read yet`; + } else { + return `${this.title} by ${this.author}, ${this.pages} pages, has been read`; + } + }; +} + +const myBook = new Book("Harry Potter", "J. K. Rowling", 495, true); + +console.log(myBook.info()); diff --git a/JS-object-constructors/web-banking-assignment.js b/JS-object-constructors/web-banking-assignment.js new file mode 100644 index 0000000..e8c0ddb --- /dev/null +++ b/JS-object-constructors/web-banking-assignment.js @@ -0,0 +1,35 @@ +"use strict"; + +function Account(fullname) { + this.fullname = fullname; + Account.IBAN++; + this.IBAN = "GR000" + Account.IBAN; + this.balance = 0; + this.deposit = function (amount) { + return this.balance = this.balance + amount; + }; + this.withdraw = function (amount) { + if (typeof amount !== "number" || amount <= 0){ + return "Error 'Invalid amount'" + } + if (amount > this.balance) { + return "Error 'Insufficient balance!'" + } + return this.balance = this.balance - amount; + }; + this.getBalance = function () { + return this.balance; + }; +} + +Account.IBAN = 10003; + + +const newAccount = new Account("Kostas Minaidis"); + + +newAccount.getBalance(); // 0 +newAccount.deposit(100); +newAccount.getBalance(); // 100 +newAccount.withdraw(50); +newAccount.getBalance(); // 50